claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[FEATURE] Add pre-compaction hook to preserve conversation history before auto-compacting

Open banifou opened this issue 1 month ago • 1 comments

Preflight Checklist

  • [x] I have searched existing requests and this feature hasn't been requested yet
  • [x] This is a single feature request (not multiple features)

Problem Statement

Feature Request

Add a preCompaction hook that triggers before auto-compacting begins, allowing users to run custom commands to preserve or export their conversation history.

Problem

Auto-compaction often starts unexpectedly during an active coding session. Once compaction completes, the detailed conversation history is summarized and the original context is lost. There's currently no way to:

  1. Get notified before compaction starts
  2. Automatically preserve the full transcript before it's compacted
  3. Take any preparatory action before context is summarized

Proposed Solution

Add a new hook type preCompaction (or PreCompact) to the hooks system:

  {
    "hooks": {
      "PreCompact": [
        {
          "matcher": "",
          "hooks": [
            {
              "type": "command",
              "command": "claude-code-transcripts local --latest -o ~/transcripts/ -a"
            }
          ]
        }
      ]
    }
  }

Use Cases

  1. Automatic transcript archiving - Export the full conversation to HTML/JSON before compaction using tools like https://github.com/simonw/claude-code-transcripts
  2. Backup critical context - Save important information that might be lost in summarization
  3. Notification - Simply notify the user that compaction is about to begin so they can prepare
  4. Logging/Analytics - Track session statistics before compaction for personal productivity analysis

Additional Context

The existing hook system (PreToolUse, PostToolUse, Notification, Stop) provides a great pattern for this. A PreCompact hook would fit naturally into this architecture.

Ideally, the hook would:

  • Fire before compaction begins
  • Include context like session ID, message count, or estimated context size
  • Allow blocking (with timeout) so the export can complete before compaction starts

Alternative Solutions

Until now, I had to laboriously copy and paste it into Markdown files.

Priority

High - Significant impact on productivity

Feature Category

Documentation

Use Case Example

1. Preserving Solution Alternatives

During problem-solving, Claude often presents multiple approaches with detailed trade-off analysis:

Here are three ways to implement caching: 1. Redis - best for distributed systems, adds infrastructure complexity 2. In-memory LRU - simple, but lost on restart 3. SQLite - persistent, single-node only

Given your requirements, I recommend...

This comparative analysis is invaluable documentation for future architectural decisions, but gets lost during compaction.

2. Debugging Session Documentation

Complex debugging sessions contain a wealth of knowledge:

  • Initial hypotheses that were tested and rejected
  • The investigative steps taken (logs checked, code paths traced)
  • Root cause analysis and explanation
  • The reasoning behind the final fix

This forms excellent documentation for similar future bugs or onboarding new team members.

3. Learning & Knowledge Capture

When exploring unfamiliar territory, Claude provides detailed explanations:

  • How a library works internally
  • Why certain patterns are preferred
  • Security implications of different approaches
  • Performance characteristics and benchmarks

These explanations are often more tailored and practical than official documentation.

4. Decision Audit Trail

For compliance or team documentation:

  • Why a specific library was chosen over alternatives
  • Security considerations that were evaluated
  • Trade-offs that were accepted and why
  • Requirements that influenced the design

5. Code Review Insights

During refactoring or review sessions:

  • Explanations of code smells and why they matter
  • Before/after comparisons with reasoning
  • Best practices specific to your codebase
  • Suggested improvements that weren't implemented yet (for future reference)

6. Research & Prototyping Sessions

When evaluating new technologies:

  • Comparison of different tools/frameworks
  • Proof-of-concept code that was explored
  • Limitations discovered during experimentation
  • Recommendations for or against adoption

Additional Context

No response

banifou avatar Dec 31 '25 19:12 banifou