Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 367 additions & 0 deletions code_quality_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,367 @@
# Code Quality Analysis Report

**Generated:** 2026-10-12
**Codebase:** OpenHands CLI (`openhands_cli/`)
**Files Analyzed:** 248 Python files (~19,400 lines)

---

## Executive Summary

The OpenHands CLI codebase demonstrates **good overall quality** with well-structured module boundaries and proper use of modern typing constructs. Pyright reports **0 errors and 0 warnings**, indicating strong type safety.

**Key Findings:**
- ✅ **Type Safety**: Excellent - no pyright errors, good use of type hints throughout
- ⚠️ **State Management**: Minor issues - module-level globals and intentional prop drilling pattern
- ⚠️ **Separation of Concerns**: Some mixing of UI/presentation with business logic in auth module
- ⚠️ **Code Duplication**: Action title formatting and confirmation handling duplicated between ACP and TUI

**Top Priorities:**
1. Extract UI code from `auth/api_client.py` into a presenter layer
2. Move action title formatting logic to shared module
3. Remove module-level `AgentStore` singleton in `tui/modals/settings/utils.py`

---

## Type Checking Issues

### Critical Issues
**Status: ✅ None Found**

Pyright output shows 0 errors across 248 files analyzed. The codebase passes strict type checking.

### Missing Type Hints

**Status: ⚠️ Minor Opportunities**

The codebase has comprehensive type hints. A few areas use `Any` that could potentially be more specific:

| File | Line | Code | Notes |
|------|------|------|-------|
| `acp_impl/agent/base_agent.py` | 227-229 | `client_capabilities: Any`, `client_info: Any` | ACP protocol parameters - acceptable as external API |
| `acp_impl/events/token_streamer.py` | 242 | `tool_call: Any` | Streaming response parsing - dynamic structure |
| `tui/core/state.py` | 308 | `value: Any` | Generic state update method - by design |

### Type Improvement Opportunities

| Category | Location | Current | Suggested |
|----------|----------|---------|-----------|
| API Response | `auth/api_client.py:89` | `dict[str, Any]` | Create `UserInfo` TypedDict |
| API Response | `auth/api_client.py:105` | `dict[str, Any]` | Create `UserSettings` TypedDict |
| Config Status | `mcp/mcp_utils.py:375` | `dict[str, Any]` | Create `MCPConfigStatus` TypedDict |
| Test Utils | `acp_impl/test_utils.py:39` | `dict[str, Any] | None` | Create `JsonRpcMessage` TypedDict |

### Type Ignore Comments
Only 2 `# type: ignore` comments exist, both justified:
- `tui/utils/critic/feedback.py:14` - Optional Posthog import
- `tui/widgets/collapsible.py:330` - Textual query type narrowing

---

## State Management Issues

### Current State

The codebase uses a **well-designed reactive state pattern** for the TUI:

```
ConversationContainer (central reactive state)
├── running, conversation_id, metrics, etc.
└── Used by Controllers via dependency injection
```

**Stores directory** (`openhands_cli/stores/`) uses proper patterns:
- ✅ Pydantic `BaseModel` for settings (`CliSettings`, `LLMEnvOverrides`)
- ✅ `TypedDict` for typed dictionaries (`PromptHistoryEntry`)
- ✅ `dataclass` for data containers (`ConversationMetadata`)
- ✅ `NamedTuple` for immutable data (`VersionInfo`)

### Problems Identified

| Issue | File | Line | Severity |
|-------|------|------|----------|
| Module-level `AgentStore` singleton | `tui/modals/settings/utils.py` | 14 | **Medium** |
| Module-level `Console` instances | Multiple (7 files) | Various | Low |
| Module-level computed value | `tui/modals/settings/choices.py` | 62 | Low |

**Module-Level AgentStore (Medium Priority):**
```python
# tui/modals/settings/utils.py:14
agent_store = AgentStore() # Creates global singleton at import time
```
This creates implicit global state. The store should be passed as a parameter or created on demand.

**Multiple Console Instances (Low Priority):**
| File | Line |
|------|------|
| `entrypoint.py` | 27 |
| `gui_launcher.py` | 15 |
| `stores/agent_store.py` | 43-44 |
| `mcp/mcp_commands.py` | 26 |
| `conversations/viewer.py` | 12 |
| `conversations/display.py` | 11 |
| `auth/utils.py` | 16 |

### Recommendations

1. **Remove module-level AgentStore** - Pass as parameter or use factory function
2. **Consider centralizing Console** - Create `console.py` utility (optional, low impact)
3. **Document prop drilling pattern** - The `ConversationContainer` being passed to all controllers is intentional for Textual's reactive system

---

## Separation of Concerns Issues

### Module Boundaries
**Status: ✅ Good**

The three core modules have **clear boundaries** with no direct cross-dependencies:
- `acp_impl/` does NOT import from `tui/`
- `tui/` does NOT import from `acp_impl/`
- `auth/` does NOT import from `tui/`

### Coupled Components

| Component Pair | Issue | Severity |
|----------------|-------|----------|
| `setup.py` → `tui/widgets/richlog_visualizer.py` | Core module imports from UI layer | **Medium** |
| `auth/api_client.py` ↔ Console output | Business logic mixed with presentation | **High** |
| `ConversationVisualizer` → `OpenHandsApp` | Direct app reference with 4+ property accesses | Low |

### Mixed Responsibilities

**auth/api_client.py (High Priority):**
Mixes API client logic with console presentation:

| Lines | Function | Issue |
|-------|----------|-------|
| 152-176 | `_print_settings_summary()` | Console printing in API client |
| 178-227 | `_ask_user_consent_for_overwrite()` | Interactive prompts in API client |
| 229-289 | `create_and_save_agent_configuration()` | Persistence + console output |
| 291-360 | `fetch_user_data_after_oauth()` | API calls + console + config |

**Large Classes (SRP Violations):**

| Class | File | Lines | Responsibilities |
|-------|------|-------|------------------|
| `OpenHandsApp` | `tui/textual_app.py` | 756 | Screen management, key bindings, state init, clipboard, panels, exit flow |
| `ConversationVisualizer` | `tui/widgets/richlog_visualizer.py` | 836 | Event visualization, critic handling, telemetry, plan refresh |
| `BaseOpenHandsACPAgent` | `acp_impl/agent/base_agent.py` | 660 | Protocol implementation, session management, slash commands, OAuth |
| `AgentStore` | `stores/agent_store.py` | 532 | Loading, saving, env vars, critic config, MCP config, tools |

### Recommendations

1. **Extract presenter from auth/api_client.py:**
- Create `auth/presenter.py` with `print_settings_summary()`, `ask_user_consent()`
- Pass presenter as dependency or use callback pattern

2. **Break down large classes:**
- Extract `OpenHandsApp.clipboard_operations` → `ClipboardMixin`
- Extract `AgentStore.critic_config` → `CriticConfigStore`

3. **Remove setup.py → tui dependency:**
- Move `ConversationVisualizer` reference out of `setup.py`
- Use protocol/interface instead of concrete import

---

## Code Duplication (ACP/TUI)

### Already Shared ✅
The `openhands_cli/shared/` module already extracts common code:
- `slash_commands.py` - `parse_slash_command()` function
- `conversation_summary.py` - `extract_conversation_summary()` function
- `delegate_formatter.py` - `format_delegate_title()` function
- `rich_utils.py` - `escape_rich_markup()` function

### Duplicated Patterns

#### 1. Action Title Building Logic (High Priority)

**ACP (`acp_impl/events/utils.py:168-209`):**
```python
def get_tool_title(tool_name: str, *, action: Action | None = None, summary: str | None = None) -> str:
if isinstance(action, FileEditorAction):
op = "Reading" if action.command == "view" else "Editing"
path = action.path or ""
return f"{clean_summary}: {op} {path}" if clean_summary else f"{op} {path}"
if isinstance(action, TerminalAction):
cmd = action.command.strip().replace("\n", " ") if action.command else ""
return f"{clean_summary}: $ {cmd}" if clean_summary else f"$ {cmd}"
```

