✨ Features: - 🧠 Shadow learner that builds intelligence from command patterns - 🛡️ Smart command validation with safety checks - 💾 Automatic context monitoring and backup system - 🔄 Session continuity across Claude restarts 📚 Documentation: - Complete Diátaxis-organized documentation - Learning-oriented tutorial for getting started - Task-oriented how-to guides for specific problems - Information-oriented reference for quick lookup - Understanding-oriented explanations of architecture 🚀 Installation: - One-command installation script - Bootstrap prompt for installation via Claude - Cross-platform compatibility - Comprehensive testing suite 🎯 Ready for real-world use and community feedback! 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
83 lines
2.7 KiB
Python
Executable File
83 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Context Monitor Hook - UserPromptSubmit hook
|
|
Monitors context usage and triggers backups when needed
|
|
"""
|
|
|
|
import sys
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add lib directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
|
|
|
|
from context_monitor import ContextMonitor
|
|
from backup_manager import BackupManager
|
|
from session_state import SessionStateManager
|
|
|
|
|
|
def main():
|
|
"""Main hook entry point"""
|
|
try:
|
|
# Read input from Claude Code
|
|
input_data = json.loads(sys.stdin.read())
|
|
|
|
# Initialize components
|
|
context_monitor = ContextMonitor()
|
|
backup_manager = BackupManager()
|
|
session_manager = SessionStateManager()
|
|
|
|
# Update context estimates from prompt
|
|
context_monitor.update_from_prompt(input_data)
|
|
|
|
# Check if backup should be triggered
|
|
backup_decision = context_monitor.check_backup_triggers("UserPromptSubmit", input_data)
|
|
|
|
if backup_decision.should_backup:
|
|
# Execute backup
|
|
session_state = session_manager.get_session_summary()
|
|
backup_result = backup_manager.execute_backup(backup_decision, session_state)
|
|
|
|
# Record backup in session
|
|
session_manager.add_backup(backup_result.backup_id, {
|
|
"reason": backup_decision.reason,
|
|
"success": backup_result.success
|
|
})
|
|
|
|
# Add context snapshot
|
|
session_manager.add_context_snapshot({
|
|
"usage_ratio": context_monitor.get_context_usage_ratio(),
|
|
"prompt_count": context_monitor.prompt_count,
|
|
"tool_executions": context_monitor.tool_executions
|
|
})
|
|
|
|
# Notify about backup
|
|
if backup_result.success:
|
|
message = f"Auto-backup created: {backup_decision.reason} (usage: {context_monitor.get_context_usage_ratio():.1%})"
|
|
else:
|
|
message = f"Backup attempted but failed: {backup_result.error}"
|
|
else:
|
|
message = f"Context usage: {context_monitor.get_context_usage_ratio():.1%}"
|
|
|
|
# Always allow operation (this is a monitoring hook)
|
|
response = {
|
|
"allow": True,
|
|
"message": message
|
|
}
|
|
|
|
print(json.dumps(response))
|
|
sys.exit(0)
|
|
|
|
except Exception as e:
|
|
# Never block on errors - always allow operation
|
|
response = {
|
|
"allow": True,
|
|
"message": f"Context monitor error: {str(e)}"
|
|
}
|
|
print(json.dumps(response))
|
|
sys.exit(0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |