Files
hermes-agent/agent/redact_command_segments.py
Maciej Lisiewski 0aee4fc05b refactor(redact): move _command_segments to redact_command_segments
Main grew agent/redact.py to 2086 lines (over the 2000 target, so it may
only go down); this PR's URL-param check adds 24. Move the leaf splitter,
used only by the env-dump / secret-file classifiers, into a sibling.

Refs #119852
2026-11-14 21:01:00 +01:00

32 lines
994 B
Python

"""Quote-aware pipeline/sequence splitting for agent.redact's command classifiers (moved from agent.redact)."""
def _command_segments(command: str) -> list[str]:
"""Pipeline/sequence segments, split only on unquoted ``| ; &`` so an
``awk '{print $1; print $2}'`` program or ``grep 'foo|bar'`` pattern stays one
segment. Backslash is not an escape (Windows ``C:\\Users\\...``)."""
segments: list[str] = []
buf: list[str] = []
quote: str | None = None
for ch in command:
if quote:
buf.append(ch)
if ch == quote:
quote = None
continue
if ch in "'\"":
quote = ch
buf.append(ch)
continue
if ch in "|;&":
seg = "".join(buf).strip()
if seg:
segments.append(seg)
buf = []
continue
buf.append(ch)
seg = "".join(buf).strip()
if seg:
segments.append(seg)
return segments