Skip to content

refactor: consolidated code quality improvements - constants, lazy init, and documentation#77

Closed
echobt wants to merge 3 commits intomainfrom
consolidated/refactoring
Closed

refactor: consolidated code quality improvements - constants, lazy init, and documentation#77
echobt wants to merge 3 commits intomainfrom
consolidated/refactoring

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

This PR consolidates 4 refactoring PRs into a single, cohesive change.

Included PRs:

Key Changes:

  • Created centralized cortex_common::timeout module with documented constants
  • Replaced scattered magic numbers throughout codebase with well-documented constants
  • Improved static initialization using LazyLock for regex patterns
  • Added comprehensive documentation for the timeout hierarchy across Cortex services

Files Modified:

  • src/cortex-common/src/timeout.rs (new)
  • src/cortex-common/src/config_substitution.rs
  • src/cortex-app-server/src/auth.rs
  • src/cortex-app-server/src/streaming.rs
  • src/cortex-engine/src/tools/handlers/batch.rs
  • src/cortex-engine/src/tools/handlers/grep.rs
  • src/cortex-engine/src/tools/unified_executor.rs
  • src/cortex-exec/src/runner.rs
  • src/cortex-tui/src/runner/event_loop/subagent.rs
  • src/cortex-tui/src/runner/event_loop/streaming.rs

Closes #27, #43, #67, #68

…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
@greptile-apps
Copy link

greptile-apps bot commented Feb 4, 2026

Greptile Overview

Greptile Summary

This PR consolidates four refactoring efforts into a cohesive code quality improvement that centralizes timeout constants, replaces magic numbers, and optimizes static initialization patterns.

Key Improvements

  • Centralized timeout module: created cortex_common::timeout with 7 well-documented timeout constants that establish a clear hierarchy across the codebase
  • LazyLock optimization: migrated regex patterns in config_substitution.rs from per-instance compilation to static LazyLock initialization, improving performance and reducing memory overhead
  • Magic number elimination: replaced hardcoded timeout values across TUI streaming and subagent modules with named constants
  • Enhanced documentation: added comprehensive timeout hierarchy table in http_client.rs documenting the relationship between all timeout values
  • Code cleanup: removed unused fields (base_dir in SessionStorage, replace_all in SearchReplace) and dead code (reconnection methods in MCP transport)
  • Safer error handling: replaced unwrap() with unwrap_or_default() in time-related operations

Issues Found

  • Unused parameter in batch tool: timeout_secs parameter in BatchToolArgs is defined but never used after the refactor introduced per-tool timeouts via tool_timeout_secs. This creates API confusion and should be removed from both the struct and tool definition schema.

Confidence Score: 4/5

  • Safe to merge after addressing the unused timeout_secs parameter in batch tool
  • Excellent refactoring with clear improvements to code quality, maintainability, and performance. The LazyLock pattern is correctly implemented, timeout constants are well-documented with tests, and dead code removal is appropriate. However, the batch tool has an unused parameter that could confuse users and should be cleaned up before merge.
  • src/cortex-engine/src/tools/handlers/batch.rs requires cleanup to remove unused timeout_secs parameter

Important Files Changed

Filename Overview
src/cortex-common/src/timeout.rs new centralized timeout module with well-documented constants and unit tests
src/cortex-common/src/config_substitution.rs replaced instance regex with static LazyLock patterns for better performance
src/cortex-common/src/http_client.rs added comprehensive timeout hierarchy documentation table
src/cortex-engine/src/tools/handlers/batch.rs added tool_timeout_secs parameter for individual tool timeouts, but timeout_secs is now unused
src/cortex-tui/src/runner/event_loop/streaming.rs replaced magic numbers with named constants for streaming timeouts
src/cortex-tui/src/runner/event_loop/subagent.rs replaced magic numbers with named constants for subagent timeouts

Sequence Diagram

sequenceDiagram
    participant Client as Client/Tool Executor
    participant Batch as BatchToolHandler
    participant Tools as Individual Tools
    participant Timeout as timeout module
    
    Note over Client,Timeout: Centralized Timeout Architecture
    
    Client->>Timeout: Import timeout constants
    Timeout-->>Client: DEFAULT_BATCH_TIMEOUT_SECS (300s)
    
    Client->>Batch: execute(BatchToolArgs)
    Note over Batch: Parse tool_timeout_secs<br/>(default: 60s)
    
    par Parallel Tool Execution
        Batch->>Tools: execute_tool_1() with timeout
        Batch->>Tools: execute_tool_2() with timeout
        Batch->>Tools: execute_tool_N() with timeout
    end
    
    alt Tool completes in time
        Tools-->>Batch: Success result
    else Tool times out after 60s
        Tools-->>Batch: Timeout error
    end
    
    Batch-->>Client: BatchResult (aggregated)
    
    Note over Client,Timeout: LazyLock Pattern for Regex
    
    Client->>cortex_common: config_substitution
    activate cortex_common
    Note over cortex_common: ENV_REGEX & FILE_REGEX<br/>initialized once via LazyLock
    cortex_common-->>Client: Compiled patterns (shared)
    deactivate cortex_common
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines 46 to 48
/// Optional timeout in seconds for the entire batch (default: 300s).
#[serde(default)]
pub timeout_secs: Option<u64>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout_secs parameter is no longer used after switching to per-tool timeouts. The code now only uses tool_timeout_secs (line 341), making this parameter obsolete.

Suggested change
/// Optional timeout in seconds for the entire batch (default: 300s).
#[serde(default)]
pub timeout_secs: Option<u64>,
/// Optional timeout in seconds for individual tools (default: 60s).
/// This prevents a single tool from consuming the entire batch timeout.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-engine/src/tools/handlers/batch.rs
Line: 46:48

Comment:
`timeout_secs` parameter is no longer used after switching to per-tool timeouts. The code now only uses `tool_timeout_secs` (line 341), making this parameter obsolete.

```suggestion
    /// Optional timeout in seconds for individual tools (default: 60s).
    /// This prevents a single tool from consuming the entire batch timeout.
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps
Copy link

greptile-apps bot commented Feb 4, 2026

Additional Comments (1)

src/cortex-engine/src/tools/handlers/batch.rs
timeout_secs is no longer used in the code and should be removed from the tool definition schema to avoid confusion.

                "tool_timeout_secs": {
                    "type": "integer",
                    "description": "Optional timeout in seconds for individual tool execution (default: 60). Prevents a single tool from consuming the entire batch timeout.",
                    "minimum": 1,
                    "maximum": 300
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-engine/src/tools/handlers/batch.rs
Line: 391:396

Comment:
`timeout_secs` is no longer used in the code and should be removed from the tool definition schema to avoid confusion.

```suggestion
                "tool_timeout_secs": {
                    "type": "integer",
                    "description": "Optional timeout in seconds for individual tool execution (default: 60). Prevents a single tool from consuming the entire batch timeout.",
                    "minimum": 1,
                    "maximum": 300
```

How can I resolve this? If you propose a fix, please make it concise.

@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Closing this PR to consolidate into a single mega-PR combining all refactoring and documentation changes.

@echobt echobt closed this Feb 4, 2026
echobt added a commit that referenced this pull request Feb 4, 2026
This PR consolidates all refactoring and documentation improvements from PRs #77 and #89.

## Refactoring (from PR #77)

### Timeout Constants
- Created centralized cortex_common::timeout module with documented constants
- Replaced scattered magic numbers with well-documented constants
- Added comprehensive documentation for timeout hierarchy

### Code Quality
- Used LazyLock for static regex initialization
- Extracted subagent timeout values as named constants
- Improved compile-time assertions

## Documentation (from PR #89)

### Timeout Hierarchy Documentation
- Added detailed header documentation explaining timeout hierarchy
- Documented each timeout constant with its purpose and use case
- Established naming conventions for constants vs config fields
- Added timeout hierarchy table for quick reference

### Timeout Reference Table

| Use Case                    | Module                      | Constant                    | Value | Rationale |
|-----------------------------|-----------------------------|-----------------------------|-------|-----------|
| Health checks               | cortex-common/http_client   | HEALTH_CHECK_TIMEOUT        | 5s    | Quick validation |
| Standard HTTP requests      | cortex-common/http_client   | DEFAULT_TIMEOUT             | 30s   | Normal API calls |
| Connection pool idle        | cortex-common/http_client   | POOL_IDLE_TIMEOUT           | 60s   | DNS refresh |
| LLM Request (non-streaming) | cortex-exec/runner          | DEFAULT_REQUEST_TIMEOUT_SECS| 120s  | Model inference |
| LLM Streaming total         | cortex-common/http_client   | STREAMING_TIMEOUT           | 300s  | Long-running streams |

## Files Modified
- src/cortex-common/src/timeout.rs (new)
- src/cortex-common/src/http_client.rs
- src/cortex-app-server/src/config.rs
- src/cortex-exec/src/runner.rs
- Multiple other files

Closes #77, #89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant