Skip to content

feat: pre-compact transcript saver + hook cleanup#83

Merged
azalio merged 3 commits intomainfrom
fix/pre-compact-hook-path
Mar 8, 2026
Merged

feat: pre-compact transcript saver + hook cleanup#83
azalio merged 3 commits intomainfrom
fix/pre-compact-hook-path

Conversation

@azalio
Copy link
Owner

@azalio azalio commented Mar 8, 2026

Summary

  • New hook: pre-compact-save-transcript.py — saves full conversation transcript to .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md before context compaction (both manual and auto)
  • Registered: ralph-iteration-logger.py as PostToolUse hook (was dead code, detects file thrashing)
  • Removed dead code: block-secrets.py, block-dangerous.sh, post-edit-reminder.py, improve-prompt.py — all superseded by safety-guardrails.py or duplicating workflow-context-injector.py
  • Net: -629 lines, 7 active hooks (was 4 active + 4 dead)

Hook audit results

Hook Event Status
safety-guardrails.py PreToolUse Kept (security)
workflow-gate.py PreToolUse Kept (MAP enforcement)
workflow-context-injector.py PreToolUse Kept (MAP reminders)
ralph-iteration-logger.py PostToolUse Registered (thrashing detection)
ralph-context-pruner.py PreCompact Kept (anti-amnesia)
pre-compact-save-transcript.py PreCompact New (transcript save)
end-of-turn.sh Stop Kept (auto-fix)

Test plan

  • pytest tests/test_template_sync.py -v — 24/24 passed
  • Manual test: hook produces valid markdown from real transcript
  • Template sync verified: .claude/ matches src/mapify_cli/templates/
  • Timing: ~30-45ms parallel overhead per tool call (unchanged)

- Add pre-compact-save-transcript.py: saves full conversation to
  .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md before compaction
- Register ralph-iteration-logger.py in PostToolUse (was dead code)
- Remove dead code hooks superseded by safety-guardrails.py:
  block-secrets.py, block-dangerous.sh, post-edit-reminder.py,
  improve-prompt.py
- Sync all changes to src/mapify_cli/templates/
Copilot AI review requested due to automatic review settings March 8, 2026 22:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a PreCompact transcript snapshot hook and cleans up hook registration by removing superseded/dead hook scripts, aligning both the project .claude/ config and the CLI templates.

Changes:

  • Register ralph-iteration-logger.py as an active PostToolUse hook (now includes Bash tool calls).
  • Add new pre-compact-save-transcript.py PreCompact hook to persist a readable markdown transcript snapshot under .map/<branch>/.
  • Remove obsolete hooks (block-secrets.py, block-dangerous.sh, post-edit-reminder.py, improve-prompt.py) from both .claude/hooks/ and template hooks, plus minor formatting cleanups in remaining hooks.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
