Skip to content

The agent that grows with you

fix(terminal): `&>` after a backgrounded compound is a redirect, not a terminator

kshitijk4poor62f2c82f2b6e
diff --git a/tests/tools/test_terminal_compound_background.py b/tests/tools/test_terminal_compound_background.py
index 101b6fbbd12..beaa1d95f4a 100644
--- a/tests/tools/test_terminal_compound_background.py
+++ b/tests/tools/test_terminal_compound_background.py
@@ -142,6 +142,20 @@ class TestTrailingStatementSeparator:
# `{ ...; } | cmd` is valid; the pipe is its own terminator.
assert rewrite("a && b & | cat") == "a && { b & } | cat"
+ def test_redirect_prefix_on_trailing_command_gets_separator(self):
+ # `&>` after the group is a redirect for the NEXT command, not a
+ # terminator: `{ b & } &>/dev/null c` is a syntax error.
+ assert rewrite("a && b & &>/dev/null c") == "a && { b & } ; &>/dev/null c"
+
+ def test_case_arm_terminator_untouched(self):
+ # `;;` already terminates the arm; adding `;` would leave an empty
+ # command between `;` and `;;`, which bash rejects.
+ assert rewrite("case $x in p) b && c & ;; esac") == "case $x in p) b && { c & } ;; esac"
+
+ def test_separator_is_idempotent(self):
+ once = rewrite("echo hi && sleep 5 & echo done")
+ assert rewrite(once) == once
+
def test_second_background_then_trailing(self):
assert rewrite("echo a && sleep 5 & echo b & echo c") == (
"echo a && { sleep 5 & } ; echo b & echo c"
@@ -168,6 +182,9 @@ class TestRewriteIsValidBash:
"A && B &; C",
"A && B &\nC",
"cd /tmp && python3 -m http.server 0 &>/dev/null & curl localhost",
+ "a && b & &>/dev/null c",
+ "case $x in p) b && c & ;; esac",
+ "A && B & echo x\nC && D & echo y && E & echo z",
],
)
def test_rewrite_parses(self, command):
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 67d7f07e985..a5e2d2bfc0e 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -1018,17 +1018,17 @@ def _rewrite_compound_background(command: str) -> str:
# `{` needs a trailing space in bash; the closing `}` needs to be
# preceded by `;` or `&` — we're providing `&` from the backgrounding.
#
- # The source `&` we consumed into the group also served as the
- # statement separator when another command followed on the SAME line
- # (`A && B & C`). A brace group must be terminated by `;`, `&`, `|`,
- # a newline, or `)`/`}` before the next command, so `{ B & } C` is a
- # bash syntax error that fails the entire command. Restore a `;`
- # separator after `}` whenever the suffix resumes with command text.
- # Strip only spaces/tabs (not newlines) — a newline already terminates
- # the group, and an existing separator (`;`/`&`/`|`/`)`/`}`) needs no
- # help.
+ # The consumed `&` also separated the compound from any statement
+ # that followed on the same line (`A && B & C`); `{ B & } C` is a
+ # syntax error, so restore a `;` when the suffix resumes with command
+ # text. No separator when the suffix already starts with a
+ # terminator (`;` `&` `|` newline `)` `}`) — except `&>`, which is a
+ # redirect prefix for the NEXT command, not a terminator.
tail = suffix.lstrip(" \t")
- separator = " ;" if tail and tail[0] not in ";\n&|)}" else ""
+ needs_separator = bool(tail) and (
+ tail[0] not in ";\n&|)}" or tail.startswith("&>")
+ )
+ separator = " ;" if needs_separator else ""
result = prefix + "{ " + middle + "& }" + separator + suffix
return result