fix(639): quoted text and heredoc bodies are not commands when an act is matched to a moment
CI & Build / TypeScript typecheck (push) Successful in 35s
CI & Build / Python lint (push) Canceled after 0s
CI & Build / Python tests (push) Canceled after 0s
CI & Build / integration (push) Canceled after 0s
CI & Build / Build & push image (push) Canceled after 0s
CI & Build / Plugin hooks (push) Canceled after 2s

A Bash command was split into segments on every `&&`, `||`, `;`, `|` and
newline, including those inside quotes and the lines of a heredoc. A script
written with `python3 - <<EOF` that mentioned curl reached env.reach, and a
grep for "a && git push" reached work.deliver. The splitter now reads the
line as the shell does: separators inside quotes are text, a heredoc's body
is skipped through its end word (`<<-` strips leading tabs), and a
backslash-newline continues the line. Command triggers use the same split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 17:57:41 -05:00
co-authored by Claude Opus 5.5
parent 5f55bd1575
commit cbe4b25dad
2 changed files with 99 additions and 4 deletions
+75 -4
View File
@@ -91,8 +91,10 @@ PROCESS_SKILL_PREFIX = "grimoire-proc-"
PROCESS_MOMENTS_FIELD = "moments" PROCESS_MOMENTS_FIELD = "moments"
PROCESS = "process" PROCESS = "process"
_SEGMENT_SPLIT = re.compile(r"&&|\|\||[;|\n]")
_ENV_ASSIGN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=\S*\s+") _ENV_ASSIGN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=\S*\s+")
# `<<WORD`, `<<-WORD`, `<<'WORD'`, `<<"WORD"`: a heredoc whose body follows
# the line and ends at a line holding only WORD (`<<<` is a here-string).
_HEREDOC = re.compile(r"""<<(-?)[ \t]*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\2""")
@dataclass(frozen=True) @dataclass(frozen=True)
@@ -169,14 +171,83 @@ def _squash(text: str) -> str:
def _segments(command: str) -> list[str]: def _segments(command: str) -> list[str]:
out = [] """The commands a shell line runs, each from its head.
for part in _SEGMENT_SPLIT.split(command or ""):
seg = part.strip().lstrip("(").strip() Split where the shell separates commands (`&&`, `||`, `;`, `|`, a
newline) — but only outside quotes, and never inside a heredoc's body. A
quoted argument and the text a heredoc feeds a program are data, not
commands: `grep "a && git push"` runs grep, and a script written with
`cat <<EOF` that contains `curl` reaches no host.
"""
text = command or ""
out: list[str] = []
buf: list[str] = []
heredocs: list[tuple[str, bool]] = []
def flush() -> None:
seg = "".join(buf).strip().lstrip("(").strip()
buf.clear()
while _ENV_ASSIGN.match(seg): while _ENV_ASSIGN.match(seg):
seg = _ENV_ASSIGN.sub("", seg, count=1) seg = _ENV_ASSIGN.sub("", seg, count=1)
seg = _squash(seg) seg = _squash(seg)
if seg: if seg:
out.append(seg) out.append(seg)
i, n, quote = 0, len(text), ""
while i < n:
c = text[i]
if quote:
if c == "\\" and quote == '"' and i + 1 < n:
buf.append(text[i:i + 2])
i += 2
continue
buf.append(c)
if c == quote:
quote = ""
i += 1
continue
if c == "\\" and i + 1 < n:
# A backslash-newline continues the line; anything else is escaped.
if text[i + 1] != "\n":
buf.append(text[i:i + 2])
i += 2
continue
if c in "'\"":
quote = c
buf.append(c)
i += 1
continue
if text.startswith("<<", i) and not text.startswith("<<<", i):
m = _HEREDOC.match(text, i)
if m:
heredocs.append((m.group(3), m.group(1) == "-"))
buf.append(m.group(0))
i = m.end()
continue
if text.startswith("&&", i) or text.startswith("||", i):
flush()
i += 2
continue
if c in ";|":
flush()
i += 1
continue
if c == "\n":
flush()
i += 1
# The bodies of this line's heredocs, in order, are skipped whole.
for word, tabs in heredocs:
while i < n:
end = text.find("\n", i)
line = text[i:end if end != -1 else n]
i = end + 1 if end != -1 else n
if (line.lstrip("\t") if tabs else line) == word:
break
heredocs.clear()
continue
buf.append(c)
i += 1
flush()
return out return out
+24
View File
@@ -62,6 +62,30 @@ def test_a_command_prefix_needs_a_word_boundary_and_the_head_of_a_segment(comman
assert "work.deliver" not in _reached("Bash", {"command": command}) assert "work.deliver" not in _reached("Bash", {"command": command})
@pytest.mark.parametrize("command", [
# A heredoc's body is data fed to a program, whatever its lines say.
"python3 - <<'EOF'\nimport os\n | curl -fsS http://x\nEOF",
"cat > hook.sh <<EOF\ncurl http://x\nEOF\nchmod +x hook.sh",
"cat <<-END\n\tcurl http://x\n\tEND",
# Quoted text is an argument, separators inside it included.
'grep -n "a && curl http://x" file',
"echo 'step; curl http://x'",
'git commit -m "fix: curl | wget"',
])
def test_quoted_text_and_heredoc_bodies_are_not_commands(command):
assert "env.reach" not in _reached("Bash", {"command": command})
@pytest.mark.parametrize("command", [
"cat > x.sh <<'EOF'\necho hi\nEOF\ncurl http://x",
"python3 - <<EOF && curl http://x\nprint(1)\nEOF",
'echo "a;b" ; curl http://x',
"make build \\\n && curl http://x",
])
def test_a_command_after_a_heredoc_or_a_quote_is_still_one(command):
assert "env.reach" in _reached("Bash", {"command": command})
def test_one_action_reaches_every_moment_it_is(): def test_one_action_reaches_every_moment_it_is():
"""`kubectl apply` is a run, a deliver and a reach outside the workspace.""" """`kubectl apply` is a run, a deliver and a reach outside the workspace."""
assert _reached("Bash", {"command": "kubectl apply -f deploy.yaml"}) == [ assert _reached("Bash", {"command": "kubectl apply -f deploy.yaml"}) == [