Skip to content

fix(core): strip bash cwd marker when output has no trailing newline - #418

Open
mccalpink wants to merge 1 commit into
lessweb:mainfrom
mccalpink:fix/415-cwd-sentinel-no-newline
Open

mccalpink wants to merge 1 commit into
lessweb:mainfrom
mccalpink:fix/415-cwd-sentinel-no-newline

Conversation

@mccalpink

Copy link
Copy Markdown

Summary

Fixes the bash tool leaking its internal working-directory marker into the command output when the command's output does not end with a newline (e.g. printf 'x' returned x__DEEPCODE_PWD__<token>__<cwd> and cwd: null).

Closes #415

Root cause

buildShellCommand appends printf '%s%s\n' "<marker>" "$PWD" after the user command. stripMarker only recognised the marker when it started a line (startsWith(marker)). If the command's last line had no trailing \n, the marker was glued to that line, was not found, and was returned to the model as part of the output, with cwd reported as null (so cwd tracking was lost too).

Fix

In packages/core/src/tools/bash-handler.ts, stripMarker now finds the last occurrence of the marker anywhere in a line (lastIndexOf), reads the cwd from the text after it, and keeps the text before it as the command output. Chosen over printing a leading newline before the marker because it leaves output of ordinary commands unchanged compared to the current behaviour and needs no extra newline stripping; when the marker is on its own line the behaviour is identical to before. For unterminated output the marker's own line break is dropped, so printf 'x' yields exactly x.

Because the marker can now be matched mid-line, the full marker must not appear in the command text itself: printf now receives the token as a separate argument (printf '__DEEPCODE_PWD__%s__%s\n' "<token>" "$PWD"). Otherwise a command that prints its own command line (ps, set -x) and exits before the marker is written could have that line mistaken for the marker.

Differences from the diff suggested in the issue: for unterminated output this keeps exactly partial (the issue's diff yields partial\n). Passing the cwd through a temp file instead of stdout was not taken, as it is out of scope for this bug.

Tests

Added to packages/core/src/tests/tool-handlers.test.ts:

  • Bash strips the cwd marker when command output has no trailing newline (single-line and multi-line unterminated output; checks output and metadata.cwd) - fails on main, passes with the fix.
  • Bash keeps trailing newline and cwd tracking for output that ends with a newline - regression guard for the unchanged path.
  • Bash does not expose the cwd marker in the command line of the shell (skipped on Windows) - runs ps -o args= -p $$; exit 3, which prints the shell's own command line and exits before the real marker; checks that the output is not truncated and that the session cwd is kept - fails without the printf change, passes with it.

How I verified it

  • Red: with only the stripMarker change (no printf change), node --import tsx --test --test-concurrency=1 --test-name-pattern="Bash (strips|keeps|does not expose)" tool-handlers.test.ts gives 2 passed, 1 failed (the marker-in-argv test; output cut at the marker text). On main the first test fails as well (output x__DEEPCODE_PWD__..., cwd: null).
  • Green with the full fix, same command: 3 passed, 0 failed.
  • npm run check (typecheck + lint + prettier): passes.
  • npm test (all workspaces; Linux, Node 22): cli 706/706 passed, core 685 passed + 2 skipped (Windows-only) / 0 failed, vscode-ide-companion 121/121 passed.
  • Not run: Windows / macOS / Node 24 matrix from CI, build/bundle.

This PR was prepared with AI assistance (Claude Code). I have reviewed the change and the test results.

stripMarker only matched the marker at the start of a line, so a command whose output did not end with a newline (e.g. printf 'x') leaked the marker into the tool output and reported cwd as null. Locate the marker anywhere in its line and keep the text before it. Closes lessweb#415.
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.

bash: cwd sentinel leaks into output when the command's last line has no newline

1 participant