Skip to content

fix: prevent wrong-cwd auto-detection when workspace not detected#175

Merged
ichoosetoaccept merged 1 commit intomainfrom
02-17-fix-prevent-wrong-cwd-auto-detection-when-workspace-not-detected
Feb 17, 2026
Merged

fix: prevent wrong-cwd auto-detection when workspace not detected#175
ichoosetoaccept merged 1 commit intomainfrom
02-17-fix-prevent-wrong-cwd-auto-detection-when-workspace-not-detected

Conversation

@ichoosetoaccept
Copy link
Member

@ichoosetoaccept ichoosetoaccept commented Feb 17, 2026

Problem

When the MCP client doesn't provide workspace roots and CRB_WORKSPACE isn't set, _get_workspace_cwd returns None. This causes gh CLI commands to silently use the server's process directory — almost certainly the wrong git repo — producing confusing errors like "no pull requests found for branch main".

Solution

  • _check_auto_detect_prerequisites — new guard that raises a clear, actionable GhError when cwd is None and the caller didn't provide explicit repo/pr_number params. Tells the agent exactly what to pass.
  • Enhanced _get_workspace_cwd logging — granular warnings for empty roots list, unsupported URI scheme, exception details, and an explicit warning when falling back to None in MCP context.
  • All 10 tool handlers guarded with the prerequisite check before any auto-detection call.
  • Error message does NOT recommend CRB_WORKSPACE — it's a per-process env var that breaks multi-window setups (multiple editors sharing one MCP server config but working on different repos).

Tests

  • 7 new tests for _check_auto_detect_prerequisites (noop when cwd detected, noop when all params explicit, raises for missing pr/repo/both, fix guidance text, correct Missing: line)
  • 1 new test for unsupported URI scheme fallthrough in _get_workspace_cwd
  • Full suite: 420 passed, 91% coverage

Fixes #174

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@greptile-apps
Copy link

greptile-apps bot commented Feb 17, 2026

Greptile Summary

Fixed a bug where the server could auto-detect the wrong repository when workspace detection fails. The PR introduces a new _check_auto_detect_prerequisites() function that validates workspace detection succeeded before allowing auto-detection of repository and PR number parameters.

Key Changes:

  • Added _WORKSPACE_HELP constant with clear error message guidance
  • Enhanced _get_workspace_cwd() logging (debug → info for successful detection, improved warning messages)
  • Created _check_auto_detect_prerequisites() to fail fast with helpful errors when workspace isn't detected and parameters are missing
  • Integrated the prerequisite check into all 9 MCP tool endpoints before auto-detection occurs
  • Added comprehensive test coverage with 6 new test cases

Impact:
Prevents confusing errors like "no pull requests found for branch main" when the server's process cwd happens to be a different git repo. Users now get clear guidance to explicitly pass repo and pr_number parameters when workspace detection fails.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is defensive, well-tested, and addresses a real UX issue. The new prerequisite check only raises errors in scenarios where auto-detection would have failed anyway (workspace not detected + missing parameters), making the failure mode clearer and more actionable. All 9 tool endpoints correctly call the check before auto-detection, and the comprehensive test suite validates all edge cases including partial parameter scenarios.
  • No files require special attention

Important Files Changed

Filename Overview
src/codereviewbuddy/server.py Added workspace detection guards and improved error logging to prevent wrong-cwd auto-detection
tests/test_server.py Added comprehensive test coverage for new _check_auto_detect_prerequisites function

Flowchart

flowchart TD
    A[MCP Tool Called] --> B[get_workspace_cwd]
    B --> C{MCP Roots Available?}
    C -->|Yes| D[Return MCP Root Path]
    C -->|No| E{CRB_WORKSPACE Set?}
    E -->|Yes| F[Return Env Var Path]
    E -->|No| G[Return None + Warning]
    
    D --> H[check_auto_detect_prerequisites]
    F --> H
    G --> H
    
    H --> I{cwd Detected?}
    I -->|Yes| J[Auto-detection Safe]
    I -->|No| K{All Params Explicit?}
    K -->|Yes| J
    K -->|No| L[Raise GhError with Help]
    
    J --> M[Proceed with Auto-detection]
    M --> N[Execute Tool Logic]
Loading

Last reviewed commit: 0c72cf9

@ichoosetoaccept ichoosetoaccept merged commit f39a6df into main Feb 17, 2026
11 checks passed
@ichoosetoaccept ichoosetoaccept deleted the 02-17-fix-prevent-wrong-cwd-auto-detection-when-workspace-not-detected branch February 17, 2026 16:28
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.

Branch/repo auto-detection fails when MCP server cwd differs from repo

1 participant