Skip to content

Fix worktree mode filtering out all diff-discovered functions#1522

Merged
KRRT7 merged 4 commits into
mainfrom
cf-fix-worktree-module-root-path
Feb 18, 2026
Merged

Fix worktree mode filtering out all diff-discovered functions#1522
KRRT7 merged 4 commits into
mainfrom
cf-fix-worktree-module-root-path

Conversation

@KRRT7
Copy link
Copy Markdown
Contributor

@KRRT7 KRRT7 commented Feb 18, 2026

Summary

  • In --worktree mode (git-diff path), get_git_diff resolves file paths from cwd (the original repo), but module_root/project_root have been mirrored to the worktree directory by mirror_paths_for_worktree_mode. This caused filter_functions to reject all diff-discovered functions as "outside module-root" since the paths are in different directory trees.
  • Fix uses pre-mirror roots for filtering, then remaps the discovered file paths to the worktree for downstream optimization.

Test plan

  • Verified codeflash --worktree now finds functions (previously reported "Found 0 function to optimize" with "Functions outside module-root: 1")
  • Verify --all and --file modes are unaffected (guarded by condition)

KRRT7 and others added 3 commits February 18, 2026 05:57
In --worktree mode, get_git_diff resolves file paths from cwd (the
original repo), but module_root/project_root are mirrored to the
worktree. This caused filter_functions to reject all diff-discovered
functions as "outside module-root". Use the pre-mirror roots for
filtering, then remap file paths to the worktree for downstream use.
@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Feb 18, 2026

PR Review Summary

Prek Checks

✅ All checks pass — ruff check and ruff format both passed with no issues.

Mypy

⚠️ 167 mypy errors reported across the two changed files, but all are pre-existing (same union-attr, type-arg, and other patterns that exist on main). No new type errors introduced by this PR.

Code Review

✅ No critical issues found. The changes are logically sound:

  • optimizer.py: In worktree mode with git-diff discovery, uses original (non-worktree) project_root/module_root for function discovery so path comparisons match, then remaps discovered file paths to the worktree via mirror_path + dataclasses.replace. The guard condition (use_original_roots) correctly checks for worktree mode and excludes --all/--file modes.
  • function_optimizer.py: Resolves git root from module_root (via GitRepo) instead of defaulting to cwd, fixing PR creation in worktree mode where cwd may not be inside the worktree.

Minor nit: Path(file_path) on line 223 of optimizer.py is redundant since file_path is already a Path (dict key type), but harmless.

Test Coverage

File Main PR Δ
codeflash/optimization/function_optimizer.py 18% (1162 stmts) 18% (1163 stmts) 0%
codeflash/optimization/optimizer.py 20% (411 stmts) 20% (430 stmts) 0%
Total 19% 19% 0%

Changed lines coverage:

  • optimizer.py lines 186-232 (new worktree remapping logic): Not covered by tests. The original get_functions_to_optimize call path was also uncovered on main.
  • function_optimizer.py lines 2226, 2230 (git root resolution change): Not covered. The original git_root_dir() calls at these lines were also uncovered on main.

Assessment: No coverage regression. The new code follows the same integration-heavy code paths that were already uncovered. These paths involve git worktree operations that are difficult to unit test without integration infrastructure.

Test results: 2374 passed, 57 skipped, 8 failed (all failures in test_tracer.py, pre-existing and unrelated to this PR).


Last updated: 2026-02-18

git_root_dir() searches from CWD (original repo), but in worktree mode
file paths have been remapped to the worktree. This caused relative_to()
to raise ValueError when creating PRs. Search from module_root instead
so root_dir is always in the same path space as the file paths.
@KRRT7 KRRT7 merged commit 2bcd91c into main Feb 18, 2026
26 of 35 checks passed
@KRRT7 KRRT7 deleted the cf-fix-worktree-module-root-path branch February 18, 2026 12:23
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