Skip to main content

calculate_safe_concurrency

Function calculate_safe_concurrency 

Source
pub fn calculate_safe_concurrency() -> usize
Expand description

Acquires a concurrency slot from the max_concurrency-position semaphore.

Iterates slots 1..=max_concurrency attempting try_lock_exclusive on each cli-slot-N.lock file. When a free slot is found, returns (File, slot_number). If all slots are occupied:

  • If wait_seconds is None or Some(0), returns immediately with AppError::AllSlotsFull { max, waited_secs: 0 }.
  • If wait_seconds is Some(n) > 0, enters a polling loop every

Returns the maximum number of parallel CLI instances the host can sustain without thrashing. The formula:

safe = min(cpus, available_mb / per_worker_mb) * 1.0

replaces the previous ... * 0.5 halving factor. The * 0.5 was the root cause of G18: even on a 64 GB host the result was always clamped to 4 because of the division-by-2.

The per-worker cost is the lower of EMBEDDING_LOAD_EXPECTED_RSS_MB (1100) and LLM_WORKER_RSS_MB (350), so LLM-only builds get a proportionally higher parallelism budget without changing the API.

Returns 1 as a defensive floor when system stats are unavailable.