Files
planning-with-files/tests/test_stop_hook_dispatch.py
T
OthmanAdi b49ea488fb feat: make the Stop hook and session recovery fire everywhere, add long-run injection controls
The Stop scalar never dispatched on macOS or Linux (the PowerShell branch
was always selected because check-complete.ps1 ships on every platform)
and was a silent no-op anywhere CLAUDE_SKILL_DIR was unset (the :- install
path fallback can never substitute because the probed variable is always a
non-empty string). Both the legacy completion advisory and the v3
completion gate were dead in those environments. The scalar now selects
targets by file existence and dispatches by platform, PowerShell only
under MINGW/MSYS/CYGWIN, sh elsewhere. Windows output is unchanged.
Patched in all 14 SKILL.md variants that carry the scalar.

session-catchup probed a ~/.claude/projects name that Claude Code never
writes for POSIX paths (leading dash stripped) or paths containing an
underscore (replaced with a dash), so recovery after /clear silently
found nothing there. The mapper now probes the exact spelling first,
keeps both legacy spellings for stores created by older versions, and
settles ambiguity via the cwd recorded in the newest session file.
Propagated to every shipped copy including the older-generation root,
.hermes, and .mastracode scripts.

Also: the attestation SHA cache is keyed on the absolute plan path so two
projects can no longer share a slot and report a false PLAN TAMPERED;
resolve-plan-dir.ps1 reaches parity with the sh resolver (slug filter,
task_plan.md requirement in the newest-dir scan, fail-closed containment);
ledger-append.sh no longer truncates summaries mid-codepoint (UTF-8-safe
trim via iconv with a pure-sh fallback).

New long-run features, all opt-in or additive: structure-aware injection
(PWF_INJECT=smart or an inject-smart .mode token) keeps the active phase,
phase counts, and the last three decisions in the window late in long
plans; a Next Step section in both plan templates plus a sixth reboot
question; session-catchup annotates tool results (ok or FAILED with the
first error line) instead of only listing attempts.

Infrastructure: macos-latest joins the CI matrix, a BSD-userland
simulation harness runs the script fleet without realpath, readlink,
flock, or sha256sum on the Linux leg, and .gitattributes pins LF for
scripts. SEO surfaces: llms.txt rewritten as a Q&A page, three
problem-query docs pages, plugin.json and CITATION.cff keyword hygiene.

Suite grows from 217 to 301 passed, 11 skipped, on Windows and both
existing CI legs; default hook output stays byte-identical to v2.43
(legacy invariant proven by the existing invariant tests).
2026-07-21 21:17:19 +02:00

281 lines
11 KiB
Python

"""Stop-hook dispatch tests (v3.8.0).
The Stop scalar in SKILL.md frontmatter is the dispatcher for the completion
advisory (legacy) and the v3 completion gate. Before v3.8.0 it had two silent
failure modes that these tests pin forever:
1. Dead fallback: ``TARGET_PS1="${SKILL_PS1:-$KNOWN_PS1}"`` never substituted
because ``SKILL_PS1="${CLAUDE_SKILL_DIR}/scripts/check-complete.ps1"`` is a
non-empty string even when ``CLAUDE_SKILL_DIR`` is unset. With the env var
unset the whole hook was a silent no-op even with the skill installed at a
known path.
2. ps1-first on every platform: ``check-complete.ps1`` ships in the skill dir
on all platforms, so the PowerShell branch was always chosen and
``powershell.exe ... 2>/dev/null`` silently did nothing on macOS/Linux
(exit 127, stderr discarded). The POSIX ``gate-stop.sh`` branch was
unreachable: the completion gate never fired on macOS or Linux.
The v3.8.0 scalar selects by file existence (``[ -f ] || ls-fallback``, the
same pattern the other hooks use) and dispatches by platform: PowerShell only
on native Windows (uname MINGW*/MSYS*/CYGWIN*), ``sh`` elsewhere.
These tests EXECUTE the scalar end-to-end, which the pre-v3.8.0 suite never
did (it only string-matched the scalar shape).
"""
from __future__ import annotations
import os
import re
import shutil
import stat
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
CANONICAL_SKILL = REPO_ROOT / "skills" / "planning-with-files" / "SKILL.md"
SKILL_DIR = REPO_ROOT / "skills" / "planning-with-files"
# Every SKILL.md that carries a Stop scalar (canonical + language variants +
# IDE mirrors + clawhub upload copy). Kept in sync with the parity surfaces.
ALL_STOP_SKILL_FILES = [
REPO_ROOT / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / "skills" / "planning-with-files-ar" / "SKILL.md",
REPO_ROOT / "skills" / "planning-with-files-de" / "SKILL.md",
REPO_ROOT / "skills" / "planning-with-files-es" / "SKILL.md",
REPO_ROOT / "skills" / "planning-with-files-zh" / "SKILL.md",
REPO_ROOT / "skills" / "planning-with-files-zht" / "SKILL.md",
REPO_ROOT / ".agents" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".codebuddy" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".codex" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".cursor" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".factory" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".mastracode" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / ".opencode" / "skills" / "planning-with-files" / "SKILL.md",
REPO_ROOT / "clawhub-upload" / "SKILL.md",
]
HOOK_RE = r'Stop:\n(?:.*?\n)*?\s*command: "((?:[^"\\]|\\.)*)"'
IS_WINDOWS = sys.platform == "win32"
def extract_stop_scalar(skill_file: Path) -> str:
text = skill_file.read_text(encoding="utf-8")
match = re.search(HOOK_RE, text)
assert match, f"Stop hook scalar not found in {skill_file}"
raw = match.group(1)
return raw.replace('\\"', '"').replace("\\\\", "\\")
def have_sh() -> bool:
return shutil.which("sh") is not None
def run_scalar(
scalar: str,
cwd: Path,
env_overrides: dict,
stdin_data: str = "",
drop_vars: tuple = (),
) -> subprocess.CompletedProcess:
env = os.environ.copy()
for var in drop_vars:
env.pop(var, None)
env.update(env_overrides)
return subprocess.run(
["sh", "-c", scalar],
cwd=str(cwd),
env=env,
input=stdin_data,
capture_output=True,
text=True,
timeout=60,
)
def make_plan(tmp: Path) -> None:
(tmp / "task_plan.md").write_text(
"# Task Plan: dispatch test\n\n"
"### Phase 1: Verify\n"
"- [ ] run the hook\n"
"- **Status:** in_progress\n",
encoding="utf-8",
)
@unittest.skipUnless(have_sh(), "requires a POSIX sh")
class StopScalarShapeTests(unittest.TestCase):
"""Static shape of every Stop scalar in the fleet."""
def test_no_dead_colon_dash_fallback(self) -> None:
# "${SKILL_PS1:-$KNOWN_PS1}" can never substitute: SKILL_PS1 is a
# non-empty string even with CLAUDE_SKILL_DIR unset. The v3.8.0 form
# selects by file existence instead.
for skill_file in ALL_STOP_SKILL_FILES:
scalar = extract_stop_scalar(skill_file)
self.assertNotIn(
":-$KNOWN",
scalar,
f"{skill_file}: dead ':-' fallback pattern present; "
"existence-based selection required",
)
def test_platform_gated_dispatch(self) -> None:
# PowerShell must be chosen only on native Windows shells; POSIX gets
# the sh path first.
for skill_file in ALL_STOP_SKILL_FILES:
scalar = extract_stop_scalar(skill_file)
self.assertIn("uname", scalar, f"{skill_file}: no platform gate")
self.assertIn("MINGW", scalar, f"{skill_file}: no MINGW match")
def test_probes_both_install_paths(self) -> None:
for skill_file in ALL_STOP_SKILL_FILES:
scalar = extract_stop_scalar(skill_file)
self.assertIn(".claude/skills/planning-with-files", scalar)
self.assertIn(".claude/plugins/marketplaces/planning-with-files", scalar)
def test_no_yaml_delimiter_collision(self) -> None:
# A literal --- inside a hook scalar corrupts frontmatter parsing
# (Discussion #153 class).
for skill_file in ALL_STOP_SKILL_FILES:
scalar = extract_stop_scalar(skill_file)
self.assertNotIn("---", scalar, f"{skill_file}: '---' in scalar")
def test_exits_zero_explicitly(self) -> None:
for skill_file in ALL_STOP_SKILL_FILES:
scalar = extract_stop_scalar(skill_file)
self.assertTrue(
scalar.rstrip().endswith("exit 0"),
f"{skill_file}: scalar must end with exit 0",
)
@unittest.skipUnless(have_sh(), "requires a POSIX sh")
class StopScalarBehaviorTests(unittest.TestCase):
"""Execute the canonical scalar end-to-end."""
def setUp(self) -> None:
self.tmp = Path(tempfile.mkdtemp(prefix="pwf-stop-"))
make_plan(self.tmp)
def tearDown(self) -> None:
shutil.rmtree(self.tmp, ignore_errors=True)
def test_advisory_fires_with_skill_dir_set(self) -> None:
# The day-one regression: on macOS/Linux this produced NO output
# because the ps1 branch swallowed the dispatch.
scalar = extract_stop_scalar(CANONICAL_SKILL)
result = run_scalar(
scalar, self.tmp, {"CLAUDE_SKILL_DIR": str(SKILL_DIR)}
)
self.assertEqual(result.returncode, 0)
self.assertIn(
"[planning-with-files]",
result.stdout,
"Stop hook produced no advisory output with an in_progress plan "
f"(stderr: {result.stderr!r})",
)
def test_fires_when_skill_dir_unset(self) -> None:
# The dead-fallback regression: with CLAUDE_SKILL_DIR unset the
# ls-discovered install path must be used.
fake_home = self.tmp / "home"
stub_scripts = fake_home / ".claude" / "skills" / "planning-with-files" / "scripts"
stub_scripts.mkdir(parents=True)
for name in (
"gate-stop.sh",
"check-complete.sh",
"check-complete.ps1",
"resolve-plan-dir.sh",
):
src = SKILL_DIR / "scripts" / name
dst = stub_scripts / name
shutil.copy2(src, dst)
dst.chmod(dst.stat().st_mode | stat.S_IEXEC)
scalar = extract_stop_scalar(CANONICAL_SKILL)
env = {"HOME": str(fake_home)}
if IS_WINDOWS:
# Git Bash maps $HOME from HOME when set; USERPROFILE is the
# Windows-native twin some layers consult.
env["USERPROFILE"] = str(fake_home)
result = run_scalar(
scalar,
self.tmp,
env,
drop_vars=("CLAUDE_SKILL_DIR",),
)
self.assertEqual(result.returncode, 0)
self.assertIn(
"[planning-with-files]",
result.stdout,
"Stop hook silent with CLAUDE_SKILL_DIR unset despite a stub "
f"install under $HOME (stderr: {result.stderr!r})",
)
@unittest.skipIf(IS_WINDOWS, "POSIX-only dispatch preference")
def test_posix_prefers_sh_over_powershell(self) -> None:
# Even with a powershell.exe on PATH (e.g. PowerShell Core on Linux),
# the POSIX branch must dispatch the sh gate, not the ps1.
bindir = self.tmp / "bin"
bindir.mkdir()
sentinel = self.tmp / "ps1-ran"
fake_ps = bindir / "powershell.exe"
fake_ps.write_text(f"#!/bin/sh\ntouch '{sentinel}'\n", encoding="utf-8")
fake_ps.chmod(0o755)
scalar = extract_stop_scalar(CANONICAL_SKILL)
env = {
"CLAUDE_SKILL_DIR": str(SKILL_DIR),
"PATH": f"{bindir}{os.pathsep}{os.environ.get('PATH', '')}",
}
result = run_scalar(scalar, self.tmp, env)
self.assertEqual(result.returncode, 0)
self.assertIn("[planning-with-files]", result.stdout)
self.assertFalse(
sentinel.exists(),
"POSIX dispatch ran powershell.exe instead of the sh gate",
)
@unittest.skipIf(IS_WINDOWS, "gate JSON path exercised via sh on POSIX")
def test_gated_mode_emits_block_json(self) -> None:
(self.tmp / ".mode").write_text("autonomous gate\n", encoding="utf-8")
scalar = extract_stop_scalar(CANONICAL_SKILL)
result = run_scalar(
scalar,
self.tmp,
{"CLAUDE_SKILL_DIR": str(SKILL_DIR)},
stdin_data='{"stop_hook_active": false}',
)
self.assertEqual(result.returncode, 0)
self.assertIn(
'"decision":"block"',
result.stdout,
f"gated mode did not block (stdout: {result.stdout!r})",
)
def test_variant_scalar_fires_end_to_end(self) -> None:
# Group-B scalars (language variants + IDE mirrors) dispatch to
# check-complete.sh; one representative execution proves the shape.
# (.codebuddy ships its own scripts/; .cursor relies on the install-path
# fallback and cannot be executed hermetically here.)
variant = REPO_ROOT / ".codebuddy" / "skills" / "planning-with-files" / "SKILL.md"
scalar = extract_stop_scalar(variant)
variant_skill_dir = REPO_ROOT / ".codebuddy" / "skills" / "planning-with-files"
result = run_scalar(
scalar, self.tmp, {"CLAUDE_SKILL_DIR": str(variant_skill_dir)}
)
self.assertEqual(result.returncode, 0)
self.assertIn(
"[planning-with-files]",
result.stdout,
f"variant Stop scalar silent (stderr: {result.stderr!r})",
)
if __name__ == "__main__":
unittest.main()