The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): navigation targets only from a cd that starts a shell segment
teknium1088d292d364f
diff --git a/agent/subdirectory_hints.py b/agent/subdirectory_hints.pyindex e11272bd363..cfd47d54d77 100644--- a/agent/subdirectory_hints.py+++ b/agent/subdirectory_hints.py@@ -53,7 +53,28 @@ def _first_hint_file(directory: Path):_NAV_COMMANDS = frozenset({"cd", "pushd"})-_SHELL_OPERATORS = frozenset({"&&", "||", "|", ";", "&"})+_SHELL_OPERATORS = frozenset({"&&", "||", "|", ";", "&", ";;", "|&", "(", ")"})+++def _nav_targets(cmd: str) -> list:+ """Operands of `cd` / `pushd` that begin a shell segment. `cd -` and bare `cd` yield nothing."""+ lexer = shlex.shlex(cmd, posix=True, punctuation_chars=True)+ lexer.whitespace_split = True+ try:+ tokens = list(lexer)+ except ValueError:+ return []+ targets, segment_start = [], True+ for idx, token in enumerate(tokens):+ if token in _SHELL_OPERATORS:+ segment_start = True+ continue+ if segment_start and token in _NAV_COMMANDS:+ operand = next((t for t in tokens[idx + 1:] if t in _SHELL_OPERATORS or not t.startswith("-")), None)+ if operand and operand not in _SHELL_OPERATORS:+ targets.append(operand)+ segment_start = False+ return targetsclass SubdirectoryHintTracker:@@ -127,13 +148,11 @@ class SubdirectoryHintTracker:except ValueError:tokens = cmd.split()# `cd backend && ls`: a bare directory name has no `/` or `.`, so the generic filter below drops- # it; the token after a navigation command is a path by construction (#11032). `cd -` / bare `cd`- # are skipped (nothing under the working dir to load).- for idx, token in enumerate(tokens):- if token in _NAV_COMMANDS:- target = next((t for t in tokens[idx + 1:] if not t.startswith("-")), None)- if target and target not in _SHELL_OPERATORS:- self._add_path_candidate(target.rstrip(";"), candidates)+ # it; the operand of a navigation command is a path by construction (#11032). Only a `cd` at the+ # START of a shell segment counts (`echo cd backend` is prose); punctuation-aware tokenizing keeps+ # a quoted `'backend;'` literal while splitting bare `backend;ls` at the operator.+ for target in _nav_targets(cmd):+ self._add_path_candidate(target, candidates)for token in tokens:if token.startswith(("-", "http://", "https://", "git@")) or ("/" not in token and "." not in token):continuediff --git a/tests/agent/test_subdirectory_hints.py b/tests/agent/test_subdirectory_hints.pyindex 78a8cdb5628..1751318d739 100644--- a/tests/agent/test_subdirectory_hints.py+++ b/tests/agent/test_subdirectory_hints.py@@ -78,13 +78,20 @@ class TestSubdirectoryHintTracker:- @pytest.mark.parametrize("command", ["cd backend && ls", "pushd backend", "echo start; cd backend; ls"])+ @pytest.mark.parametrize("command", ["cd backend && ls", "pushd backend", "echo start; cd backend; ls", "cd backend;ls"])def test_bare_directory_after_navigation_command_is_a_path(self, project, command):"""`cd backend` has no `/` or `.` yet names a subdirectory; its AGENTS.md must load (#11032)."""tracker = SubdirectoryHintTracker(working_dir=str(project))result = tracker.check_tool_call("terminal", {"command": command})assert result is not None and "Backend-specific instructions" in result+ @pytest.mark.parametrize("command", ["echo cd backend", "printf '%s %s' cd backend", "cd 'backend;'"])+ def test_cd_as_an_argument_or_a_quoted_other_name_is_not_navigation(self, project, command):+ """Only a `cd` that starts a shell segment navigates, and a quoted `'backend;'` is a+ different directory than `backend` — neither may inject backend/AGENTS.md."""+ tracker = SubdirectoryHintTracker(working_dir=str(project))+ assert tracker.check_tool_call("terminal", {"command": command}) is None+def test_relative_path(self, project):"""Relative paths resolved against working_dir."""tracker = SubdirectoryHintTracker(working_dir=str(project))