fix: Claude Code 2.1.1 compatibility + log-level audit + path validation fixes (#614)
* Refactor CLAUDE.md and related files for December 2025 updates - Updated CLAUDE.md in src/services/worker with new entries for December 2025, including changes to Search.ts, GeminiAgent.ts, SDKAgent.ts, and SessionManager.ts. - Revised CLAUDE.md in src/shared to reflect updates and new entries for December 2025, including paths.ts and worker-utils.ts. - Modified hook-constants.ts to clarify exit codes and their behaviors. - Added comprehensive hooks reference documentation for Claude Code, detailing usage, events, and examples. - Created initial CLAUDE.md files in various directories to track recent activity. * fix: Merge user-message-hook output into context-hook hookSpecificOutput - Add footer message to additionalContext in context-hook.ts - Remove user-message-hook from SessionStart hooks array - Fixes issue where stderr+exit(1) approach was silently discarded Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Update logs and documentation for recent plugin and worker service changes - Added detailed logs for worker service activities from Dec 10, 2025 to Jan 7, 2026, including initialization patterns, cleanup confirmations, and diagnostic logging. - Updated plugin documentation with recent activities, including plugin synchronization and configuration changes from Dec 3, 2025 to Jan 7, 2026. - Enhanced the context hook and worker service logs to reflect improvements and fixes in the plugin architecture. - Documented the migration and verification processes for the Claude memory system and its integration with the marketplace. * Refactor hooks architecture and remove deprecated user-message-hook - Updated hook configurations in CLAUDE.md and hooks.json to reflect changes in session start behavior. - Removed user-message-hook functionality as it is no longer utilized in Claude Code 2.1.0; context is now injected silently. - Enhanced context-hook to handle session context injection without user-visible messages. - Cleaned up documentation across multiple files to align with the new hook structure and removed references to obsolete hooks. - Adjusted timing and command execution for hooks to improve performance and reliability. * fix: Address PR #610 review issues - Replace USER_MESSAGE_ONLY test with BLOCKING_ERROR test in hook-constants.test.ts - Standardize Claude Code 2.1.0 note wording across all three documentation files - Exclude deprecated user-message-hook.ts from logger-usage-standards test Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: Remove hardcoded fake token counts from context injection Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Address PR #610 review issues by fixing test files, standardizing documentation notes, and verifying code quality improvements. * fix: Add path validation to CLAUDE.md distribution to prevent invalid directory creation - Add isValidPathForClaudeMd() function to reject invalid paths: - Tilde paths (~) that Node.js doesn't expand - URLs (http://, https://) - Paths with spaces (likely command text or PR references) - Paths with # (GitHub issue/PR references) - Relative paths that escape project boundary - Integrate validation in updateFolderClaudeMdFiles loop - Add 6 unit tests for path validation - Update .gitignore to prevent accidental commit of malformed directories - Clean up existing invalid directories (~/, PR #610..., git diff..., https:) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: Implement path validation in CLAUDE.md generation to prevent invalid directory creation - Added `isValidPathForClaudeMd()` function to validate file paths in `src/utils/claude-md-utils.ts`. - Integrated path validation in `updateFolderClaudeMdFiles` to skip invalid paths. - Added 6 new unit tests in `tests/utils/claude-md-utils.test.ts` to cover various rejection cases. - Updated `.gitignore` to prevent tracking of invalid directories. - Cleaned up existing invalid directories in the repository. * feat: Promote critical WARN logs to ERROR level across codebase Comprehensive log-level audit promoting 38+ WARN messages to ERROR for improved debugging and incident response: - Parser: observation type errors, data contamination - SDK/Agents: empty init responses (Gemini, OpenRouter) - Worker/Queue: session recovery, auto-recovery failures - Chroma: sync failures, search failures (now treated as critical) - SQLite: search failures (primary data store) - Session/Generator: failures, missing context - Infrastructure: shutdown, process management failures - File Operations: CLAUDE.md updates, config reads - Branch Management: recovery checkout failures Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix: Address PR #614 review issues - Remove incorrectly tracked tilde-prefixed files from git - Fix absolute path validation to check projectRoot boundaries - Add test coverage for absolute path validation edge cases Closes review issues: - Issue 1: ~/ prefixed files removed from tracking - Issue 3: Absolute paths now validated against projectRoot - Issue 4: Added 3 new test cases for absolute path scenarios Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * build assets and context --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+64
-100
@@ -3,111 +3,75 @@
|
||||
|
||||
<!-- This section is auto-generated by claude-mem. Edit content outside the tags. -->
|
||||
|
||||
### Nov 13, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #7806 | 4:54 PM | 🔵 | PR #101 Enhancement: Continuation Prompt Token Reduction | ~634 |
|
||||
|
||||
### Nov 16, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #9976 | 11:35 PM | 🔵 | Endless Mode Architecture Plan Documented | ~661 |
|
||||
| #9967 | 11:18 PM | ⚖️ | Endless Mode Architecture: Immutable Storage with Ephemeral Transform | ~217 |
|
||||
|
||||
### Nov 17, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #10131 | 1:22 AM | 🔵 | Endless Mode Token Economics Analysis Output: Complete Infrastructure Impact | ~542 |
|
||||
| #10130 | " | ✅ | Integration of Actual Compute Savings Analysis into Main Execution Flow | ~258 |
|
||||
| #10129 | " | 🔵 | Prompt Caching Economics: User Cost vs. Anthropic Compute Cost Divergence | ~451 |
|
||||
| #10126 | 1:19 AM | 🔴 | Fix Return Statement Variable Names in playTheTapeThrough Function | ~313 |
|
||||
| #10125 | " | ✅ | Redesign Timeline Display to Show Fresh/Cached Token Breakdown and Real Dollar Costs | ~501 |
|
||||
| #10124 | " | ✅ | Replace Estimated Cost Model with Actual Caching-Based Costs in Anthropic Scale Analysis | ~516 |
|
||||
| #10123 | " | ✅ | Pivot Session Length Comparison Table from Token to Cost Metrics | ~413 |
|
||||
| #10122 | " | ✅ | Add Dual Reporting: Token Count vs Actual Cost in Comparison Output | ~410 |
|
||||
| #10121 | 1:18 AM | ✅ | Apply Prompt Caching Cost Model to Endless Mode Calculation Function | ~501 |
|
||||
| #10120 | " | ✅ | Integrate Prompt Caching Cost Calculations into Without-Endless-Mode Function | ~426 |
|
||||
| #10119 | " | ✅ | Display Prompt Caching Pricing in Initial Calculator Output | ~297 |
|
||||
| #10118 | " | ✅ | Add Prompt Caching Pricing Model to Token Economics Calculator | ~316 |
|
||||
| #10115 | 1:15 AM | 🟣 | Token Economics Calculator for Endless Mode Sessions | ~465 |
|
||||
| #10013 | 12:13 AM | 🔵 | Duplicate Agent SDK TypeScript Reference Documentation | ~340 |
|
||||
| #10012 | " | 🔵 | Agent SDK TypeScript API Reference Complete | ~349 |
|
||||
|
||||
### Nov 18, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #11738 | 11:51 PM | ⚖️ | Comprehensive Architecture Document Created for Phase 1 | ~868 |
|
||||
| #11711 | 11:44 PM | 🔵 | Language Model Tool Documentation Index | ~282 |
|
||||
| #11710 | " | 🔵 | Language Model Tool API Implementation Guide | ~718 |
|
||||
| #11709 | 11:43 PM | 🔵 | Comprehensive Copilot Extension Implementation Plan | ~624 |
|
||||
| #11708 | " | 🔵 | VS Code Chat Sample Documentation Unavailable | ~327 |
|
||||
| #11707 | " | 🔵 | VS Code Language Model API Structure and Capabilities | ~515 |
|
||||
| #11705 | " | ⚖️ | VS Code Extension Development Planning Phase Initiated | ~327 |
|
||||
| #11206 | 3:01 PM | 🔵 | mem-search skill architecture and migration details retrieved in full format | ~538 |
|
||||
|
||||
### Nov 25, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #15538 | 8:36 PM | 🔵 | Context Document for Landing Page Refinements | ~381 |
|
||||
| #15314 | 5:04 PM | 🔵 | Endless Mode Documentation Post Retrieved with 156 Lines | ~671 |
|
||||
|
||||
### Dec 20, 2025
|
||||
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #31257 | 8:58 PM | ⚖️ | Eight Conflict Detection Hypotheses Evaluated with Simulation Results | ~525 |
|
||||
| #31256 | " | 🔵 | Supersession vs Conflict Detection Feature Analysis | ~515 |
|
||||
|
||||
### Dec 30, 2025
|
||||
|
||||
**agent-sdk-v2-docs.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #34477 | 2:25 PM | ✅ | V2 Upgrade Branch Modifies Four Files with Net Code Reduction | ~328 |
|
||||
| #34466 | 2:24 PM | 🔵 | Agent SDK V2 Documentation Reveals Correct API Surface | ~399 |
|
||||
| #34425 | 2:04 PM | 🔵 | Agent SDK V2 API Documentation and Migration Patterns | ~698 |
|
||||
| #34422 | 2:03 PM | ✅ | Added Agent SDK V2 Documentation Files | ~240 |
|
||||
| #34419 | 2:02 PM | ✅ | Committed Agent SDK V2 upgrade preparation | ~275 |
|
||||
| #34520 | 2:34 PM | 🔵 | V2 Example Code Demonstrates All Key Patterns | ~537 |
|
||||
|
||||
### Jan 7, 2026
|
||||
|
||||
**agent-sdk-v2-example.ts**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #34431 | 2:06 PM | 🔵 | V2 SDK Executable Example Code Patterns | ~710 |
|
||||
| #34428 | 2:05 PM | 🔵 | Agent SDK v2 Example File Reading Requested | ~204 |
|
||||
| #34411 | 1:55 PM | ⚖️ | Agent SDK V2 Migration Plan Created | ~519 |
|
||||
| #34410 | " | ⚖️ | Agent SDK V2 Migration Strategy Analysis | ~499 |
|
||||
| #34406 | 1:54 PM | 🔵 | Comprehensive V2 Migration Analysis Shows Architectural Incompatibility | ~556 |
|
||||
| #34401 | " | 🔵 | Agent SDK V2 API Design and Patterns | ~435 |
|
||||
|
||||
### Dec 31, 2025
|
||||
|
||||
**agent-sdk-v2-preview.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #34635 | 3:04 PM | 🟣 | Documentation-First Workflow Design Document Created | ~769 |
|
||||
| #34616 | 2:59 PM | ⚖️ | Documentation-First Workflow Agent Analyzing V2 SDK Examples and Patterns | ~515 |
|
||||
| #34581 | 2:44 PM | 🔵 | V2 Session API Official Documentation Confirms Clear Separation Pattern | ~446 |
|
||||
|
||||
**agent-sdk-v2-examples.ts**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #34605 | 2:57 PM | 🔵 | V2 SDK Migration Failure Root Cause: Context Rot and Knowledge Transfer Gaps | ~550 |
|
||||
| #34583 | 2:44 PM | 🔵 | Executable V2 Examples Demonstrate All Core Patterns With Crystal Clear Code | ~471 |
|
||||
| #34580 | 2:43 PM | 🔵 | V2 Session API Documentation Shows Clear Send/Receive Pattern Despite Naming Confusion | ~364 |
|
||||
|
||||
### Jan 1, 2026
|
||||
|
||||
**try-catch-audit-report.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35635 | 11:14 PM | ✅ | Removed Temporary Try-Catch Audit Report | ~224 |
|
||||
| #35463 | 8:59 PM | 🟣 | Enforceable Anti-Pattern Detection System for Try-Catch Abuse | ~485 |
|
||||
| #35462 | " | 🟣 | Error handling audit tooling and documentation added | ~271 |
|
||||
| #35435 | 8:46 PM | ✅ | Try-Catch Audit Report Documented in Markdown | ~781 |
|
||||
|
||||
**agent-sdk-v2-examples.ts**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35429 | 6:32 PM | ⚖️ | KISS Principle Applied to SDKAgent.ts Defensive Code | ~322 |
|
||||
| #35412 | 5:25 PM | 🔵 | Canonical Example Shows Cost Tracking Without Token Usage Checks | ~289 |
|
||||
| #35406 | 5:24 PM | ⚖️ | Categorized Conditionals as Required Business Logic vs Defensive Code | ~377 |
|
||||
| #35404 | " | 🔵 | Canonical V2 SDK Patterns Require Message Type Checking | ~313 |
|
||||
| #35398 | 5:23 PM | 🟣 | Added comprehensive Claude Agent SDK V2 examples | ~231 |
|
||||
| #35383 | 3:11 PM | 🔵 | Phase 2 code quality review identified 5 critical bugs | ~604 |
|
||||
| #35382 | 3:08 PM | ✅ | Phase 2 Anti-Pattern Recheck Passed | ~315 |
|
||||
| #35379 | 3:07 PM | 🔵 | Phase 2 Anti-Pattern Check Found Content Extraction Bug | ~373 |
|
||||
| #35353 | 3:01 PM | 🔵 | Phase 2 preparation analysis completed | ~502 |
|
||||
| #35340 | 2:58 PM | 🔵 | Phase 1 Anti-Pattern Check Reveals V1/V2 API Mismatch | ~352 |
|
||||
| #35330 | 2:54 PM | 🔵 | Claude Agent SDK V2 API Pattern Documentation | ~305 |
|
||||
| #35329 | 2:53 PM | 🔵 | Agent SDK V2 API patterns and capabilities | ~453 |
|
||||
| #35292 | 1:23 PM | 🔵 | Agent SDK query() resume parameter: SDK-generated session IDs cannot be predetermined | ~543 |
|
||||
|
||||
**sdkagent-removal-list.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35425 | 5:27 PM | ✅ | Reorganized Removal List with DELETE/SIMPLIFY Section Header | ~240 |
|
||||
| #35424 | " | ✅ | Revised Token Tracking from Delete to Simplify with Hardcoded Zero | ~365 |
|
||||
| #35416 | 5:25 PM | 🟣 | Created Actionable Removal Checklist for SDKAgent.ts Cleanup | ~431 |
|
||||
|
||||
**agent-sdk-v2-preview.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35414 | 5:25 PM | 🔵 | V2 Preview Documentation Shows Direct Result Access Pattern | ~281 |
|
||||
| #35289 | 1:23 PM | 🔵 | Agent SDK V2 preview documentation: Explicit session lifecycle with createSession and resumeSession | ~523 |
|
||||
|
||||
**sdkagent-conditional-logic-CORRECTED.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35409 | 5:24 PM | 🟣 | Created Comprehensive Conditional Logic Removal Report | ~488 |
|
||||
|
||||
**dont-be-an-idiot.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35400 | 5:23 PM | 🔄 | Phase 1 SDK V2 migration - Updated imports and added API documentation | ~294 |
|
||||
| #35347 | 3:01 PM | ✅ | Phase 1 commit completed via Task agent | ~374 |
|
||||
| #35346 | " | ✅ | Committed Phase 1 of V2 API migration | ~333 |
|
||||
| #35345 | 3:00 PM | ✅ | Phase 1 Changes Committed to Git | ~338 |
|
||||
| #35343 | " | ✅ | Phase 1 Git Status Shows Modified Files | ~315 |
|
||||
| #35334 | 2:55 PM | 🔵 | V2 SDK Migration Preparation Complete | ~368 |
|
||||
| #35332 | 2:54 PM | 🟣 | SDK V2 API Documentation Created | ~305 |
|
||||
| #35331 | " | ✅ | Created documentation affirming V2 API stability | ~358 |
|
||||
|
||||
### Jan 2, 2026
|
||||
|
||||
**try-catch-audit-report.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #35703 | 1:01 PM | ⚖️ | Try-Catch as Anti-Pattern: Root Cause Analysis and Documentation | ~363 |
|
||||
|
||||
### Jan 6, 2026
|
||||
|
||||
**windows-code-evaluation.md**
|
||||
| ID | Time | T | Title | Read |
|
||||
|----|------|---|-------|------|
|
||||
| #38104 | 12:14 AM | 🔵 | Windows Compatibility Issues Documented Across 56 Memory Entries | ~509 |
|
||||
| #38209 | 7:39 PM | 🔵 | Claude Code Hooks System Architecture and Usage | ~491 |
|
||||
</claude-mem-context>
|
||||
@@ -0,0 +1,338 @@
|
||||
# Get started with Claude Code hooks
|
||||
|
||||
> Learn how to customize and extend Claude Code's behavior by registering shell commands
|
||||
|
||||
Claude Code hooks are user-defined shell commands that execute at various points
|
||||
in Claude Code's lifecycle. Hooks provide deterministic control over Claude
|
||||
Code's behavior, ensuring certain actions always happen rather than relying on
|
||||
the LLM to choose to run them.
|
||||
|
||||
<Tip>
|
||||
For reference documentation on hooks, see [Hooks reference](/en/hooks).
|
||||
</Tip>
|
||||
|
||||
Example use cases for hooks include:
|
||||
|
||||
* **Notifications**: Customize how you get notified when Claude Code is awaiting
|
||||
your input or permission to run something.
|
||||
* **Automatic formatting**: Run `prettier` on .ts files, `gofmt` on .go files,
|
||||
etc. after every file edit.
|
||||
* **Logging**: Track and count all executed commands for compliance or
|
||||
debugging.
|
||||
* **Feedback**: Provide automated feedback when Claude Code produces code that
|
||||
does not follow your codebase conventions.
|
||||
* **Custom permissions**: Block modifications to production files or sensitive
|
||||
directories.
|
||||
|
||||
By encoding these rules as hooks rather than prompting instructions, you turn
|
||||
suggestions into app-level code that executes every time it is expected to run.
|
||||
|
||||
<Warning>
|
||||
You must consider the security implication of hooks as you add them, because hooks run automatically during the agent loop with your current environment's credentials.
|
||||
For example, malicious hooks code can exfiltrate your data. Always review your hooks implementation before registering them.
|
||||
|
||||
For full security best practices, see [Security Considerations](/en/hooks#security-considerations) in the hooks reference documentation.
|
||||
</Warning>
|
||||
|
||||
## Hook Events Overview
|
||||
|
||||
Claude Code provides several hook events that run at different points in the
|
||||
workflow:
|
||||
|
||||
* **PreToolUse**: Runs before tool calls (can block them)
|
||||
* **PermissionRequest**: Runs when a permission dialog is shown (can allow or deny)
|
||||
* **PostToolUse**: Runs after tool calls complete
|
||||
* **UserPromptSubmit**: Runs when the user submits a prompt, before Claude processes it
|
||||
* **Notification**: Runs when Claude Code sends notifications
|
||||
* **Stop**: Runs when Claude Code finishes responding
|
||||
* **SubagentStop**: Runs when subagent tasks complete
|
||||
* **PreCompact**: Runs before Claude Code is about to run a compact operation
|
||||
* **SessionStart**: Runs when Claude Code starts a new session or resumes an existing session
|
||||
* **SessionEnd**: Runs when Claude Code session ends
|
||||
|
||||
Each event receives different data and can control Claude's behavior in
|
||||
different ways.
|
||||
|
||||
## Quickstart
|
||||
|
||||
In this quickstart, you'll add a hook that logs the shell commands that Claude
|
||||
Code runs.
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Install `jq` for JSON processing in the command line.
|
||||
|
||||
### Step 1: Open hooks configuration
|
||||
|
||||
Run the `/hooks` [slash command](/en/slash-commands) and select
|
||||
the `PreToolUse` hook event.
|
||||
|
||||
`PreToolUse` hooks run before tool calls and can block them while providing
|
||||
Claude feedback on what to do differently.
|
||||
|
||||
### Step 2: Add a matcher
|
||||
|
||||
Select `+ Add new matcher…` to run your hook only on Bash tool calls.
|
||||
|
||||
Type `Bash` for the matcher.
|
||||
|
||||
<Note>You can use `*` to match all tools.</Note>
|
||||
|
||||
### Step 3: Add the hook
|
||||
|
||||
Select `+ Add new hook…` and enter this command:
|
||||
|
||||
```bash theme={null}
|
||||
jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/bash-command-log.txt
|
||||
```
|
||||
|
||||
### Step 4: Save your configuration
|
||||
|
||||
For storage location, select `User settings` since you're logging to your home
|
||||
directory. This hook will then apply to all projects, not just your current
|
||||
project.
|
||||
|
||||
Then press `Esc` until you return to the REPL. Your hook is now registered.
|
||||
|
||||
### Step 5: Verify your hook
|
||||
|
||||
Run `/hooks` again or check `~/.claude/settings.json` to see your configuration:
|
||||
|
||||
```json theme={null}
|
||||
{
|
||||
"hooks": {
|
||||
"PreToolUse": [
|
||||
{
|
||||
"matcher": "Bash",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "jq -r '\"\\(.tool_input.command) - \\(.tool_input.description // \"No description\")\"' >> ~/.claude/bash-command-log.txt"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 6: Test your hook
|
||||
|
||||
Ask Claude to run a simple command like `ls` and check your log file:
|
||||
|
||||
```bash theme={null}
|
||||
cat ~/.claude/bash-command-log.txt
|
||||
```
|
||||
|
||||
You should see entries like:
|
||||
|
||||
```
|
||||
ls - Lists files and directories
|
||||
```
|
||||
|
||||
## More Examples
|
||||
|
||||
<Note>
|
||||
For a complete example implementation, see the [bash command validator example](https://github.com/anthropics/claude-code/blob/main/examples/hooks/bash_command_validator_example.py) in our public codebase.
|
||||
</Note>
|
||||
|
||||
### Code Formatting Hook
|
||||
|
||||
Automatically format TypeScript files after editing:
|
||||
|
||||
```json theme={null}
|
||||
{
|
||||
"hooks": {
|
||||
"PostToolUse": [
|
||||
{
|
||||
"matcher": "Edit|Write",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "jq -r '.tool_input.file_path' | { read file_path; if echo \"$file_path\" | grep -q '\\.ts$'; then npx prettier --write \"$file_path\"; fi; }"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Markdown Formatting Hook
|
||||
|
||||
Automatically fix missing language tags and formatting issues in markdown files:
|
||||
|
||||
```json theme={null}
|
||||
{
|
||||
"hooks": {
|
||||
"PostToolUse": [
|
||||
{
|
||||
"matcher": "Edit|Write",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/markdown_formatter.py"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Create `.claude/hooks/markdown_formatter.py` with this content:
|
||||
|
||||
````python theme={null}
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Markdown formatter for Claude Code output.
|
||||
Fixes missing language tags and spacing issues while preserving code content.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
|
||||
def detect_language(code):
|
||||
"""Best-effort language detection from code content."""
|
||||
s = code.strip()
|
||||
|
||||
# JSON detection
|
||||
if re.search(r'^\s*[{\[]', s):
|
||||
try:
|
||||
json.loads(s)
|
||||
return 'json'
|
||||
except:
|
||||
pass
|
||||
|
||||
# Python detection
|
||||
if re.search(r'^\s*def\s+\w+\s*\(', s, re.M) or \
|
||||
re.search(r'^\s*(import|from)\s+\w+', s, re.M):
|
||||
return 'python'
|
||||
|
||||
# JavaScript detection
|
||||
if re.search(r'\b(function\s+\w+\s*\(|const\s+\w+\s*=)', s) or \
|
||||
re.search(r'=>|console\.(log|error)', s):
|
||||
return 'javascript'
|
||||
|
||||
# Bash detection
|
||||
if re.search(r'^#!.*\b(bash|sh)\b', s, re.M) or \
|
||||
re.search(r'\b(if|then|fi|for|in|do|done)\b', s):
|
||||
return 'bash'
|
||||
|
||||
# SQL detection
|
||||
if re.search(r'\b(SELECT|INSERT|UPDATE|DELETE|CREATE)\s+', s, re.I):
|
||||
return 'sql'
|
||||
|
||||
return 'text'
|
||||
|
||||
def format_markdown(content):
|
||||
"""Format markdown content with language detection."""
|
||||
# Fix unlabeled code fences
|
||||
def add_lang_to_fence(match):
|
||||
indent, info, body, closing = match.groups()
|
||||
if not info.strip():
|
||||
lang = detect_language(body)
|
||||
return f"{indent}```{lang}\n{body}{closing}\n"
|
||||
return match.group(0)
|
||||
|
||||
fence_pattern = r'(?ms)^([ \t]{0,3})```([^\n]*)\n(.*?)(\n\1```)\s*$'
|
||||
content = re.sub(fence_pattern, add_lang_to_fence, content)
|
||||
|
||||
# Fix excessive blank lines (only outside code fences)
|
||||
content = re.sub(r'\n{3,}', '\n\n', content)
|
||||
|
||||
return content.rstrip() + '\n'
|
||||
|
||||
# Main execution
|
||||
try:
|
||||
input_data = json.load(sys.stdin)
|
||||
file_path = input_data.get('tool_input', {}).get('file_path', '')
|
||||
|
||||
if not file_path.endswith(('.md', '.mdx')):
|
||||
sys.exit(0) # Not a markdown file
|
||||
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
formatted = format_markdown(content)
|
||||
|
||||
if formatted != content:
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(formatted)
|
||||
print(f"✓ Fixed markdown formatting in {file_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error formatting markdown: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
````
|
||||
|
||||
Make the script executable:
|
||||
|
||||
```bash theme={null}
|
||||
chmod +x .claude/hooks/markdown_formatter.py
|
||||
```
|
||||
|
||||
This hook automatically:
|
||||
|
||||
* Detects programming languages in unlabeled code blocks
|
||||
* Adds appropriate language tags for syntax highlighting
|
||||
* Fixes excessive blank lines while preserving code content
|
||||
* Only processes markdown files (`.md`, `.mdx`)
|
||||
|
||||
### Custom Notification Hook
|
||||
|
||||
Get desktop notifications when Claude needs input:
|
||||
|
||||
```json theme={null}
|
||||
{
|
||||
"hooks": {
|
||||
"Notification": [
|
||||
{
|
||||
"matcher": "",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "notify-send 'Claude Code' 'Awaiting your input'"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### File Protection Hook
|
||||
|
||||
Block edits to sensitive files:
|
||||
|
||||
```json theme={null}
|
||||
{
|
||||
"hooks": {
|
||||
"PreToolUse": [
|
||||
{
|
||||
"matcher": "Edit|Write",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "python3 -c \"import json, sys; data=json.load(sys.stdin); path=data.get('tool_input',{}).get('file_path',''); sys.exit(2 if any(p in path for p in ['.env', 'package-lock.json', '.git/']) else 0)\""
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Learn more
|
||||
|
||||
* For reference documentation on hooks, see [Hooks reference](/en/hooks).
|
||||
* For comprehensive security best practices and safety guidelines, see [Security Considerations](/en/hooks#security-considerations) in the hooks reference documentation.
|
||||
* For troubleshooting steps and debugging techniques, see [Debugging](/en/hooks#debugging) in the hooks reference
|
||||
documentation.
|
||||
|
||||
|
||||
---
|
||||
|
||||
> To find navigation and other pages in this documentation, fetch the llms.txt file at: https://code.claude.com/docs/llms.txt
|
||||
Reference in New Issue
Block a user