agpm_cli/
constants.rs

1//! Global constants used throughout the AGPM codebase.
2//!
3//! This module contains timeout durations, retry parameters, and other
4//! numeric constants that are used across multiple modules. Defining
5//! them centrally improves maintainability and makes magic numbers
6//! more discoverable.
7
8use std::time::Duration;
9
10/// Default timeout for cache lock acquisition (120 seconds).
11///
12/// This timeout must be long enough to accommodate multiple sequential worktree
13/// operations that share the same lock (e.g., `bare-worktree-{owner}_{repo}`).
14/// On slow CI environments or when conflict resolution creates many worktrees,
15/// the lock may be held for extended periods. Set to 2× GIT_WORKTREE_TIMEOUT
16/// to allow for at least 2 sequential worktree creations.
17pub fn default_lock_timeout() -> Duration {
18    Duration::from_secs(120)
19}
20
21/// Legacy constant for backwards compatibility - prefer `default_lock_timeout()` function.
22pub const DEFAULT_LOCK_TIMEOUT: Duration = Duration::from_secs(120);
23
24/// Timeout for pending operations (10 seconds).
25pub fn pending_state_timeout() -> Duration {
26    Duration::from_secs(10)
27}
28
29/// Legacy constant for backwards compatibility - prefer `pending_state_timeout()` function.
30pub const PENDING_STATE_TIMEOUT: Duration = Duration::from_secs(10);
31
32/// Maximum backoff delay for exponential backoff.
33///
34/// On Windows, a shorter max backoff (200ms) with more retries reduces total wait time
35/// when antivirus scans cause brief file locking delays (typically 1-2 seconds).
36/// On Unix, 500ms allows for larger gaps between retries on slower systems.
37#[cfg(windows)]
38pub const MAX_BACKOFF_DELAY_MS: u64 = 200;
39
40/// Maximum backoff delay for exponential backoff (500ms).
41///
42/// Exponential backoff delays are capped at this value to prevent
43/// excessive wait times during retry operations.
44#[cfg(not(windows))]
45pub const MAX_BACKOFF_DELAY_MS: u64 = 500;
46
47/// Starting delay for exponential backoff.
48///
49/// On Windows, start with a slightly longer delay (25ms) to give antivirus
50/// scanners time to release files before the first retry attempt.
51#[cfg(windows)]
52pub const STARTING_BACKOFF_DELAY_MS: u64 = 25;
53
54/// Starting delay for exponential backoff (10ms).
55///
56/// This is the initial delay used in exponential backoff calculations,
57/// which doubles on each retry attempt.
58#[cfg(not(windows))]
59pub const STARTING_BACKOFF_DELAY_MS: u64 = 10;
60
61/// Timeout for Git fetch operations (60 seconds).
62///
63/// This timeout prevents hung network connections from blocking
64/// worktree creation indefinitely.
65pub const GIT_FETCH_TIMEOUT: Duration = Duration::from_secs(60);
66
67/// Timeout for Git clone operations (120 seconds).
68///
69/// Clone operations may take longer than fetch, especially
70/// for large repositories.
71pub const GIT_CLONE_TIMEOUT: Duration = Duration::from_secs(120);
72
73/// Timeout for Git worktree creation (60 seconds).
74///
75/// Creating a worktree involves checking out files which
76/// can take time for large repositories.
77pub const GIT_WORKTREE_TIMEOUT: Duration = Duration::from_secs(60);
78
79/// Timeout for batch operations using `join_all` (5 minutes).
80///
81/// This prevents indefinite blocking when batch futures hang.
82pub fn batch_operation_timeout() -> Duration {
83    Duration::from_secs(300)
84}
85
86/// Minimum number of parallel operations regardless of CPU count.
87///
88/// This ensures reasonable parallelism even on single-core machines.
89/// On Windows, a lower value (4) reduces lock contention and AV interference.
90/// On Unix, the value of 10 provides good throughput for I/O-bound Git operations.
91#[cfg(windows)]
92pub const MIN_PARALLELISM: usize = 4;
93
94/// Minimum number of parallel operations regardless of CPU count.
95///
96/// This ensures reasonable parallelism even on single-core machines.
97/// The value of 10 provides good throughput for I/O-bound Git operations.
98#[cfg(not(windows))]
99pub const MIN_PARALLELISM: usize = 10;
100
101/// Multiplier applied to CPU core count for default parallelism.
102///
103/// On Windows, a lower multiplier (1x cores) reduces lock contention, context
104/// switching overhead, and antivirus interference from scanning parallel operations.
105#[cfg(windows)]
106pub const PARALLELISM_CORE_MULTIPLIER: usize = 1;
107
108/// Multiplier applied to CPU core count for default parallelism.
109///
110/// Higher values increase throughput but may strain resources or hit rate limits.
111/// The value of 2 balances throughput with system stability.
112#[cfg(not(windows))]
113pub const PARALLELISM_CORE_MULTIPLIER: usize = 2;
114
115/// Default CPU core count when detection fails.
116///
117/// Used as a fallback when `std::thread::available_parallelism()` returns an error.
118pub const FALLBACK_CORE_COUNT: usize = 4;