.claude/settings.json Switch PostToolUse hook to iteration logger (incl. Bash) and register the new PreCompact transcript saver
.claude/hooks/pre-compact-save-transcript.py New hook to serialize JSONL transcripts into readable markdown before compaction
.claude/hooks/post-edit-reminder.py Deleted (superseded)
.claude/hooks/improve-prompt.py Deleted (superseded)
.claude/hooks/block-secrets.py Deleted (superseded by safety-guardrails)
.claude/hooks/block-dangerous.sh Deleted (superseded by safety-guardrails/settings permissions)
src/mapify_cli/templates/settings.json Template mirror of .claude/settings.json hook registrations
src/mapify_cli/templates/hooks/pre-compact-save-transcript.py Template mirror of the new transcript saver hook
src/mapify_cli/templates/hooks/workflow-gate.py Minor string formatting tweak (template sync)
src/mapify_cli/templates/hooks/safety-guardrails.py Minor formatting cleanup (template sync)
src/mapify_cli/templates/hooks/ralph-context-pruner.py Minor formatting cleanup (template sync)
src/mapify_cli/templates/hooks/post-edit-reminder.py Deleted (superseded)
src/mapify_cli/templates/hooks/improve-prompt.py Deleted (superseded)
src/mapify_cli/templates/hooks/block-secrets.py Deleted (superseded)
src/mapify_cli/templates/hooks/block-dangerous.sh Deleted (superseded)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +78 to 82
"matcher": "Edit|Write|MultiEdit|Bash",
"description": "Iteration Logger - logs tool calls, detects thrashing patterns",
"hooks": [
{
"type": "command",
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR removes block-secrets.py, block-dangerous.sh, and improve-prompt.py, but the repo still has tests/docs that reference those paths (e.g., tests/hooks/test_block_secrets.py, tests/hooks/test_block_dangerous.py, docs/USAGE.md, src/mapify_cli/templates/skills/README.md). As-is, running the full test suite will fail with missing-file errors and the documentation will be inaccurate; please update/remove those references (and port the relevant assertions to safety-guardrails.py if that’s now the single enforcement hook).

Copilot uses AI. Check for mistakes.
Pre-Compact Transcript Saver - PreCompact Hook.

Before context compaction, saves the full conversation transcript
to .map/<branch>-YYYY-MM-DD-HH-MM.md as readable markdown.
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring says transcripts are saved to .map/<branch>-YYYY-MM-DD-HH-MM.md, but the implementation writes to .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md. Please update the docstring to match the actual output path/pattern so users can find the saved files.

Suggested change
to .map/<branch>-YYYY-MM-DD-HH-MM.md as readable markdown.
to .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md as readable markdown.

Copilot uses AI. Check for mistakes.
Comment on lines +146 to +150
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M")

branch_dir = MAP_DIR / branch
branch_dir.mkdir(parents=True, exist_ok=True)
outfile = branch_dir / f"transcript-{timestamp}.md"
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timestamp is only minute-granular (%Y-%m-%d-%H-%M), so two compactions within the same minute can overwrite the previous transcript file. Consider including seconds (or adding a unique suffix when the path already exists) to avoid silent data loss.

Copilot uses AI. Check for mistakes.
"""Parse JSONL transcript into readable markdown."""
lines = []
try:
with open(transcript_path) as f:
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transcript JSONL is opened without an explicit encoding. On non-UTF-8 default locales (notably Windows), this can raise decode errors or corrupt output. Use encoding="utf-8" when reading the transcript file for consistent behavior.

Suggested change
with open(transcript_path) as f:
with open(transcript_path, encoding="utf-8") as f:

Copilot uses AI. Check for mistakes.
Pre-Compact Transcript Saver - PreCompact Hook.

Before context compaction, saves the full conversation transcript
to .map/<branch>-YYYY-MM-DD-HH-MM.md as readable markdown.
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring says transcripts are saved to .map/<branch>-YYYY-MM-DD-HH-MM.md, but the implementation writes to .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md. Please update the docstring to match the actual output path/pattern so users can find the saved files.

Suggested change
to .map/<branch>-YYYY-MM-DD-HH-MM.md as readable markdown.
to .map/<branch>/transcript-YYYY-MM-DD-HH-MM.md as readable markdown.

Copilot uses AI. Check for mistakes.
Comment on lines +146 to +150
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M")

branch_dir = MAP_DIR / branch
branch_dir.mkdir(parents=True, exist_ok=True)
outfile = branch_dir / f"transcript-{timestamp}.md"
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timestamp is only minute-granular (%Y-%m-%d-%H-%M), so two compactions within the same minute can overwrite the previous transcript file. Consider including seconds (or adding a unique suffix when the path already exists) to avoid silent data loss.

Copilot uses AI. Check for mistakes.
"""Parse JSONL transcript into readable markdown."""
lines = []
try:
with open(transcript_path) as f:
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transcript JSONL is opened without an explicit encoding. On non-UTF-8 default locales (notably Windows), this can raise decode errors or corrupt output. Use encoding="utf-8" when reading the transcript file for consistent behavior.

Suggested change
with open(transcript_path) as f:
with open(transcript_path, encoding="utf-8") as f:

Copilot uses AI. Check for mistakes.
Comment on lines +78 to 82
"matcher": "Edit|Write|MultiEdit|Bash",
"description": "Iteration Logger - logs tool calls, detects thrashing patterns",
"hooks": [
{
"type": "command",
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR removes block-secrets.py, block-dangerous.sh, and improve-prompt.py, but the repo still has tests/docs that reference those paths (e.g., tests/hooks/test_block_secrets.py, tests/hooks/test_block_dangerous.py, docs/USAGE.md, .claude/skills/README.md). As-is, running the full test suite will fail with missing-file errors and the documentation will be inaccurate; please update/remove those references (and port the relevant assertions to safety-guardrails.py if that’s now the single enforcement hook).

Copilot uses AI. Check for mistakes.
azalio added 2 commits March 9, 2026 01:39
…compaction

PreCompact hooks have no decision control per docs — additionalContext
is silently ignored. Move context injection to SessionStart with
matcher "compact", which properly supports additionalContext.

- New post-compact-context.py: reads last-transcript.txt pointer and
  restore_point.json, injects paths into Claude's context
- pre-compact-save-transcript.py: writes last-transcript.txt pointer
- ralph-context-pruner.py: remove dead additionalContext output
…ilot review comments

- Delete test_block_secrets.py and test_block_dangerous.py (tested removed hooks)
- Add test_safety_guardrails.py with 75 tests covering the merged hook
- Fix docstring path pattern in pre-compact-save-transcript.py
- Add seconds to timestamp to prevent same-minute overwrites
- Add encoding="utf-8" to transcript file open
@azalio azalio merged commit a21926d into main Mar 8, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants