Skip to content

loopguard: a here-string is not a heredoc - #176

Merged
Aaron Li (aaron-jx-li) merged 1 commit into
microsoft:mainfrom
VISHNU0906:fix/here-string-is-not-a-heredoc
Nov 10, 2026
Merged

Aaron Li (aaron-jx-li) merged 1 commit into
microsoft:mainfrom
VISHNU0906:fix/here-string-is-not-a-heredoc

Conversation

@VISHNU0906

Copy link
Copy Markdown
Contributor

Hi Aaron Li (@aaron-jx-li), I ran into this while reading how loopguard tells edits and test runs apart in a shell command.

_HEREDOC_START in loopguard/shell_edits.py also matches the last two characters of a here-string (<<<). So after something like grep -q ok <<< "ok", every later line is treated as a heredoc body that never ends. Two effects:

  • sed -i 's/a/b/' pkg/mod.py on the next line is not counted as an edit, so TurnGuard.fold misses it.
  • exit_status_covers("python repro.py <<< \"y\"\npython -m pytest -q | tail -3") returns True, because the piped pytest line is hidden inside the "heredoc".

microskills/observe.py had the same pattern in _HEREDOC_RE, so classify_command could drop the commands between a here-string and a later line starting with its word.

The fix is a lookbehind and lookahead so << only matches when it is not part of <<<. Real heredocs, <<- and quoted delimiters behave as before.

Tests cover the shell-edit detection (including one through TurnGuard.fold), exit_status_covers and classify_command. With the fix reverted, 5 of them fail. python -m pytest tests passes on Python 3.10 and 3.13 (855 passed, 16 skipped), and experiments/tests passes too.

#174 also touches heredoc handling, but in turn.py's status_of_tests, so the two don't overlap.

`<<<` feeds one word to a command, but the heredoc pattern also matched
its last two characters. A command such as `grep -q ok <<< ok` then read
every later line as a heredoc body, so a `sed -i` after it was not
counted as an edit and a test run after it was not seen by
exit_status_covers. microskills' segments() had the same pattern and
could elide the commands between a here-string and a later line that
starts with its word.

Signed-off-by: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com>
@aaron-jx-li
Aaron Li (aaron-jx-li) merged commit d618e79 into microsoft:main Nov 10, 2026
10 checks passed
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