**TUI (`tui/widgets/richlog_visualizer.py:468-512`):**
```python
def _build_action_title(self, event: ActionEvent) -> str:
if isinstance(action, TerminalAction) and action.command:
cmd = escape_rich_markup(action.command.strip().replace("\n", " "))
return f"{agent_prefix}[bold]{summary}[/bold][dim]: $ {cmd}[/dim]"
elif isinstance(action, FileEditorAction) and action.path:
op = "Reading" if action.command == "view" else "Editing"
return f"{agent_prefix}[bold]{summary}[/bold][dim]: {op} {path}[/dim]"
```

**Similarity:** ~80% identical logic. Same patterns for `FileEditorAction`, `TerminalAction`, `DelegateAction`.

#### 2. Confirmation Decision Handling (High Priority)

**ACP (`acp_impl/runner.py:113-138`):**
```python
if decision == UserConfirmation.REJECT:
conversation.reject_pending_actions(result.reason or "User rejected the actions")
if decision == UserConfirmation.DEFER:
conversation.pause()
if isinstance(result.policy_change, NeverConfirm):
conversation.set_confirmation_policy(NeverConfirm())
```

**TUI (`tui/core/confirmation_flow_controller.py:38-48`):**
```python
if decision == UserConfirmation.ALWAYS_PROCEED:
self._policy_service.set_policy(NeverConfirm())
elif decision == UserConfirmation.CONFIRM_RISKY:
self._policy_service.set_policy(ConfirmRisky())
```

**Similarity:** ~70% identical - same mapping of decisions to policy changes.

#### 3. Tool Kind Mapping (Internal ACP Duplication)

**`acp_impl/events/utils.py:146-165`** and **`acp_impl/events/tool_state.py:95-114`** contain nearly identical tool kind computation logic.

### Shared Code Opportunities

| Pattern | Current Locations | Proposed Shared Location | Effort |
|---------|-------------------|-------------------------|--------|
| Action title core logic | `events/utils.py`, `richlog_visualizer.py` | `shared/action_title.py` | Medium |
| Confirmation decision mapping | `runner.py`, `confirmation_flow_controller.py` | `shared/confirmation_actions.py` | Small |
| Tool kind computation | `events/utils.py`, `tool_state.py` | `shared/tool_kind.py` | Small |
| String constants ("Reading", "Editing", "$ ") | Multiple files | `shared/constants.py` | Trivial |

### Recommendations

1. **Create `shared/action_title.py`:**
```python
def build_action_title_parts(action: Action, summary: str | None) -> tuple[str, str]:
"""Returns (prefix, detail) for action title. UI layers add formatting."""
```

2. **Create `shared/confirmation_actions.py`:**
```python
def apply_confirmation_decision(conversation, decision, reason="User rejected"):
"""Apply REJECT/DEFER decision to conversation."""

def get_policy_for_decision(decision) -> ConfirmationPolicyBase | None:
"""Map ALWAYS_PROCEED/CONFIRM_RISKY to policy classes."""
```

---

## Low-Hanging Fruit

Easy fixes that can be addressed quickly:

| Issue | File | Line | Effort | Impact |
|-------|------|------|--------|--------|
| Remove module-level `agent_store` | `tui/modals/settings/utils.py` | 14 | **Trivial** | Medium |
| Extract string constants | Multiple | - | **Trivial** | Low |
| Add TypedDict for `UserSettings` | `auth/api_client.py` | 105 | **Small** | Low |
| Add TypedDict for `UserInfo` | `auth/api_client.py` | 89 | **Small** | Low |
| Consolidate tool kind logic | `acp_impl/events/` | - | **Small** | Low |
| Extract presenter from api_client | `auth/api_client.py` | 152-360 | **Medium** | High |
| Create shared action title module | `shared/action_title.py` | - | **Medium** | Medium |

---

## Statistics

### Issues by Category

| Category | Critical | High | Medium | Low | Total |
|----------|----------|------|--------|-----|-------|
| Type Checking | 0 | 0 | 4 | 2 | 6 |
| State Management | 0 | 0 | 1 | 8 | 9 |
| Separation of Concerns | 0 | 1 | 2 | 3 | 6 |
| Code Duplication | 0 | 2 | 1 | 2 | 5 |
| **Total** | **0** | **3** | **8** | **15** | **26** |

### Codebase Metrics

| Metric | Value |
|--------|-------|
| Total Python files | 248 |
| Total lines (openhands_cli/) | ~19,400 |
| Pyright errors | 0 |
| Pyright warnings | 0 |
| Type ignore comments | 2 |
| Cast usages | 9 |
| `dict[str, Any]` usages | 30+ |

### Largest Files (Lines)

| File | Lines |
|------|-------|
| `tui/widgets/richlog_visualizer.py` | 836 |
| `tui/textual_app.py` | 756 |
| `acp_impl/agent/base_agent.py` | 660 |
| `tui/modals/settings/settings_screen.py` | 606 |
| `stores/agent_store.py` | 532 |
| `tui/widgets/user_input/input_field.py` | 505 |

---

## Prioritized Action Items

### High Priority (Should Address Soon)

1. **Extract UI from auth/api_client.py** *(Medium effort, High impact)*
- Create `auth/presenter.py` with `print_settings_summary()`, `ask_user_consent()`
- Improves testability and separation of concerns

2. **Create shared/action_title.py** *(Medium effort, Medium impact)*
- Extract common action title logic from ACP and TUI
- Reduces ~80 lines of duplicated code

3. **Remove module-level AgentStore singleton** *(Trivial effort, Medium impact)*
- In `tui/modals/settings/utils.py:14`
- Pass as parameter or use factory pattern

### Medium Priority (Address When Convenient)

4. **Create shared/confirmation_actions.py** *(Small effort, Medium impact)*
- Extract confirmation decision handling logic
- Reduces duplication between ACP runner and TUI controller

5. **Add TypedDict for API responses** *(Small effort, Low impact)*
- `UserInfo`, `UserSettings`, `MCPConfigStatus`
- Improves type safety for external API responses

6. **Consolidate tool kind logic** *(Small effort, Low impact)*
- Merge `events/utils.py:146-165` with `events/tool_state.py:95-114`

### Low Priority (Nice to Have)

7. **Consider centralizing Console instances** *(Trivial effort, Very Low impact)*
- Optional: 7 modules create their own Console
- May not be worth the refactoring effort

8. **Document prop drilling pattern** *(Trivial effort, Documentation impact)*
- Add note to AGENTS.md explaining `ConversationContainer` dependency injection
- Already noted in TUI State Management Architecture section

9. **Break down large classes** *(Medium-Large effort, Medium impact)*
- `OpenHandsApp`: Extract clipboard operations, panel management
- `AgentStore`: Extract critic/MCP config to separate stores
- Consider only if maintenance becomes problematic

---

## Summary

The OpenHands CLI codebase is **well-maintained with good architecture**. The main areas for improvement are:

1. **Separation of Concerns** in the auth module (mixing UI with business logic)
2. **Code Duplication** between ACP and TUI implementations (action titles, confirmation handling)
3. **Minor State Management** issues (module-level globals)

The pyright type checking passes completely, and the module boundaries are clear. The recommended refactorings would improve testability, reduce maintenance burden, and make the codebase more consistent.
2 changes: 1 addition & 1 deletion openhands_cli/tui/modals/settings/settings_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def _save_settings(self) -> None:
# (validator returned None)
if form_data.timeout is None and self.current_agent:
form_data.timeout = getattr(self.current_agent.llm, "timeout", None)
result = save_settings(form_data, self.current_agent)
result = save_settings(form_data, self.current_agent, self.agent_store)
if not result.success:
self._show_message(result.error_message or "Unknown error", is_error=True)
return
Expand Down
Loading