4.3 KiB
Plan: Session Summarization Hook
Problem
Sessions are tracked in ~/.claude/state/personal-assistant/history/index.json but:
- No conversation logs are captured to our history folder
- Sessions never get marked as summarized
- Memory files remain empty (decisions, preferences, projects, facts)
Root Cause
Missing SessionEnd hook to trigger summarization when sessions end.
Solution
Add a SessionEnd hook that:
- Reads the transcript from Claude's built-in storage (
transcript_path) - Extracts key information (decisions, preferences, project context, facts)
- Saves to appropriate memory files
- Updates history index to mark session as summarized
Files to Modify
| File | Action |
|---|---|
~/.claude/hooks/hooks.json |
Add SessionEnd hook entry |
~/.claude/hooks/scripts/session-end.sh |
Create - orchestrates summarization |
~/.claude/hooks/scripts/summarize-transcript.py |
Create - Python script to process transcript |
Implementation Details
1. Hook Configuration (hooks.json)
Add SessionEnd hook that calls the summarization script:
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/scripts/session-end.sh",
"timeout": 120
}
]
}
]
2. Session End Script (session-end.sh)
- Receives JSON via stdin with
session_id,transcript_path,reason - Calls Python summarization script
- Handles errors gracefully (session end shouldn't fail)
3. Summarization Script (summarize-transcript.py)
The script will:
- Parse transcript - Read the
.jsonlfile fromtranscript_path - Extract key items - Use heuristics to identify:
- Decisions: "let's use", "we decided", "I'll go with"
- Preferences: "I prefer", "always", "never", "I like"
- Project context: file paths, config references
- Facts: environment info, tool locations
- Deduplicate - Check against existing memory items
- Save to memory files - Append new items with UUIDs
- Update history index - Mark session as summarized, add topics
Processing Approach: Hybrid (Decision)
Step 1: Threshold check
- Skip sessions with < 3 user messages
- Skip sessions that are only quick commands (no substantive discussion)
Step 2: Heuristic extraction (fast, no API)
- File paths mentioned → project context
- Environment facts (tool locations, versions)
- Simple preferences with clear keywords
Step 3: LLM extraction (if substantive content)
- Complex decisions with rationale
- Nuanced preferences
- Project context requiring interpretation
- Use Claude API (Haiku for cost efficiency)
Transcript Storage (Decision)
Reference Claude's existing transcript location (~/.claude/projects/.../[uuid].jsonl) rather than copying to our history folder. The history index will store the transcript path for future reference.
Memory File Format
Each item:
{
"id": "uuid",
"date": "YYYY-MM-DD",
"content": "Brief description",
"context": "Additional context",
"session": "session-id"
}
Implementation Steps
Step 1: Update hooks.json
Add SessionEnd entry to ~/.claude/hooks/hooks.json
Step 2: Create session-end.sh
Shell wrapper at ~/.claude/hooks/scripts/session-end.sh:
- Parse JSON input from stdin
- Extract session_id, transcript_path, reason
- Call Python summarization script
- Handle errors silently (don't break session exit)
Step 3: Create summarize-transcript.py
Python script at ~/.claude/hooks/scripts/summarize-transcript.py:
Arguments: --session-id <id> --transcript <path> [--reason <reason>]
1. Load transcript (.jsonl)
2. Count user messages → skip if < 3
3. Heuristic pass:
- Extract file paths → projects.json
- Extract env facts → facts.json
4. If substantive content detected:
- Call Claude API (Haiku) for decisions/preferences
- Parse response → decisions.json, preferences.json
5. Update history/index.json:
- Set summarized: true
- Add transcript_path
- Add extracted topics
Step 4: Update history index schema
Add transcript_path field to session entries in history/index.json
Testing
- Start a test session with substantive discussion
- Exit session normally
- Verify:
- Hook fired (check with
--debug) - Memory files updated
- History index marked summarized
- Hook fired (check with