refactor: replace magic numbers with documented named constants#43
Closed
refactor: replace magic numbers with documented named constants#43
Conversation
Greptile OverviewGreptile SummaryThis PR refactors magic numbers into well-documented named constants, significantly improving code maintainability and readability. A new Key improvements:
Issue found:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| src/cortex-common/src/timeout.rs | New module centralizing timeout constants with comprehensive documentation and validation tests |
| src/cortex-exec/src/runner.rs | Replaced magic numbers with named constants (MAX_TURNS, TEMPERATURE, MAX_OUTPUT_TOKENS, RETRY_DELAY_BASE_MS) and imports timeout constants from cortex-common |
| src/cortex-engine/src/tools/handlers/batch.rs | Renamed DEFAULT_BATCH_TIMEOUT_SECS to DEFAULT_TOOL_TIMEOUT_SECS, added new tool_timeout_secs parameter, improved error messages |
Sequence Diagram
sequenceDiagram
participant User
participant ExecRunner
participant CommonModule as cortex-common
participant BatchHandler as BatchToolHandler
Note over CommonModule: New timeout.rs module<br/>with centralized constants
User->>ExecRunner: Initialize with options
ExecRunner->>CommonModule: Import DEFAULT_EXEC_TIMEOUT_SECS<br/>DEFAULT_REQUEST_TIMEOUT_SECS
Note over ExecRunner: Uses named constants:<br/>DEFAULT_MAX_TURNS = 10<br/>DEFAULT_TEMPERATURE = 0.7<br/>DEFAULT_MAX_OUTPUT_TOKENS = 4096<br/>RETRY_DELAY_BASE_MS = 500
User->>ExecRunner: run()
ExecRunner->>ExecRunner: Wrap execution with<br/>DEFAULT_EXEC_TIMEOUT_SECS (600s)
loop For each turn (up to DEFAULT_MAX_TURNS)
ExecRunner->>ExecRunner: send_request()<br/>with DEFAULT_TEMPERATURE<br/>and DEFAULT_MAX_OUTPUT_TOKENS
alt Request fails
ExecRunner->>ExecRunner: Retry with exponential backoff<br/>using RETRY_DELAY_BASE_MS
end
end
User->>BatchHandler: Execute batch with tools
BatchHandler->>CommonModule: Import DEFAULT_BATCH_TIMEOUT_SECS (300s)
Note over BatchHandler: New: DEFAULT_TOOL_TIMEOUT_SECS = 60s<br/>for individual tool timeouts
BatchHandler->>BatchHandler: execute_parallel()<br/>with tool_timeout (60s per tool)
Note over BatchHandler: Issue: timeout_secs parameter<br/>no longer used in implementation
Comment on lines
+340
to
+342
| // Determine per-tool timeout (prevents single tool from blocking others) | ||
| let tool_timeout_secs = args.tool_timeout_secs.unwrap_or(DEFAULT_TOOL_TIMEOUT_SECS); | ||
| let tool_timeout = Duration::from_secs(tool_timeout_secs); |
There was a problem hiding this comment.
timeout_secs parameter is still defined in BatchToolArgs and the tool definition but is no longer used in the implementation. Either remove it entirely (breaking change) or use it for an overall batch timeout wrapper around the parallel execution.
Suggested change
| // Determine per-tool timeout (prevents single tool from blocking others) | |
| let tool_timeout_secs = args.tool_timeout_secs.unwrap_or(DEFAULT_TOOL_TIMEOUT_SECS); | |
| let tool_timeout = Duration::from_secs(tool_timeout_secs); | |
| // Determine per-tool timeout (prevents single tool from blocking others) | |
| let tool_timeout_secs = args.tool_timeout_secs.unwrap_or(DEFAULT_TOOL_TIMEOUT_SECS); | |
| let tool_timeout = Duration::from_secs(tool_timeout_secs); | |
| // Optionally enforce overall batch timeout if specified | |
| let batch_timeout_secs = args.timeout_secs.unwrap_or(cortex_common::DEFAULT_BATCH_TIMEOUT_SECS); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-engine/src/tools/handlers/batch.rs
Line: 340:342
Comment:
`timeout_secs` parameter is still defined in `BatchToolArgs` and the tool definition but is no longer used in the implementation. Either remove it entirely (breaking change) or use it for an overall batch timeout wrapper around the parallel execution.
```suggestion
// Determine per-tool timeout (prevents single tool from blocking others)
let tool_timeout_secs = args.tool_timeout_secs.unwrap_or(DEFAULT_TOOL_TIMEOUT_SECS);
let tool_timeout = Duration::from_secs(tool_timeout_secs);
// Optionally enforce overall batch timeout if specified
let batch_timeout_secs = args.timeout_secs.unwrap_or(cortex_common::DEFAULT_BATCH_TIMEOUT_SECS);
```
How can I resolve this? If you propose a fix, please make it concise.
echobt
added a commit
that referenced
this pull request
Feb 4, 2026
…it, and documentation This PR consolidates the following refactoring changes: - #27: Centralize timeout constants - #43: Replace magic numbers with documented named constants - #67: Use LazyLock for static regex initialization - #68: Extract subagent timeout values as named constants Key changes: - Created centralized timeout module with documented constants - Replaced scattered magic numbers with well-documented constants - Improved static initialization using LazyLock for regexes - Added comprehensive documentation for timeout hierarchy
Contributor
Author
|
Consolidated into #77 - refactor: consolidated code quality improvements - constants, lazy init, and documentation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR replaces magic numbers scattered throughout cortex-exec/src/runner.rs with documented named constants. This improves code readability, maintainability, and makes it easier to tune configuration values.
Changes
10->DEFAULT_MAX_TURNS(default conversation turns limit)0.7->DEFAULT_TEMPERATURE(LLM temperature setting)4096->DEFAULT_MAX_OUTPUT_TOKENS(max output token limit)500->RETRY_DELAY_BASE_MS(base delay for exponential backoff)Verification
Both pass without errors.