Merge pull request 'fix(ai): match target root by basename in _apply_plan()' (#77) from fix/issue-76-apply-plan-root-match into main

This commit is contained in:
claude-code 2026-04-12 20:39:15 -06:00
commit 1328793a55
2 changed files with 43 additions and 0 deletions

View file

@ -1549,21 +1549,29 @@ def _apply_plan(all_dirs, to_investigate, plan, target):
return list(to_investigate), {} return list(to_investigate), {}
# Build lookup from relative path to absolute path. # Build lookup from relative path to absolute path.
# The target root maps to "." via relpath, but the planner sees
# basename(target) in the tree output and uses that as the path.
# Register both so either form matches (#76).
rel_to_abs = {} rel_to_abs = {}
for d in all_dirs: for d in all_dirs:
rel = os.path.relpath(d, target) rel = os.path.relpath(d, target)
rel_to_abs[rel] = d rel_to_abs[rel] = d
if rel == ".":
rel_to_abs[os.path.basename(d)] = d
# Classify directories by tier. # Classify directories by tier.
skip_set = set() skip_set = set()
priority_set = set() priority_set = set()
shallow_set = set() shallow_set = set()
turn_map = {} turn_map = {}
unmatched = []
for entry in plan.get("skip_dirs", []): for entry in plan.get("skip_dirs", []):
rel = entry.get("path", "") rel = entry.get("path", "")
if rel in rel_to_abs: if rel in rel_to_abs:
skip_set.add(rel_to_abs[rel]) skip_set.add(rel_to_abs[rel])
else:
unmatched.append(rel)
for entry in plan.get("priority_dirs", []): for entry in plan.get("priority_dirs", []):
rel = entry.get("path", "") rel = entry.get("path", "")
@ -1573,6 +1581,8 @@ def _apply_plan(all_dirs, to_investigate, plan, target):
abs_path = rel_to_abs[rel] abs_path = rel_to_abs[rel]
priority_set.add(abs_path) priority_set.add(abs_path)
turn_map[abs_path] = capped turn_map[abs_path] = capped
else:
unmatched.append(rel)
for entry in plan.get("shallow_dirs", []): for entry in plan.get("shallow_dirs", []):
rel = entry.get("path", "") rel = entry.get("path", "")
@ -1580,6 +1590,15 @@ def _apply_plan(all_dirs, to_investigate, plan, target):
abs_path = rel_to_abs[rel] abs_path = rel_to_abs[rel]
shallow_set.add(abs_path) shallow_set.add(abs_path)
turn_map[abs_path] = _SHALLOW_TURNS turn_map[abs_path] = _SHALLOW_TURNS
else:
unmatched.append(rel)
if unmatched:
print(
f" [AI] Warning: plan referenced unknown dirs: "
f"{', '.join(unmatched)}",
file=sys.stderr,
)
# Remove skipped dirs from the investigation list. # Remove skipped dirs from the investigation list.
remaining = [d for d in to_investigate if d not in skip_set] remaining = [d for d in to_investigate if d not in skip_set]

View file

@ -878,6 +878,30 @@ class TestApplyPlan(unittest.TestCase):
) )
self.assertEqual(len(ordered), len(subset)) self.assertEqual(len(ordered), len(subset))
def test_target_root_matched_by_basename(self):
"""Plan can reference the target root by its basename, not just '.' (#76)."""
plan = _default_plan()
basename = os.path.basename(self.tmp)
plan["priority_dirs"] = [
{"path": basename, "reason": "root is important",
"suggested_turns": 18},
]
_, turn_map = _apply_plan(
self.all_dirs, list(self.all_dirs), plan, self.target,
)
self.assertEqual(turn_map[self.tmp], 18)
def test_target_root_matched_by_dot(self):
"""Plan can also reference the target root as '.'."""
plan = _default_plan()
plan["priority_dirs"] = [
{"path": ".", "reason": "root", "suggested_turns": 16},
]
_, turn_map = _apply_plan(
self.all_dirs, list(self.all_dirs), plan, self.target,
)
self.assertEqual(turn_map[self.tmp], 16)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# _get_child_summaries (updated placeholder behavior) # _get_child_summaries (updated placeholder behavior)