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
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:
@@ -91,8 +91,10 @@ PROCESS_SKILL_PREFIX = "grimoire-proc-"
|
||||
PROCESS_MOMENTS_FIELD = "moments"
|
||||
PROCESS = "process"
|
||||
|
||||
_SEGMENT_SPLIT = re.compile(r"&&|\|\||[;|\n]")
|
||||
_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)
|
||||
@@ -169,14 +171,83 @@ def _squash(text: str) -> str:
|
||||
|
||||
|
||||
def _segments(command: str) -> list[str]:
|
||||
out = []
|
||||
for part in _SEGMENT_SPLIT.split(command or ""):
|
||||
seg = part.strip().lstrip("(").strip()
|
||||
"""The commands a shell line runs, each from its head.
|
||||
|
||||
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):
|
||||
seg = _ENV_ASSIGN.sub("", seg, count=1)
|
||||
seg = _squash(seg)
|
||||
if 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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user