Skip to content

fix(bash): track cwd across ; chains, bare cd, quoted paths, subshells - #28

Closed
Franky100-pig wants to merge 3 commits into
he-yufeng:mainfrom
Franky100-pig:fix/bash-cwd-tracking
Closed

Franky100-pig wants to merge 3 commits into
he-yufeng:mainfrom
Franky100-pig:fix/bash-cwd-tracking

Conversation

@Franky100-pig

Copy link
Copy Markdown

What

_update_cwd previously only split commands on &&, so a cd inside a
;-chained command, a quoted path with spaces, or a subshell was never
reflected in the working directory reported to the model.

How

Rewrote it with shlex(punctuation_chars=";&") plus a small
_split_statements helper, so separators inside quotes are not mistaken
for statement breaks. Now handled:

  • cd a; cd b and ls x; cd a (semicolon chains)
  • bare cd → home, cd to a missing dir → ignored
  • quoted targets with spaces (cd "my dir")
  • separators inside quotes are not treated as cd (echo "x; cd a")
  • cd inside a subshell (cd a) is not tracked

Tests

Added 7 tests to tests/test_tools.py (a bash_mod fixture clears
_local.cwd). Full suite: 160 passed, including the README LoC guard.

Docs

Synced README/README_CN headline numbers (engine 1161→1191, physical
2384→2417, net 1931→1961) and the bash.py tree count (131→164).

_update_cwd previously only split commands on '&&', so a 'cd' inside a
';'-chained command, a quoted path with spaces, or a subshell was not
reflected in the working directory reported to the model.

Rewrite using shlex (punctuation_chars=';&') so separators inside quotes
are not mistaken for statement breaks, and handle:
- 'cd a; cd b' and 'ls x; cd a' (semicolon chains)
- bare 'cd' -> home, 'cd' to a missing dir -> ignored
- quoted targets with spaces ('cd "my dir"')
- separators inside quotes are not treated as cd ('echo "x; cd a"')
- 'cd' inside a subshell '(cd a)' is not tracked

Adds 7 tests. Syncs README LoC numbers (engine 1161->1191, physical
2384->2417, net 1931->1961) and the bash.py tree count (131->164).
The suite actually has 160 tests (153 baseline + 7 added for the bash cwd
tracking fix), but the prose still said 146. Update both README and
README_CN to match the real number.
he-yufeng added a commit that referenced this pull request Sep 18, 2026
_update_cwd split statements on && and ; with a plain regex, so a
separator inside quotes was misread as a statement break
(echo "x; cd a" moved the tracked cwd) and a bare cd never went home.
Split with shlex (punctuation_chars=";&") so quoted separators hold
and quoted targets with spaces arrive as one token; bare cd resolves
to ~. README line counts resynced (engine 1192->1209, package
2,430->2,449 physical / 1,966->1,983 net).

Reference: the shape of the fix follows PR #28's diagnosis (statement
splitting must be quote-aware); implemented fresh here with tests for
the quoted-separator false positive, bare cd, and quoted paths.

166/166 tests, ruff clean.
@he-yufeng

Copy link
Copy Markdown
Owner

Thanks for this — the diagnosis was right and it held up when I ran it down on current main. The regex split misread separators inside quotes (echo "x; cd a" moved the tracked cwd) and a bare cd never went home; the quoted-path case already worked via the quote strip.

I implemented the fix fresh in 2c49e74 rather than merging this branch: same idea as yours (split on &&/; quote-aware via shlex punctuation_chars, then work on dequoted word lists), plus bare-cd-to-home and the README line-count resync the repo's badge test demands. New tests pin the quoted-separator false positive, bare cd, and a quoted target with spaces; full suite 166/166 green.

Closing this since the fix is in. Appreciate the careful writeup.

@he-yufeng he-yufeng closed this Sep 18, 2026
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.

2 participants