diff --git a/scripts/hooks/README.md b/scripts/hooks/README.md index 88257474..6b125d34 100644 --- a/scripts/hooks/README.md +++ b/scripts/hooks/README.md @@ -25,7 +25,7 @@ Bash 직후 ──> post-bash ──> desktop에서 pnpm·게이트를 돌 | N2 | 같은 때, `*.md` | `scripts/check-work-docs.py`를 돌린다(작업 매뉴얼의 분량·이력 ID·뼈대·상대 링크) | 실패 줄. 고친 문서를 가리키는 줄이 있으면 그것만 | | N5 | Stop | 이 세션에서 desktop만 확인할 수 있는 파일(아래 밖의 모든 파일)을 고친 뒤 desktop 실행이 없으면 턴을 한 번 막고 이유를 준다 | `decision: block`, 다음 편집 전까지 한 번 | -- N5가 "desktop 실행"으로 치는 것: `ssh …`로 pnpm·`ci:gate`·`verify.sh`·vitest·playwright·`node scripts/…`를 돌린 명령, 또는 `scripts/verify.sh`를 `docs`·`hooks` 밖 단계로(`--on here` 없이) 돌린 명령. +- N5가 "desktop 실행"으로 치는 것: `ssh …`로 pnpm·`ci:gate`·`verify.sh`·vitest·playwright·`node scripts/…`를 돌린 명령(로그를 읽기만 하는 `cat`·`grep`·`tail` 등은 빼고), 또는 `scripts/verify.sh`를 명령으로 `docs`·`hooks` 밖 단계로(`--on here`·`--dry-run` 없이) 돌린 명령. 앞의 `VAR=값`과 `timeout`·`nohup` 같은 감싸는 낱말은 건너뛴다. `verify.sh`를 읽는 명령(`sed`, `grep`, `git log -- scripts/verify.sh`)은 세지 않는다. - N5가 세지 않는 파일(노트북에서 확인되는 것): `.claude/`의 문서 밖 파일(설정·훅 상태), `scripts/hooks/`, `scripts/tests/`, `scripts/check-work-docs.py`, `docs/ledger/`. 이것들은 `scripts/verify.sh docs`·`hooks`가 노트북에서 본다. `CLAUDE.md`, `src/**/AGENTS.md`·`CLAUDE.md`, `.claude/**/*.md`는 desktop `verify:documentation`도 보므로 센다. - N5가 기억하는 것은 `.claude/hook-state/.json`이다(git이 무시한다). - 권한 쪽 차단: `.claude/settings.json`의 `permissions.deny`가 실제 `.env`·`.env.local`·`.env.*.local`·`.env.` 읽기·쓰기를 막는다. `.env.example`은 막지 않는다. diff --git a/scripts/hooks/hooks.py b/scripts/hooks/hooks.py index 65987781..6ddeb4f5 100755 --- a/scripts/hooks/hooks.py +++ b/scripts/hooks/hooks.py @@ -83,8 +83,16 @@ LAPTOP_ONLY = ( ) DESKTOP_HOST = re.compile(r"desktop") DESKTOP_WORK = re.compile(r"\b(?:pnpm|ci:gate|verify\.sh|vitest|playwright)\b|\bnode\s+scripts/") -VERIFY = re.compile(r"(?:^|[\s;&|(])(?:\./)?scripts/verify\.sh\s+(\S+)(.*)") LAPTOP_STAGES = {"docs", "hooks"} +# 1112/08-09. One command of a shell line: what follows ;, &&, ||, |, ( or a newline. +COMMAND_SEPARATOR = re.compile(r"&&|\|\||;|\||\(|\)|\n") +# Words that run the next command: `timeout 900 scripts/verify.sh …`. +WRAPPERS = {"timeout": 1, "nohup": 0, "setsid": 0, "env": 0, "time": 0, "exec": 0} +# A remote command made only of these reads logs; it runs nothing. +READ_ONLY = {"cat", "grep", "tail", "head", "less", "ls", "sed", "awk", "wc", "find", "stat", "echo", + "printf", "date", "uptime", "ps", "test", "true", "for", "do", "done", "if", "then", "fi"} +ASSIGNMENT = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=") +STEP_WORD = re.compile(r"^[a-z][a-z0-9-]*$") # ---------------------------------------------------------------------------------------------------- @@ -277,20 +285,52 @@ def needs_desktop(path: str) -> bool: return not any(pattern.search(path) for pattern in LAPTOP_ONLY) -def ran_on_desktop(command: str) -> bool: - """An `ssh …` running pnpm or a gate, or scripts/verify.sh with a stage that goes there.""" +def split_words(text: str) -> list[str]: try: - words = shlex.split(command) + return shlex.split(text) except ValueError: - words = command.split() + return text.split() + + +def command_words(segment: str) -> list[str]: + """The words of one command, its leading `VAR=value` and wrapper words dropped.""" + words = split_words(segment) + while words: + if ASSIGNMENT.match(words[0]): + words = words[1:] + elif words[0] in WRAPPERS: + words = words[1 + WRAPPERS[words[0]]:] + else: + break + return words + + +def runs_only_reads(remote: str) -> bool: + segments = [command_words(part) for part in COMMAND_SEPARATOR.split(remote)] + return all(not words or words[0] in READ_ONLY for words in segments) + + +def ran_on_desktop(command: str) -> bool: + """An `ssh …` running pnpm or a gate, or scripts/verify.sh running a step that goes there. + + 1112/08-09. Reading verify.sh (`sed`, `grep`, `git log -- scripts/verify.sh`), `--dry-run` and + an ssh that only reads logs run nothing on the desktop. + """ + words = split_words(command) for position, word in enumerate(words): if word.endswith("ssh"): - hosts = [w for w in words[position + 1:position + 5] if not w.startswith("-")][:2] - if any(DESKTOP_HOST.search(host) for host in hosts) and DESKTOP_WORK.search(command): + rest = [w for w in words[position + 1:] if not w.startswith("-")] + hosts, remote = rest[:1], " ".join(rest[1:]) + if (any(DESKTOP_HOST.search(host) for host in hosts) and DESKTOP_WORK.search(remote) + and not runs_only_reads(remote)): return True - for match in VERIFY.finditer(command): - stage, rest = match.group(1), match.group(2) - if stage not in LAPTOP_STAGES and not re.search(r"--on\s+here\b", rest): + for segment in COMMAND_SEPARATOR.split(command): + words = command_words(segment) + if len(words) < 2 or not re.search(r"(?:^|/)scripts/verify\.sh$", words[0]): + continue + step, flags = words[1], words[2:] + if (STEP_WORD.match(step) and step not in LAPTOP_STAGES and "--dry-run" not in flags + and not re.search(r"--on\s+here\b", " ".join(flags))): return True return False diff --git a/scripts/tests/test_hooks.py b/scripts/tests/test_hooks.py index 2517e5d6..e109318d 100644 --- a/scripts/tests/test_hooks.py +++ b/scripts/tests/test_hooks.py @@ -234,6 +234,26 @@ class TurnEndNudgeTest(RepositoryTestCase): self.bash(command) self.assertIsNone(self.stop()) + def test_reading_verify_sh_or_a_dry_run_does_not_count(self) -> None: + # 1112/08-09. Only a command that runs a desktop step clears the reminder. + for command in ("scripts/verify.sh merge --dry-run", + "sed -n 1,80p scripts/verify.sh && ls", + "grep -n foo scripts/verify.sh config/ci/gates.json", + "git log -- scripts/verify.sh docs/work", + "ssh desktop 'cat /tmp/claude-gates/x/FE-GATE-005.log | grep vitest'"): + with self.subTest(command=command): + self.edit("src/domain/order.ts", "export const a = 5;\n") + self.bash(command) + self.assertIsNotNone(self.stop()) + + def test_a_wrapped_desktop_step_counts(self) -> None: + for command in ("timeout 900 scripts/verify.sh exec --log x -- node scripts/run-vitest.ts run tests/unit/a.test.ts", + "VERIFY_SNAPSHOT=1 scripts/verify.sh merge --log r9"): + with self.subTest(command=command): + self.edit("src/domain/order.ts", "export const a = 6;\n") + self.bash(command) + self.assertIsNone(self.stop()) + def test_laptop_commands_and_other_hosts_do_not_count(self) -> None: for command in ("corepack pnpm lint", "ssh build-box 'corepack pnpm lint'", "scripts/verify.sh docs", "scripts/verify.sh hooks", "scripts/verify.sh gate FE-GATE-005 --on here",