Skip to content

Commit a22a368

Browse files
committed
refactor: centralize timeout constants into cortex-common module
1 parent d201070 commit a22a368

File tree

7 files changed

+81
-14
lines changed

7 files changed

+81
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cortex-common/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod signal_safety;
1818
pub mod subprocess_env;
1919
pub mod subprocess_output;
2020
pub mod text_sanitize;
21+
pub mod timeout;
2122
pub mod truncate;
2223

2324
#[cfg(feature = "cli")]
@@ -73,6 +74,11 @@ pub use subprocess_output::{
7374
pub use text_sanitize::{
7475
has_control_chars, normalize_code_fences, sanitize_control_chars, sanitize_for_terminal,
7576
};
77+
pub use timeout::{
78+
DEFAULT_BATCH_TIMEOUT_SECS, DEFAULT_EXEC_TIMEOUT_SECS, DEFAULT_HEALTH_CHECK_TIMEOUT_SECS,
79+
DEFAULT_READ_TIMEOUT_SECS, DEFAULT_REQUEST_TIMEOUT_SECS, DEFAULT_SHUTDOWN_TIMEOUT_SECS,
80+
DEFAULT_STREAMING_TIMEOUT_SECS,
81+
};
7682
pub use truncate::{
7783
truncate_command, truncate_first_line, truncate_for_display, truncate_id, truncate_id_default,
7884
truncate_model_name, truncate_with_ellipsis, truncate_with_unicode_ellipsis,

src/cortex-common/src/timeout.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Centralized timeout constants for the Cortex CLI.
2+
//!
3+
//! This module provides consistent timeout values used throughout the codebase.
4+
//! Centralizing these values ensures uniformity and makes it easier to adjust
5+
//! timeouts across the application.
6+
7+
/// Default timeout for the entire execution in seconds (10 minutes).
8+
///
9+
/// This is the maximum time allowed for a complete headless execution,
10+
/// including all LLM requests and tool executions.
11+
pub const DEFAULT_EXEC_TIMEOUT_SECS: u64 = 600;
12+
13+
/// Default timeout for a single LLM request in seconds (2 minutes).
14+
///
15+
/// This is the maximum time to wait for a single completion request
16+
/// to the LLM provider.
17+
pub const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 120;
18+
19+
/// Default timeout for streaming responses in seconds (5 minutes).
20+
///
21+
/// Extended timeout for LLM streaming requests where responses are
22+
/// delivered incrementally over time.
23+
pub const DEFAULT_STREAMING_TIMEOUT_SECS: u64 = 300;
24+
25+
/// Default timeout for health check requests in seconds (5 seconds).
26+
///
27+
/// Short timeout used for quick health check endpoints.
28+
pub const DEFAULT_HEALTH_CHECK_TIMEOUT_SECS: u64 = 5;
29+
30+
/// Default timeout for graceful shutdown in seconds (30 seconds).
31+
///
32+
/// Maximum time to wait for in-flight operations to complete during
33+
/// shutdown before forcing termination.
34+
pub const DEFAULT_SHUTDOWN_TIMEOUT_SECS: u64 = 30;
35+
36+
/// Default timeout for batch execution in seconds (5 minutes).
37+
///
38+
/// Maximum time allowed for executing a batch of parallel tool calls.
39+
pub const DEFAULT_BATCH_TIMEOUT_SECS: u64 = 300;
40+
41+
/// Default timeout for individual read operations in seconds (30 seconds).
42+
///
43+
/// Timeout for individual read operations to prevent hangs when
44+
/// Content-Length doesn't match actual body size.
45+
pub const DEFAULT_READ_TIMEOUT_SECS: u64 = 30;
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn test_timeout_values_are_reasonable() {
53+
// Exec timeout should be greater than request timeout
54+
assert!(DEFAULT_EXEC_TIMEOUT_SECS > DEFAULT_REQUEST_TIMEOUT_SECS);
55+
56+
// Streaming timeout should be greater than request timeout
57+
assert!(DEFAULT_STREAMING_TIMEOUT_SECS > DEFAULT_REQUEST_TIMEOUT_SECS);
58+
59+
// Health check should be short
60+
assert!(DEFAULT_HEALTH_CHECK_TIMEOUT_SECS <= 10);
61+
62+
// Batch timeout should be reasonable
63+
assert!(DEFAULT_BATCH_TIMEOUT_SECS >= 60);
64+
}
65+
}

src/cortex-engine/src/tools/handlers/batch.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::Arc;
77
use std::time::{Duration, Instant};
88

99
use async_trait::async_trait;
10+
use cortex_common::DEFAULT_BATCH_TIMEOUT_SECS;
1011
use futures::future::join_all;
1112
use serde::{Deserialize, Serialize};
1213
use serde_json::{Value, json};
@@ -20,9 +21,6 @@ use crate::tools::spec::{ToolDefinition, ToolHandler, ToolResult};
2021
/// Maximum number of tools that can be executed in a batch.
2122
pub const MAX_BATCH_SIZE: usize = 10;
2223

23-
/// Default timeout for batch execution in seconds.
24-
pub const DEFAULT_BATCH_TIMEOUT_SECS: u64 = 300;
25-
2624
/// Tools that cannot be called within a batch (prevent recursion).
2725
/// Note: Task is now allowed to enable parallel task execution.
2826
pub const DISALLOWED_TOOLS: &[&str] = &["Batch", "batch", "Agent", "agent"];

src/cortex-engine/src/tools/handlers/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ mod web_search;
2323
pub use apply_patch::ApplyPatchHandler;
2424
pub use batch::{
2525
BatchCallResult, BatchParams, BatchResult, BatchToolArgs, BatchToolCall, BatchToolExecutor,
26-
BatchToolHandler, DEFAULT_BATCH_TIMEOUT_SECS, DISALLOWED_TOOLS, LegacyBatchToolCall,
27-
MAX_BATCH_SIZE, batch_tool_definition, execute_batch,
26+
BatchToolHandler, DISALLOWED_TOOLS, LegacyBatchToolCall, MAX_BATCH_SIZE, batch_tool_definition,
27+
execute_batch,
2828
};
29+
pub use cortex_common::DEFAULT_BATCH_TIMEOUT_SECS;
2930
pub use create_agent::CreateAgentHandler;
3031
pub use edit_file::PatchHandler;
3132

src/cortex-exec/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ path = "src/lib.rs"
1313
workspace = true
1414

1515
[dependencies]
16+
cortex-common = { workspace = true }
1617
cortex-engine = { workspace = true }
1718
cortex-protocol = { workspace = true }
1819
tokio = { workspace = true, features = ["full"] }

src/cortex-exec/src/runner.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::time::Duration;
1414

1515
use tokio_stream::StreamExt;
1616

17+
use cortex_common::{DEFAULT_EXEC_TIMEOUT_SECS, DEFAULT_REQUEST_TIMEOUT_SECS};
1718
use cortex_engine::{
1819
Config, ConversationManager, CortexError,
1920
client::{
@@ -26,12 +27,6 @@ use cortex_protocol::ConversationId;
2627

2728
use crate::output::{OutputFormat, OutputWriter};
2829

29-
/// Default timeout for the entire execution (10 minutes).
30-
const DEFAULT_TIMEOUT_SECS: u64 = 600;
31-
32-
/// Default timeout for a single LLM request (2 minutes).
33-
const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 120;
34-
3530
/// Maximum retries for transient errors.
3631
const MAX_RETRIES: usize = 3;
3732

@@ -75,7 +70,7 @@ impl Default for ExecOptions {
7570
output_format: OutputFormat::Text,
7671
full_auto: false,
7772
max_turns: Some(10),
78-
timeout_secs: Some(DEFAULT_TIMEOUT_SECS),
73+
timeout_secs: Some(DEFAULT_EXEC_TIMEOUT_SECS),
7974
request_timeout_secs: Some(DEFAULT_REQUEST_TIMEOUT_SECS),
8075
sandbox: true,
8176
system_prompt: None,
@@ -267,7 +262,7 @@ impl ExecRunner {
267262
/// Run the execution with full timeout enforcement.
268263
pub async fn run(&mut self) -> Result<ExecResult, CortexError> {
269264
let timeout =
270-
Duration::from_secs(self.options.timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS));
265+
Duration::from_secs(self.options.timeout_secs.unwrap_or(DEFAULT_EXEC_TIMEOUT_SECS));
271266

272267
// Wrap the entire execution in a timeout
273268
match tokio::time::timeout(timeout, self.run_inner()).await {
@@ -766,7 +761,7 @@ mod tests {
766761
assert!(opts.prompt.is_empty());
767762
assert!(opts.sandbox);
768763
assert_eq!(opts.max_turns, Some(10));
769-
assert_eq!(opts.timeout_secs, Some(DEFAULT_TIMEOUT_SECS));
764+
assert_eq!(opts.timeout_secs, Some(DEFAULT_EXEC_TIMEOUT_SECS));
770765
assert!(!opts.full_auto);
771766
assert!(opts.streaming);
772767
}

0 commit comments

Comments
 (0)