Files
planning-with-files/tests/test_precompact_hook.py
T
OthmanAdi 2d089130a9 release: v2.38.0 (Claude Code turn-loop integration + OpenCode SQLite fix)
PreCompact hook fires on /compact and autoCompact. When task_plan.md is
present, surfaces a reminder to flush in-context progress to progress.md
before compaction completes and prints the active Plan-SHA256 if attested.
Stays silent without a plan. Exit 0 always; never blocks compaction.

/plan-goal composes with Claude Code's /goal (v2.1.139, May 12 2026).
Derives a termination condition from the active plan so /goal stops the
agent when the plan-file is genuinely complete, not just when the
conversation looks done.

/plan-loop composes with /loop (v2.1.72+). Default 10-minute tick re-reads
planning files, runs check-complete, nudges a progress.md entry if the
last tick produced no change. templates/loop.md ships a planning-aware
default for ~/.claude/loop.md so bare /loop works grounded in the plan.

OpenCode session-catchup rewritten for the SQLite migration (sst/opencode
dev @ 2026-05-14). Reads ${XDG_DATA_HOME:-~/.local/share}/opencode/opencode.db
read-only via URI, scopes by session.directory, surfaces unsynced planning
edits. Verified against a real 162 MB OpenCode database (94 sessions, 56
unsynced parts correctly extracted from the lovable-at-home directory).

Codex gains a PermissionRequest adapter that surfaces a one-line plan
reminder when an active plan exists. Read-only; never blocks.

Tests: +19 covering PreCompact behavior, slash command file structure,
OpenCode SQLite catchup with a synthetic schema-matching DB, and Codex
PermissionRequest emit/silent behavior. 98 tests passing.

Deliberately not changed: paths glob (auto-invocation behavior risk),
wholesale !command swap (would freeze attestation hash and defeat v2.37
tamper gate), native Plan Mode panel integration (no plugin API
documented), language variant consolidation (separate breaking-change
track). PreCompact lives in the canonical SKILL.md + 5 language variants
+ clawhub-upload; other IDE mirrors keep their existing compaction
hooks until per-IDE PreCompact adapters land in a future release.
2026-05-14 19:26:17 +02:00

117 lines
4.2 KiB
Python

"""Regression tests for the PreCompact hook (v2.38.0).
PreCompact fires on Claude Code's autoCompact and manual /compact. The hook
re-injects a planning reminder before context compaction. It must:
- Be declared in the canonical SKILL.md frontmatter.
- Match all triggers (manual and auto).
- Print a reminder when task_plan.md exists.
- Stay silent when task_plan.md is absent.
- Surface the Plan-SHA256 hash when an attestation is set.
"""
from __future__ import annotations
import os
import re
import shutil
import subprocess
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"
def extract_precompact_command(text: str) -> str:
"""Pull the PreCompact hook command string out of SKILL.md frontmatter.
Frontmatter format:
PreCompact:
- matcher: "*"
hooks:
- type: command
command: "..."
"""
in_section = False
for line in text.splitlines():
stripped = line.strip()
if stripped == "PreCompact:":
in_section = True
continue
if in_section and stripped.startswith("command:"):
m = re.match(r'command:\s*"(.+)"\s*$', stripped)
if m:
return m.group(1).replace('\\"', '"')
if in_section and stripped.endswith(":") and not stripped.startswith("-") and stripped != "hooks:":
# Next top-level frontmatter key starts a new section.
break
return ""
class PreCompactHookDeclarationTests(unittest.TestCase):
def setUp(self) -> None:
self.text = CANONICAL_SKILL.read_text(encoding="utf-8")
def test_precompact_is_declared(self) -> None:
self.assertIn("PreCompact:", self.text, "PreCompact hook missing from canonical SKILL.md")
def test_precompact_uses_wildcard_matcher(self) -> None:
# We want both autoCompact and manual /compact to fire the reminder.
self.assertRegex(
self.text,
r"PreCompact:\s*\n\s*-\s*matcher:\s*\"\*\"",
"PreCompact should match all triggers ('*'), got something stricter",
)
@unittest.skipUnless(shutil.which("sh"), "sh not available on this platform")
class PreCompactCommandBehaviorTests(unittest.TestCase):
def setUp(self) -> None:
self.tmp = Path(tempfile.mkdtemp(prefix="pwf-precompact-"))
self.cmd = extract_precompact_command(CANONICAL_SKILL.read_text(encoding="utf-8"))
self.assertTrue(self.cmd, "Could not extract PreCompact command from SKILL.md")
def tearDown(self) -> None:
shutil.rmtree(self.tmp, ignore_errors=True)
def _run(self) -> subprocess.CompletedProcess:
return subprocess.run(
["sh", "-c", self.cmd],
cwd=str(self.tmp),
text=True,
capture_output=True,
check=False,
)
def test_silent_when_no_plan(self) -> None:
result = self._run()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual("", result.stdout.strip(), "PreCompact should be silent without task_plan.md")
def test_emits_reminder_when_plan_exists(self) -> None:
(self.tmp / "task_plan.md").write_text("# Plan\n", encoding="utf-8")
result = self._run()
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("[planning-with-files] PreCompact", result.stdout)
self.assertIn("progress.md", result.stdout, "reminder must mention progress.md")
def test_emits_plan_sha256_when_legacy_attestation_set(self) -> None:
(self.tmp / "task_plan.md").write_text("# Plan\n", encoding="utf-8")
(self.tmp / ".plan-attestation").write_text(
"abc123def456" + "0" * 52 + "\n", encoding="utf-8"
)
result = self._run()
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("Plan-SHA256", result.stdout)
def test_no_sha256_line_when_no_attestation(self) -> None:
(self.tmp / "task_plan.md").write_text("# Plan\n", encoding="utf-8")
result = self._run()
self.assertEqual(0, result.returncode, result.stderr)
self.assertNotIn("Plan-SHA256", result.stdout)
if __name__ == "__main__":
unittest.main()