diff --git a/code_quality_report.md b/code_quality_report.md new file mode 100644 index 00000000..ea0ed634 --- /dev/null +++ b/code_quality_report.md @@ -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. diff --git a/openhands_cli/tui/modals/settings/settings_screen.py b/openhands_cli/tui/modals/settings/settings_screen.py index 5609e15b..f6d66337 100644 --- a/openhands_cli/tui/modals/settings/settings_screen.py +++ b/openhands_cli/tui/modals/settings/settings_screen.py @@ -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 diff --git a/openhands_cli/tui/modals/settings/utils.py b/openhands_cli/tui/modals/settings/utils.py index 5fce4775..0c0255f1 100644 --- a/openhands_cli/tui/modals/settings/utils.py +++ b/openhands_cli/tui/modals/settings/utils.py @@ -11,9 +11,6 @@ ) -agent_store = AgentStore() - - class SettingsFormData(BaseModel): """Raw values captured from the SettingsScreen UI.""" @@ -174,9 +171,13 @@ class SettingsSaveResult(BaseModel): def save_settings( - data: SettingsFormData, existing_agent: Agent | None + data: SettingsFormData, + existing_agent: Agent | None, + agent_store: AgentStore | None = None, ) -> SettingsSaveResult: try: + if agent_store is None: + agent_store = AgentStore() data.resolve_data_fields(existing_agent) extra_kwargs: dict[str, Any] = {} diff --git a/tests/tui/modals/settings/test_settings_utils.py b/tests/tui/modals/settings/test_settings_utils.py index 4cac2ab8..043514b6 100644 --- a/tests/tui/modals/settings/test_settings_utils.py +++ b/tests/tui/modals/settings/test_settings_utils.py @@ -27,9 +27,6 @@ def deps(monkeypatch) -> FakeAgentStore: """ fake_store = FakeAgentStore() - # Use our fake store instead of the real AgentStore singleton - monkeypatch.setattr(settings_utils, "agent_store", fake_store) - # When utils wants a default CLI agent, build a minimal real Agent from the LLM def _mock_get_default_cli_agent(llm: LLM) -> Agent: return Agent(llm=llm) @@ -96,7 +93,7 @@ def test_preserves_existing_api_key_when_not_provided( assert data.api_key_input == existing_key_str # Now run through save_settings with that existing agent - result = settings_utils.save_settings(data, existing_agent) + result = settings_utils.save_settings(data, existing_agent, deps) assert result.success is True assert deps.saved_agents, "Expected agent_store.save to have been called" @@ -213,7 +210,7 @@ def test_mode_specific_fields_cleared_and_not_saved( assert data.get_full_model_name() == expected_model # Run the full save and assert we built the LLM correctly - result = settings_utils.save_settings(data, existing_agent=None) + result = settings_utils.save_settings(data, existing_agent=None, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] assert saved_agent.llm.model == expected_model @@ -243,7 +240,7 @@ def capture_llm_kwargs(**kwargs): memory_condensation_enabled=True, ) - result = settings_utils.save_settings(data, existing_agent=None) + result = settings_utils.save_settings(data, existing_agent=None, agent_store=deps) assert result.success is True assert captured_llm_kwargs[0]["model"] == "openhands/claude-opus-4-8" @@ -286,7 +283,7 @@ def test_save_settings_wraps_errors_into_result(deps: FakeAgentStore) -> None: memory_condensation_enabled=True, ) - result = settings_utils.save_settings(data, existing_agent=None) + result = settings_utils.save_settings(data, existing_agent=None, agent_store=deps) assert result.success is False # The error message should mention missing custom model/base url assert "Custom model is required" in (result.error_message or "") @@ -316,7 +313,7 @@ def test_memory_condensation_toggle(deps: FakeAgentStore, enabled: bool) -> None memory_condensation_enabled=enabled, ) - result = settings_utils.save_settings(data, existing_agent=existing_agent) + result = settings_utils.save_settings(data, existing_agent=existing_agent, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] @@ -350,7 +347,7 @@ def test_existing_condenser_llm_updated(deps: FakeAgentStore) -> None: memory_condensation_enabled=True, ) - result = settings_utils.save_settings(data, existing_agent=existing_agent) + result = settings_utils.save_settings(data, existing_agent=existing_agent, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] @@ -398,7 +395,7 @@ def test_litellm_metadata_is_added_when_required( memory_condensation_enabled=True, ) - result = settings_utils.save_settings(data, existing_agent=None) + result = settings_utils.save_settings(data, existing_agent=None, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] @@ -445,7 +442,7 @@ def spy_llm(*args, **kwargs): memory_condensation_enabled=False, ) - result = settings_utils.save_settings(data, existing_agent=existing_agent) + result = settings_utils.save_settings(data, existing_agent=existing_agent, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] @@ -485,7 +482,7 @@ def spy_llm(*args, **kwargs): memory_condensation_enabled=False, ) - result = settings_utils.save_settings(data, existing_agent=existing_agent) + result = settings_utils.save_settings(data, existing_agent=existing_agent, agent_store=deps) assert result.success is True saved_agent = deps.saved_agents[-1] diff --git a/tests/tui/modals/settings/test_timeout_preservation.py b/tests/tui/modals/settings/test_timeout_preservation.py index c30f66f5..0c613a5b 100644 --- a/tests/tui/modals/settings/test_timeout_preservation.py +++ b/tests/tui/modals/settings/test_timeout_preservation.py @@ -9,7 +9,6 @@ import pytest -import openhands_cli.tui.modals.settings.utils as settings_utils from openhands.sdk import LLM, Agent from openhands_cli.tui.modals.settings.utils import SettingsFormData, save_settings @@ -23,10 +22,8 @@ def save(self, agent: Agent) -> None: @pytest.fixture -def deps(monkeypatch) -> FakeAgentStore: - fake_store = FakeAgentStore() - monkeypatch.setattr(settings_utils, "agent_store", fake_store) - return fake_store +def deps() -> FakeAgentStore: + return FakeAgentStore() def test_invalid_timeout_keeps_existing_value(deps: FakeAgentStore): @@ -55,7 +52,7 @@ def test_invalid_timeout_keeps_existing_value(deps: FakeAgentStore): if form_data.timeout is None: form_data.timeout = getattr(existing_agent.llm, "timeout", None) - result = save_settings(form_data, existing_agent) + result = save_settings(form_data, existing_agent, agent_store=deps) assert result.success # The saved agent should retain the original timeout (120 seconds) assert result.error_message is None