Last active
February 6, 2026 18:12
-
-
Save ivorpad/96b7877ee4cdd57bf6011a139e2437d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bun | |
| /** | |
| * Pre-compaction context extractor. | |
| * Parses the Claude Code transcript JSONL, extracts key context | |
| * (user corrections, decisions, important state), and calls claude -p | |
| * to produce a structured summary saved to .claude/compact-context.md | |
| */ | |
| import { readFileSync, writeFileSync, mkdirSync, appendFileSync } from "fs"; | |
| import { join, dirname } from "path"; | |
| import { execSync } from "child_process"; | |
| const LOG = "/tmp/cc-compact-context.log"; | |
| const log = (msg: string) => { | |
| const ts = new Date().toISOString().slice(11, 19); | |
| appendFileSync(LOG, `[${ts}] ${msg}\n`); | |
| }; | |
| const transcriptPath = process.argv[2]; | |
| const cwd = process.argv[3]; | |
| if (!transcriptPath || !cwd) { | |
| log("ERROR: missing args (transcript_path, cwd)"); | |
| process.exit(1); | |
| } | |
| log(`Starting extraction: transcript=${transcriptPath} cwd=${cwd}`); | |
| // Parse the JSONL transcript | |
| interface TranscriptEntry { | |
| type?: string; | |
| message?: { | |
| role?: string; | |
| content?: Array<{ type?: string; text?: string }> | string; | |
| }; | |
| // Tool use/result entries may have different shapes | |
| [key: string]: unknown; | |
| } | |
| let lines: string[]; | |
| try { | |
| const raw = readFileSync(transcriptPath, "utf-8"); | |
| lines = raw.split("\n").filter((l) => l.trim()); | |
| log(`Read ${lines.length} transcript lines`); | |
| } catch (e) { | |
| log(`ERROR reading transcript: ${e}`); | |
| process.exit(1); | |
| } | |
| // Extract meaningful messages | |
| const userMessages: string[] = []; | |
| const assistantMessages: string[] = []; | |
| const corrections: string[] = []; | |
| const correctionPatterns = | |
| /\b(no|wrong|not|don't|shouldn't|never|stop|fix|incorrect|actually|instead|should be|must be|always use|never use)\b/i; | |
| for (const line of lines) { | |
| let entry: TranscriptEntry; | |
| try { | |
| entry = JSON.parse(line); | |
| } catch { | |
| continue; | |
| } | |
| const role = entry?.message?.role; | |
| const content = entry?.message?.content; | |
| if (!content) continue; | |
| // Extract text from content (can be string or array) | |
| let text = ""; | |
| if (typeof content === "string") { | |
| text = content; | |
| } else if (Array.isArray(content)) { | |
| text = content | |
| .filter((c) => c.type === "text" && c.text) | |
| .map((c) => c.text!) | |
| .join(" "); | |
| } | |
| if (!text.trim()) continue; | |
| // Truncate very long messages | |
| const truncated = text.length > 500 ? text.slice(0, 500) + "..." : text; | |
| if (role === "user") { | |
| userMessages.push(truncated); | |
| // Detect corrections/directives | |
| if (correctionPatterns.test(text)) { | |
| corrections.push(truncated); | |
| } | |
| } else if (role === "assistant") { | |
| assistantMessages.push(truncated); | |
| } | |
| } | |
| log( | |
| `Extracted: ${userMessages.length} user msgs, ${assistantMessages.length} assistant msgs, ${corrections.length} corrections` | |
| ); | |
| if (userMessages.length === 0) { | |
| log("No user messages found, nothing to extract"); | |
| process.exit(0); | |
| } | |
| // Get git state for additional context | |
| let gitContext = ""; | |
| try { | |
| const branch = execSync("git branch --show-current", { cwd, timeout: 5000 }) | |
| .toString() | |
| .trim(); | |
| const diff = execSync("git diff --stat HEAD", { cwd, timeout: 5000 }) | |
| .toString() | |
| .trim(); | |
| const status = execSync("git status --short", { cwd, timeout: 5000 }) | |
| .toString() | |
| .trim(); | |
| gitContext = `Branch: ${branch}\nChanged files:\n${diff}\nStatus:\n${status}`; | |
| } catch { | |
| gitContext = "(git context unavailable)"; | |
| } | |
| // Build the prompt for Claude using the full /compact summarization prompt | |
| const recentUserMsgs = userMessages.slice(-15); | |
| const recentAssistantMsgs = assistantMessages.slice(-10); | |
| const conversationTranscript = [ | |
| `## Git state\n${gitContext}`, | |
| `## User corrections and directives (auto-detected)\n${corrections.length > 0 ? corrections.map((c, i) => `${i + 1}. ${c}`).join("\n") : "(none detected)"}`, | |
| `## Recent user messages (last ${recentUserMsgs.length})\n${recentUserMsgs.map((m, i) => `${i + 1}. ${m}`).join("\n\n")}`, | |
| `## Recent assistant context (last ${recentAssistantMsgs.length})\n${recentAssistantMsgs.map((m, i) => `${i + 1}. ${m}`).join("\n\n")}`, | |
| ].join("\n\n---\n\n"); | |
| const prompt = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions. | |
| This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context. | |
| Your output will be written directly to a file. Output ONLY the summary content — no preamble, no sign-off, no conversational text, no suggestions. | |
| Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process: | |
| 1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify: | |
| - The user's explicit requests and intents | |
| - Your approach to addressing the user's requests | |
| - Key decisions, technical concepts and code patterns | |
| - Specific details like: | |
| - file names | |
| - full code snippets | |
| - function signatures | |
| - file edits | |
| - Errors that you ran into and how you fixed them | |
| - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently. | |
| 2. Double-check for technical accuracy and completeness, addressing each required element thoroughly. | |
| Your summary should include the following sections: | |
| 1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail | |
| 2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed. | |
| 3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important. | |
| 4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently. | |
| 5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts. | |
| 6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent. | |
| 7. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on. | |
| 8. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable. | |
| 9. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first. | |
| If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation. | |
| Use this exact output structure: | |
| <analysis> | |
| [Your thought process, ensuring all points are covered thoroughly and accurately] | |
| </analysis> | |
| <summary> | |
| 1. Primary Request and Intent: | |
| [Detailed description] | |
| 2. Key Technical Concepts: | |
| - [Concept 1] | |
| - [Concept 2] | |
| 3. Files and Code Sections: | |
| - [File Name 1] | |
| - [Summary of why this file is important] | |
| - [Summary of the changes made to this file, if any] | |
| - [Important Code Snippet] | |
| 4. Errors and fixes: | |
| - [Detailed description of error 1]: | |
| - [How you fixed the error] | |
| - [User feedback on the error if any] | |
| 5. Problem Solving: | |
| [Description of solved problems and ongoing troubleshooting] | |
| 6. All user messages: | |
| - [Detailed non tool use user message] | |
| 7. Pending Tasks: | |
| - [Task 1] | |
| - [Task 2] | |
| 8. Current Work: | |
| [Precise description of current work] | |
| 9. Optional Next Step: | |
| [Optional Next step to take] | |
| </summary> | |
| --- | |
| Here is the conversation to summarize: | |
| ${conversationTranscript}`; | |
| log("Calling claude -p --model haiku for summarization..."); | |
| // Write prompt to temp file to avoid shell escaping issues | |
| const tmpPrompt = `/tmp/cc-compact-prompt-${Date.now()}.txt`; | |
| writeFileSync(tmpPrompt, prompt); | |
| try { | |
| const result = execSync( | |
| `claude -p --model haiku < "${tmpPrompt}"`, | |
| { | |
| timeout: 30000, | |
| encoding: "utf-8", | |
| env: { ...process.env, TERM: "dumb" }, | |
| } | |
| ); | |
| // Write the context file | |
| const outputDir = join(cwd, ".context"); | |
| mkdirSync(outputDir, { recursive: true }); | |
| const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19); | |
| const outputFile = join(outputDir, `compact-context-${ts}.md`); | |
| const header = `<!-- Auto-generated by pre-compact hook at ${new Date().toISOString()} -->\n<!-- This file preserves context from before conversation compaction -->\n\n`; | |
| writeFileSync(outputFile, header + result.trim() + "\n"); | |
| log(`Wrote context to ${outputFile} (${result.length} chars)`); | |
| // Clean up temp file | |
| try { | |
| execSync(`rm "${tmpPrompt}"`); | |
| } catch {} | |
| } catch (e) { | |
| log(`ERROR calling claude: ${e}`); | |
| // Clean up temp file even on error | |
| try { | |
| execSync(`rm "${tmpPrompt}"`); | |
| } catch {} | |
| process.exit(1); | |
| } | |
| log("Done."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # PreCompact hook - launches Bun context extractor in background. | |
| # Fires before conversation compaction to preserve key context. | |
| LOG="/tmp/cc-compact-context.log" | |
| log() { echo "[$(date '+%H:%M:%S')] pre-compact.sh: $*" >> "$LOG"; } | |
| INPUT=$(cat) | |
| log "Hook fired" | |
| TRANSCRIPT=$(echo "$INPUT" | grep -o '"transcript_path":"[^"]*"' | cut -d'"' -f4) | |
| CWD=$(echo "$INPUT" | grep -o '"cwd":"[^"]*"' | cut -d'"' -f4) | |
| TRIGGER=$(echo "$INPUT" | grep -o '"trigger":"[^"]*"' | cut -d'"' -f4) | |
| log "trigger=$TRIGGER transcript=$TRANSCRIPT cwd=$CWD" | |
| if [[ -z "$TRANSCRIPT" || -z "$CWD" ]]; then | |
| log "SKIP: missing transcript or cwd" | |
| exit 0 | |
| fi | |
| if [[ ! -f "$TRANSCRIPT" ]]; then | |
| log "SKIP: transcript file not found" | |
| exit 0 | |
| fi | |
| # Launch Bun extractor in background - don't block compaction | |
| BUN="${HOME}/.bun/bin/bun" | |
| SCRIPT="${HOME}/.claude/hooks/compact-context.ts" | |
| if [[ ! -x "$BUN" ]]; then | |
| log "ERROR: bun not found at $BUN" | |
| exit 0 | |
| fi | |
| "$BUN" run "$SCRIPT" "$TRANSCRIPT" "$CWD" >> "$LOG" 2>&1 & | |
| log "Launched bun extractor (PID $!)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment