Skip to content
Merged
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
2 changes: 1 addition & 1 deletion codex-rs/core/src/context_manager/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl ContextManager {
}

fn process_item(&self, item: &ResponseItem, policy: TruncationPolicy) -> ResponseItem {
let policy_with_serialization_budget = policy.mul(1.2);
let policy_with_serialization_budget = policy * 1.2;
match item {
ResponseItem::FunctionCallOutput { call_id, output } => {
let truncated =
Expand Down
27 changes: 15 additions & 12 deletions codex-rs/core/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ impl From<TruncationPolicyConfig> for TruncationPolicy {
}

impl TruncationPolicy {
/// Scale the underlying budget by `multiplier`, rounding up to avoid under-budgeting.
pub fn mul(self, multiplier: f64) -> Self {
match self {
TruncationPolicy::Bytes(bytes) => {
TruncationPolicy::Bytes((bytes as f64 * multiplier).ceil() as usize)
}
TruncationPolicy::Tokens(tokens) => {
TruncationPolicy::Tokens((tokens as f64 * multiplier).ceil() as usize)
}
}
}

/// Returns a token budget derived from this policy.
///
/// - For `Tokens`, this is the explicit token limit.
Expand Down Expand Up @@ -73,6 +61,21 @@ impl TruncationPolicy {
}
}

impl std::ops::Mul<f64> for TruncationPolicy {
type Output = Self;

fn mul(self, multiplier: f64) -> Self::Output {
Comment on lines +64 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore inherent mul or keep backward-compatible API

Implementing Mul<f64> is fine for operator use, but removing the inherent TruncationPolicy::mul is a breaking API change: callers using policy.mul(1.2) (without use std::ops::Mul;) will now fail to compile. Mul is not in the prelude, so the method is no longer available by default. If this type is part of the public API, consider keeping an inherent mul that delegates to self * multiplier to preserve downstream compatibility.

Useful? React with 👍 / 👎.

match self {
TruncationPolicy::Bytes(bytes) => {
TruncationPolicy::Bytes((bytes as f64 * multiplier).ceil() as usize)
}
TruncationPolicy::Tokens(tokens) => {
TruncationPolicy::Tokens((tokens as f64 * multiplier).ceil() as usize)
}
}
}
}

pub(crate) fn formatted_truncate_text(content: &str, policy: TruncationPolicy) -> String {
if content.len() <= policy.byte_budget() {
return content.to_string();
Expand Down
Loading