#!/usr/bin/env python3 """hooks/lib/strip_heredoc.py -- shared helper, not a gate (inkstone/ISEP#40 S7: helpers live in lib/ and do not count as a hand-written gate). What it guards: nothing by itself. It strips the *body* of a bash heredoc out of a command string, keeping the start line (the control-flow part) intact. Why: a heredoc body is data, not an instruction. Two mis-blocks on 2026-08-20 (inkstone/InkStoneCo#23, #56) shared one root cause: a gate ran a keyword scan over the *entire* command string, heredoc body included, so text that merely *mentioned* a trigger phrase inside a file being written (or a comment being posted) was treated as if that phrase were actually being executed. One case was writing docs/TESTING.md (the body had one example line of a GitHub push command as literal text for a human to try later); the other was posting a Gitea comment that quoted this very ticket's own description. This helper only removes the body; each gate keeps its own keyword rules, it just no longer has to solve heredoc-quoting itself. Usage: printf '%s' "$CMD" | python3 hooks/lib/strip_heredoc.py Or import it as a module: from strip_heredoc import strip_heredocs """ import re import sys # < str: """Replace every heredoc body in cmd with nothing; keep the start line and everything outside heredocs untouched.""" lines = cmd.split("\n") out = [] i = 0 n = len(lines) while i < n: line = lines[i] m = _START_RE.search(line) if not m: out.append(line) i += 1 continue strip_tabs = m.group(1) == "-" delim = m.group(3) out.append(line) # the start line itself is control flow, keep it i += 1 found_end = False while i < n: probe = lines[i] check = probe.lstrip("\t") if strip_tabs else probe if check == delim: i += 1 # the terminator line is a marker, drop it too found_end = True break i += 1 # body line: drop it, do not append to out if not found_end: # command was truncated / no terminator found -- do not invent # one, we've already consumed to the end of the string. pass return "\n".join(out) def main() -> None: cmd = sys.stdin.read() sys.stdout.write(strip_heredocs(cmd)) if __name__ == "__main__": main()