Files
planning-with-files/tests/test_check_complete_resolver.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

242 lines
9.9 KiB
Python

"""Tests for scripts/check-complete.sh resolver integration (v2.40).
Before v2.40, check-complete.sh defaulted to `./task_plan.md` when invoked
without arguments. Any caller running in pure-slug-mode (no root plan, only
`.planning/<slug>/task_plan.md` + `.active_plan`) would receive the
"No task_plan.md found" message even though an active plan existed.
The Stop hook in SKILL.md frontmatter passes the resolved plan path
explicitly, so this was silent: only user-driven invocations or third-party
tooling that called check-complete with no args hit the bug.
v2.40 wires check-complete.sh into resolve-plan-dir.sh when no explicit path is
passed, restoring slug-mode parity.
"""
from __future__ import annotations
import json
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
CHECK_COMPLETE = REPO_ROOT / "scripts" / "check-complete.sh"
CANONICAL_TEMPLATES = REPO_ROOT / "skills" / "planning-with-files" / "templates"
ROOT_TEMPLATES = REPO_ROOT / "templates"
# Both canonical plan templates ship 5 phases: Phase 1 in_progress, 2-5 pending.
TEMPLATE_NAMES = ("task_plan.md", "task_plan_autonomous.md")
PLAN_WITH_FIVE_PHASES = """# Task Plan: Smoke
## Phases
### Phase 1
- **Status:** in_progress
### Phase 2
- **Status:** pending
### Phase 3
- **Status:** pending
### Phase 4
- **Status:** pending
### Phase 5
- **Status:** pending
"""
PLAN_ALL_COMPLETE = """# Task Plan: Done
## Phases
### Phase 1
- **Status:** complete
### Phase 2
- **Status:** complete
"""
class CheckCompleteResolverTests(unittest.TestCase):
def run_check(self, cwd: Path, plan_id: str | None = None, arg: str | None = None) -> subprocess.CompletedProcess[str]:
env = os.environ.copy()
env.pop("PLAN_ID", None)
if plan_id is not None:
env["PLAN_ID"] = plan_id
cmd = ["sh", str(CHECK_COMPLETE)]
if arg is not None:
cmd.append(arg)
return subprocess.run(
cmd,
cwd=str(cwd),
text=True,
encoding="utf-8",
capture_output=True,
env=env,
check=False,
)
def test_explicit_path_arg_still_works(self) -> None:
# Backward compat: passing the plan-file path directly bypasses the
# resolver and operates on that file. The Stop hook in SKILL.md does
# this; the contract must not change.
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "task_plan.md").write_text(PLAN_WITH_FIVE_PHASES, encoding="utf-8")
result = self.run_check(root, arg="task_plan.md")
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("0/5 phases complete", result.stdout)
def test_no_args_resolves_slug_plan_via_active_pointer(self) -> None:
# Regression for v2.40: with only .planning/<slug>/task_plan.md and an
# .active_plan pointer, no-args invocation must resolve the slug plan
# instead of falling back to "no task_plan.md".
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
plan_dir = root / ".planning" / "2026-05-21-smoke"
plan_dir.mkdir(parents=True)
(plan_dir / "task_plan.md").write_text(PLAN_WITH_FIVE_PHASES, encoding="utf-8")
(root / ".planning" / ".active_plan").write_text("2026-05-21-smoke\n", encoding="utf-8")
result = self.run_check(root)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("0/5 phases complete", result.stdout)
self.assertNotIn("No task_plan.md found", result.stdout)
def test_no_args_resolves_via_plan_id_env(self) -> None:
# PLAN_ID env takes precedence over .active_plan in the resolver. The
# check-complete script should honor that exact chain.
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
alpha = root / ".planning" / "alpha"
beta = root / ".planning" / "beta"
alpha.mkdir(parents=True)
beta.mkdir(parents=True)
(alpha / "task_plan.md").write_text(PLAN_ALL_COMPLETE, encoding="utf-8")
(beta / "task_plan.md").write_text(PLAN_WITH_FIVE_PHASES, encoding="utf-8")
(root / ".planning" / ".active_plan").write_text("beta\n", encoding="utf-8")
# PLAN_ID env should override .active_plan, pointing at alpha (all complete).
result = self.run_check(root, plan_id="alpha")
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("ALL PHASES COMPLETE", result.stdout)
def test_no_args_legacy_root_plan_still_works(self) -> None:
# Backward compat: when no slug-mode plans exist but a root-level
# task_plan.md does, the resolver returns empty and we fall back to the
# legacy root path. v1.x users keep working.
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "task_plan.md").write_text(PLAN_WITH_FIVE_PHASES, encoding="utf-8")
result = self.run_check(root)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("0/5 phases complete", result.stdout)
def test_no_args_no_plan_anywhere_clean_message(self) -> None:
# If no plan exists in either location, the script must say so and exit
# 0 (Stop hook contract).
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
result = self.run_check(root)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("No task_plan.md found", result.stdout)
class TemplateNextStepTests(unittest.TestCase):
"""v3.8.0: plan templates gain a '## Next Step' section right after '## Goal'.
check-complete.sh derives phase totals from '### Phase' headings, status
counts from '**Status:** ...' lines, and the gate's in_progress phase name
from the first '### ' heading above an in_progress status. The new section
is a '##' heading plus one bracketed placeholder line, so none of those
patterns may shift. These tests run the real script against the real
templates to pin that down.
"""
def template_body(self, name: str) -> str:
return (CANONICAL_TEMPLATES / name).read_text(encoding="utf-8")
def run_check_on_template(self, name: str, root: Path, *, gate: bool = False) -> subprocess.CompletedProcess[str]:
shutil.copyfile(CANONICAL_TEMPLATES / name, root / "task_plan.md")
cmd = ["sh", str(CHECK_COMPLETE)]
if gate:
cmd.append("--gate")
cmd.append("task_plan.md")
return subprocess.run(
cmd,
cwd=str(root),
text=True,
encoding="utf-8",
capture_output=True,
input=json.dumps({"stop_hook_active": False}),
check=False,
)
def test_canonical_templates_contain_next_step(self) -> None:
for name in TEMPLATE_NAMES:
body = self.template_body(name)
self.assertIn("## Next Step", body, name)
self.assertIn(
"[The single next action. Update whenever phase status changes.]",
body,
name,
)
def test_root_template_copy_contains_next_step(self) -> None:
# templates/task_plan.md is a manually maintained copy; sync-ide-folders
# does not manage the repo-root templates dir. The autonomous template
# deliberately has no root copy.
body = (ROOT_TEMPLATES / "task_plan.md").read_text(encoding="utf-8")
self.assertIn("## Next Step", body)
def test_next_step_sits_between_goal_and_current_phase(self) -> None:
# Placement contract: directly after ## Goal, so the section rides
# inside the head-30/head-50 hook injections.
for name in TEMPLATE_NAMES:
body = self.template_body(name)
goal = body.index("## Goal")
next_step = body.index("## Next Step")
current = body.index("## Current Phase")
self.assertLess(goal, next_step, name)
self.assertLess(next_step, current, name)
def test_template_phase_counts_unchanged(self) -> None:
# 5 phases, 0 complete, 1 in_progress, 4 pending, with the new section
# present. A count shift here means the section leaked a parse token.
for name in TEMPLATE_NAMES:
with tempfile.TemporaryDirectory() as tmp:
result = self.run_check_on_template(name, Path(tmp))
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("0/5 phases complete", result.stdout, name)
self.assertIn("1 phase(s) still in progress", result.stdout, name)
self.assertIn("4 phase(s) pending", result.stdout, name)
def test_gate_extracts_phase_1_as_in_progress(self) -> None:
# in_progress extraction: the gate names the first in_progress phase in
# its block reason. '## Next Step' is a '##' heading, so the awk pass
# tracking '### ' headings must still land on Phase 1.
for name in TEMPLATE_NAMES:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / ".mode").write_text("gate\n", encoding="utf-8")
result = self.run_check_on_template(name, root, gate=True)
self.assertEqual(0, result.returncode, result.stderr)
decision_lines = [
ln for ln in result.stdout.splitlines() if ln.startswith("{")
]
self.assertEqual(1, len(decision_lines), result.stdout)
decision = json.loads(decision_lines[0])
self.assertEqual("block", decision["decision"], name)
self.assertIn(
"Phase 1: Requirements & Discovery", decision["reason"], name
)
if __name__ == "__main__":
unittest.main()