Files
planning-with-files/tests/test_check_complete_resolver.py
T
OthmanAdi 3e35f428f3 release: v2.40.0 (slug-mode resolution fix + perf cache + KV-cache hygiene + Pi false-positive fix)
Bug fixes (items 1-5 in proposal_v2_40.md):

1. Hook resolution order inverted across UserPromptSubmit, PreToolUse, PreCompact.
   Slug-mode now wins over legacy root task_plan.md when both exist. Closes the
   bug where an active .planning/<slug>/ plan was silently bypassed in favor of
   the root file. Resolution chain: PLAN_ID env > .planning/.active_plan >
   newest .planning/<slug>/ by mtime > root fallback.

2. .active_plan target dir is validated. Dead pointers fall through cleanly.

3. .active_plan content is regex-validated as ^[A-Za-z0-9_][A-Za-z0-9._-]*$.
   Whitespace-only and path-traversal corruption fall through instead of
   producing weird path lookups. Tests cover whitespace, ../escape, and
   leading-dot cases.

4. check-complete.sh wires into resolve-plan-dir.sh when no path arg given.
   Restores slug-mode parity for any caller that invokes the script without an
   explicit plan path.

5. Pi extension dangerous-command detection uses word-boundary regex. Benign
   git push origin <branch> no longer triggers the warning; only --force,
   --mirror, +ref, plus rm -rf, sudo, chmod 777/a+rwx, git reset --hard, git
   clean -fd, fork bombs, dd to /dev/[sh]d[a-z].

Performance (items 6, 7):

6. mtime-keyed SHA-256 cache under ${TMPDIR:-/tmp}/pwf-sha cuts redundant hash
   work on warm cache. Caches the result keyed by absolute plan-file path,
   stores mtime + hash, skips sha256sum when mtime unchanged.

7. KV-cache hygiene on injected progress.md tail. Sub-second timestamps and
   timezone-suffix forms are normalized to a stable epoch-zero form so the
   model's prefix cache stays warm across turns. Manus-aligned.

Portability + races (items 19, 20):

19. resolve-plan-dir.sh portable mtime fallback chain: GNU stat -> BSD stat ->
    date -r -> python3 -> python -> perl -> 0. Multi-plan workflows stop
    silently degrading on non-GNU systems.

20. attest-plan.sh uses atomic temp+rename with optional flock guard.
    Concurrent legacy-mode attestations stop producing truncated files that
    triggered false [PLAN TAMPERED] warnings.

Tests: 130 pass / 2 pre-existing Windows exec-bit failures unchanged. +20 new
tests vs v2.39.0. See CHANGELOG for full inventory.

Version bumped to 2.40.0 across 14 SKILL.md variants + plugin.json +
marketplace.json + CITATION.cff via scripts/bump-version.py. .continue,
.gemini, .pi, .kiro lag intentionally.
2026-05-21 23:07:55 +02:00

144 lines
5.4 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 os
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"
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,
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)
if __name__ == "__main__":
unittest.main()