返回 CodeWhale
subagent_limits.rs
根目录 / crates / tui / src / config / subagent_limits.rs
1 //! Sub-agent concurrency/timeout limits and their clamp resolvers.
2 //!
3 //! Pure numeric/string limit constants plus the two private clamp helpers that
4 //! operate solely on them. Extracted verbatim from `config.rs`; the constants
5 //! are re-exported via `pub use subagent_limits::*;` (preserving each item's
6 //! `pub`/`pub(crate)` visibility) and the resolvers are pulled back into
7 //! `config.rs` with a private `use`, so no new external surface is created
8 //! (#3311).
9
10 /// Temporary high-throughput default while the shared-context cutover makes
11 /// agent fanout cheap. This should eventually be governed by API/backpressure
12 /// budgets rather than memory-driven count throttles.
13 pub const DEFAULT_MAX_SUBAGENTS: usize = 64;
14 /// User-configurable ceiling for concurrent sub-agent execution. Keep this
15 /// above the default so operators can opt into larger API-bound fanout without
16 /// code changes while the full resource budget gate lands.
17 pub const MAX_SUBAGENTS: usize = 128;
18 /// Upper bound for queued + running sub-agent admissions. This is deliberately
19 /// higher than the instantaneous concurrency cap so Workflow-style fanout can
20 /// opt into large bounded populations without unbounded queue growth.
21 pub const MAX_SUBAGENT_ADMISSION: usize = 1024;
22 /// Default per-step DeepSeek API timeout for sub-agent requests, in seconds.
23 /// Raised from the legacy 120s: a live-but-slow reasoning call routinely
24 /// outlasts two minutes, and a timed-out attempt is now retried with backoff
25 /// before the step interrupts, so the default should only trip on genuinely
26 /// stuck calls (FINISH-0.9.4 entry #40). Applies when `[subagents]
27 /// api_timeout_secs` is unset (#1806, #1808).
28 pub const DEFAULT_SUBAGENT_API_TIMEOUT_SECS: u64 = 600;
29 /// Minimum accepted `[subagents] api_timeout_secs`. Anything lower (including
30 /// `0`, which would otherwise produce an immediate timeout footgun) clamps
31 /// up to this value before the runtime sees it.
32 pub const MIN_SUBAGENT_API_TIMEOUT_SECS: u64 = 1;
33 /// Maximum accepted `[subagents] api_timeout_secs` (60 minutes). The cap
34 /// keeps a misconfigured per-step timeout from masking real model/network
35 /// hangs forever.
36 pub const MAX_SUBAGENT_API_TIMEOUT_SECS: u64 = 3600;
37 /// Default wall-clock budget for a single sub-agent tool execution, in
38 /// seconds. This is the single source of truth for the default:
39 /// `tools::subagent::DEFAULT_TOOL_TIMEOUT` derives from it, so the heartbeat
40 /// floor below and the timeout actually applied to a running tool can never
41 /// drift apart.
42 pub const DEFAULT_SUBAGENT_TOOL_TIMEOUT_SECS: u64 = 300;
43 /// Default wall-clock interval without manager-visible sub-agent progress
44 /// before a running child can be auto-cancelled to release its slot (#2614).
45 pub const DEFAULT_SUBAGENT_HEARTBEAT_TIMEOUT_SECS: u64 = 300;
46 /// Minimum accepted `[subagents] heartbeat_timeout_secs`.
47 pub const MIN_SUBAGENT_HEARTBEAT_TIMEOUT_SECS: u64 = 30;
48 /// Maximum accepted `[subagents] heartbeat_timeout_secs` (1 hour).
49 pub const MAX_SUBAGENT_HEARTBEAT_TIMEOUT_SECS: u64 = 3600;
50 /// Default per-SSE-chunk idle timeout, in seconds.
51 pub const DEFAULT_STREAM_CHUNK_TIMEOUT_SECS: u64 = 900;
52 /// Minimum accepted stream chunk timeout.
53 pub const MIN_STREAM_CHUNK_TIMEOUT_SECS: u64 = 1;
54 /// Maximum accepted stream chunk timeout.
55 pub const MAX_STREAM_CHUNK_TIMEOUT_SECS: u64 = 3600;
56 /// Default TCP/TLS connect timeout for the model HTTP client, in seconds.
57 pub const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;
58 /// Minimum accepted connect timeout.
59 pub const MIN_CONNECT_TIMEOUT_SECS: u64 = 1;
60 /// Maximum accepted connect timeout.
61 pub const MAX_CONNECT_TIMEOUT_SECS: u64 = 300;
62 pub(crate) const STREAM_CHUNK_TIMEOUT_ENV: &str = "CODEWHALE_STREAM_IDLE_TIMEOUT_SECS";
63 /// Legacy alias for [`STREAM_CHUNK_TIMEOUT_ENV`].
64 pub(crate) const LEGACY_STREAM_CHUNK_TIMEOUT_ENV: &str = "DEEPSEEK_STREAM_IDLE_TIMEOUT_SECS";
65 /// R1: env override for `[tui].max_model_steps`.
66 pub(crate) const MAX_MODEL_STEPS_ENV: &str = "CODEWHALE_MAX_MODEL_STEPS";
67 /// R1: env override for `[tui].turn_wall_clock_secs`.
68 pub(crate) const TURN_WALL_CLOCK_ENV: &str = "CODEWHALE_TURN_WALL_CLOCK_SECS";
69
70 pub(crate) fn resolve_subagent_api_timeout_secs(raw: Option<u64>) -> u64 {
71 let raw = raw.unwrap_or(DEFAULT_SUBAGENT_API_TIMEOUT_SECS);
72 if raw == 0 {
73 return DEFAULT_SUBAGENT_API_TIMEOUT_SECS;
74 }
75 raw.clamp(MIN_SUBAGENT_API_TIMEOUT_SECS, MAX_SUBAGENT_API_TIMEOUT_SECS)
76 }
77
78 pub(crate) fn resolve_subagent_heartbeat_timeout_secs(
79 raw: Option<u64>,
80 api_timeout_secs: u64,
81 tool_timeout_secs: u64,
82 ) -> u64 {
83 let raw = raw.unwrap_or(DEFAULT_SUBAGENT_HEARTBEAT_TIMEOUT_SECS);
84 let configured = if raw == 0 {
85 DEFAULT_SUBAGENT_HEARTBEAT_TIMEOUT_SECS
86 } else {
87 raw.clamp(
88 MIN_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
89 MAX_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
90 )
91 };
92 let min_for_api = api_timeout_secs.saturating_add(30).clamp(
93 MIN_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
94 MAX_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
95 );
96 // A single tool execution may legitimately run up to `tool_timeout_secs`
97 // without the child touching its progress heartbeat (activity is recorded
98 // at step boundaries, not mid-tool), so the floor must also sit above the
99 // tool timeout. Deriving it from `api_timeout_secs` alone let a low
100 // `[subagents] api_timeout_secs` pull the floor under a long tool, and
101 // cleanup then killed a legitimately-working child (2026-08-04 sub-agent
102 // hunt, finding 4).
103 let min_for_tool = tool_timeout_secs.saturating_add(30).clamp(
104 MIN_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
105 MAX_SUBAGENT_HEARTBEAT_TIMEOUT_SECS,
106 );
107 configured.max(min_for_api).max(min_for_tool)
108 }
109
109 lines RUST