Skip to content

Commit 66f1b2c

Browse files
committed
fix(config): add validation for threshold, ratio, and token count fields
1 parent 40318a8 commit 66f1b2c

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

src/cortex-compact/src/config.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Compaction configuration.
22
3-
use serde::{Deserialize, Serialize};
3+
use serde::{Deserialize, Deserializer, Serialize};
44

55
/// Configuration for auto-compaction.
66
#[derive(Debug, Clone, Serialize, Deserialize)]
77
pub struct CompactionConfig {
88
/// Whether auto-compaction is enabled.
99
#[serde(default = "default_true")]
1010
pub enabled: bool,
11-
/// Token threshold to trigger compaction (percentage of max).
12-
#[serde(default = "default_threshold")]
11+
/// Token threshold to trigger compaction (ratio 0.0-1.0 of max context).
12+
#[serde(default = "default_threshold", deserialize_with = "deserialize_threshold_percent")]
1313
pub threshold_percent: f32,
1414
/// Minimum tokens to keep after compaction.
1515
#[serde(default = "default_min_tokens")]
@@ -25,6 +25,20 @@ pub struct CompactionConfig {
2525
pub preserve_recent_turns: usize,
2626
}
2727

28+
/// Deserialize threshold_percent with validation (must be 0.0-1.0).
29+
fn deserialize_threshold_percent<'de, D>(deserializer: D) -> Result<f32, D::Error>
30+
where
31+
D: Deserializer<'de>,
32+
{
33+
let value = f32::deserialize(deserializer)?;
34+
if !(0.0..=1.0).contains(&value) {
35+
return Err(serde::de::Error::custom(
36+
"threshold_percent must be between 0.0 and 1.0",
37+
));
38+
}
39+
Ok(value)
40+
}
41+
2842
fn default_true() -> bool {
2943
true
3044
}

src/cortex-engine/src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ pub struct Config {
4646
/// Provider configuration.
4747
pub model_provider: ModelProviderInfo,
4848
/// Context window size.
49-
pub model_context_window: Option<i64>,
49+
pub model_context_window: Option<u64>,
5050
/// Auto-compact token limit.
51-
pub model_auto_compact_token_limit: Option<i64>,
51+
pub model_auto_compact_token_limit: Option<u64>,
5252
/// Approval policy.
5353
pub approval_policy: AskForApproval,
5454
/// Sandbox policy.

src/cortex-engine/src/config/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub struct PermissionConfig {
6969
pub struct ConfigToml {
7070
pub model: Option<String>,
7171
pub model_provider: Option<String>,
72-
pub model_context_window: Option<i64>,
73-
pub model_auto_compact_token_limit: Option<i64>,
72+
pub model_context_window: Option<u64>,
73+
pub model_auto_compact_token_limit: Option<u64>,
7474
pub approval_policy: Option<AskForApproval>,
7575
pub sandbox_mode: Option<SandboxMode>,
7676
pub sandbox_workspace_write: Option<SandboxWorkspaceWrite>,

src/cortex-otel/src/config.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! OpenTelemetry configuration.
22
3-
use serde::{Deserialize, Serialize};
3+
use serde::{Deserialize, Deserializer, Serialize};
44

55
/// OpenTelemetry settings.
66
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -30,14 +30,28 @@ pub struct OtelSettings {
3030
pub propagate_context: bool,
3131

3232
/// Sampling ratio (0.0 to 1.0).
33-
#[serde(default = "default_sampling_ratio")]
33+
#[serde(default = "default_sampling_ratio", deserialize_with = "deserialize_sampling_ratio")]
3434
pub sampling_ratio: f64,
3535

3636
/// Export timeout in seconds.
3737
#[serde(default = "default_export_timeout")]
3838
pub export_timeout_secs: u64,
3939
}
4040

41+
/// Deserialize sampling_ratio with validation (must be 0.0-1.0).
42+
fn deserialize_sampling_ratio<'de, D>(deserializer: D) -> Result<f64, D::Error>
43+
where
44+
D: Deserializer<'de>,
45+
{
46+
let value = f64::deserialize(deserializer)?;
47+
if !(0.0..=1.0).contains(&value) {
48+
return Err(serde::de::Error::custom(
49+
"sampling_ratio must be between 0.0 and 1.0",
50+
));
51+
}
52+
Ok(value)
53+
}
54+
4155
impl Default for OtelSettings {
4256
fn default() -> Self {
4357
OtelSettings {
@@ -84,7 +98,8 @@ impl OtelSettings {
8498
}
8599

86100
if let Ok(ratio) = std::env::var("OTEL_TRACES_SAMPLER_ARG")
87-
&& let Ok(ratio) = ratio.parse()
101+
&& let Ok(ratio) = ratio.parse::<f64>()
102+
&& (0.0..=1.0).contains(&ratio)
88103
{
89104
settings.sampling_ratio = ratio;
90105
}

src/cortex-protocol/src/protocol/event_payloads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct SessionConfiguredEvent {
4040

4141
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
4242
pub struct TaskStartedEvent {
43-
pub model_context_window: Option<i64>,
43+
pub model_context_window: Option<u64>,
4444
}
4545

4646
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]

src/cortex-protocol/src/protocol/tokens.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct TokenUsage {
1818
pub struct TokenUsageInfo {
1919
pub total_token_usage: TokenUsage,
2020
pub last_token_usage: TokenUsage,
21-
pub model_context_window: Option<i64>,
21+
pub model_context_window: Option<u64>,
2222
#[serde(default)]
2323
pub context_tokens: i64,
2424
}

0 commit comments

Comments
 (0)