Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

chore: implement Mul for TruncationPolicy (#10272)

Michael Bolin10ea117ee1e5
diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs
index 080b701d42..1638a243cc 100644
--- a/codex-rs/core/src/context_manager/history.rs
+++ b/codex-rs/core/src/context_manager/history.rs
@@ -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 =
diff --git a/codex-rs/core/src/truncate.rs b/codex-rs/core/src/truncate.rs
index 8150b994d0..441a157375 100644
--- a/codex-rs/core/src/truncate.rs
+++ b/codex-rs/core/src/truncate.rs
@@ -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.
@@ -73,6 +61,21 @@ impl TruncationPolicy {
}
}
+impl std::ops::Mul<f64> for TruncationPolicy {
+ type Output = Self;
+
+ fn mul(self, multiplier: f64) -> Self::Output {
+ 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();