Skip to content

Jeffrey0117/saveclaudenode

Repository files navigation

🛡️ SaveClaudeNode

Protect Your Claude Code Sessions from Accidental Termination

Multi-layered defense against killing all Node.js processes

License: MIT Rust Windows GitHub Stars

English繁體中文InstallationFeatures


😫 The Problem

Ever accidentally killed ALL Node.js processes with this command?

taskkill /F /IM node.exe  # Windows
pkill node                 # Linux/Mac

💥 What actually happens:

  • Claude Code terminates → Your entire session is lost
  • Development servers die → All your running apps crash
  • Background tasks killed → Build processes, watchers, everything
  • Work interrupted → Have to restart everything from scratch

You just wanted to kill ONE stubborn server on port 3000. Instead, you nuked your entire Node.js ecosystem.

✨ The Solution

SaveClaudeNode provides two-layer protection:

🚫 Layer 1: Prevention

PreToolUse Hook

Blocks dangerous commands before they execute inside Claude Code

Effectiveness: ~95%

📊 Layer 2: Monitoring

Guardian Daemon

Monitors all Node.js processes and logs termination events

Effectiveness: 100% logging


🎯 Features

🛡️ Smart Detection

  • Auto-identifies Claude Code processes
  • Command-line argument pattern matching
  • Real-time process monitoring (500ms scan interval)
  • Zero false positives

Lightweight & Fast

  • Only 287KB executable
  • < 0.1% CPU usage
  • Rust-powered performance
  • Native Windows integration

📝 Complete Audit Trail

  • Logs all protected processes
  • Records termination events
  • Timestamp + PID tracking
  • Full command history

🔧 Easy Setup

  • One-click hook installer
  • No manual configuration
  • Works out-of-the-box
  • Auto-start on boot options

🚀 Installation

⚡ Quick Install (Recommended)

Install PreToolUse Hook only (blocks 95% of accidental kills):

PowerShell:

git clone https://github.com/Jeffrey0117/saveclaudenode.git
cd saveclaudenode
.\install-hook.ps1

Command Prompt:

git clone https://github.com/Jeffrey0117/saveclaudenode.git
cd saveclaudenode
install-hook.bat

Restart Claude Code → ✅ Protected!


🔨 Advanced Install (Hook + Guardian Daemon)

For full protection with monitoring:

1. Install Rust:

winget install Rustlang.Rustup

2. Build Guardian:

git clone https://github.com/Jeffrey0117/saveclaudenode.git
cd saveclaudenode
cargo build --release

3. Install Hook:

.\install-hook.ps1

4. Run Guardian:

# Visible window
.\start-guard.bat

# Background (recommended)
.\start-guard-silent.vbs

📖 Usage

Testing the Hook

After installation, try this command in Claude Code:

taskkill /F /IM node.exe

Expected Output:

⛔ 禁止無差別殺 node.exe!這會把 Claude Code 也殺掉!

請使用 /kill <端口號> 來精確殺掉特定端口的進程。

Hook is working!

Guardian Daemon Output

════════════════════════════════════════════════
   SaveClaudeNode - Node.js Process Guardian
════════════════════════════════════════════════

[2026-01-28 07:30:15] 🚀 SaveClaudeNode guardian started
[2026-01-28 07:30:15] 📁 Log file: saveclaudenode.log
[2026-01-28 07:30:15] ⏱️  Scan interval: 500ms

守護程式已啟動!
- Monitoring all node.exe and electron.exe processes
- Auto-detecting Claude Code processes
- Log file: saveclaudenode.log

[2026-01-28 07:30:16] 🛡️  Protected Claude Code process: PID 12345
[2026-01-28 07:35:22] ⚠️  ALERT: Protected process PID 12345 was TERMINATED!

🛠️ How It Works

Architecture

graph TB
    A[User Types Command] --> B{Inside Claude Code?}
    B -->|Yes| C[PreToolUse Hook]
    C --> D{Dangerous Command?}
    D -->|Yes| E[❌ BLOCKED]
    D -->|No| F[✅ Execute]

    B -->|No| G[External Command]
    G --> H[Guardian Daemon]
    H --> I{Claude Code Killed?}
    I -->|Yes| J[⚠️ Log Alert]
    I -->|No| K[Continue Monitoring]
Loading

Protection Layers

Layer Tool What It Blocks Effectiveness Limitation
1 PreToolUse Hook taskkill /IM node.exe
pkill node
Stop-Process -Name node
~95% Only works inside Claude Code
2 Guardian Daemon N/A - Monitoring only 100% logging Cannot prevent termination

Blocked Command Patterns

The hook detects and blocks these patterns:

# Windows
taskkill /F /IM node.exe
taskkill //F //IM node.exe
taskkill /IM electron.exe
wmic process where name="node.exe" delete

# PowerShell
Stop-Process -Name node
Get-Process node | Stop-Process

# Unix-style (if running in WSL/Git Bash)
pkill node
killall node
kill -9 $(pidof node)

📁 Project Structure

saveclaudenode/
├── 🦀 src/
│   └── main.rs                  # Rust guardian daemon source
├── 🪝 hooks/
│   └── block-node-kill.js       # Claude Code PreToolUse hook
├── 📦 target/release/
│   └── saveclaudenode.exe       # Compiled binary (287KB)
├── 🔧 install-hook.ps1          # PowerShell hook installer
├── 🔧 install-hook.bat          # Batch hook installer
├── ▶️ start-guard.bat           # Start daemon (visible)
├── 🔇 start-guard-silent.vbs    # Start daemon (background)
├── 📖 README.md                 # This file
├── 📖 INSTALL.md                # Detailed installation guide
└── ⚖️ LICENSE                   # MIT License

⚙️ Configuration

Auto-start on Windows Boot

Option 1: Startup Folder (Easiest)

  1. Press Win + R
  2. Type shell:startup → Enter
  3. Copy start-guard-silent.vbs to the opened folder
  4. ✅ Guardian will start automatically on boot

Option 2: Task Scheduler (More Control)

$action = New-ScheduledTaskAction -Execute "C:\path\to\saveclaudenode.exe"
$trigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -TaskName "SaveClaudeNode" -Action $action -Trigger $trigger -RunLevel Highest

🔍 Technical Details

Guardian Daemon

Spec Value
Language Rust 🦀
Dependencies sysinfo, chrono, windows-rs
Binary Size 287KB
CPU Usage < 0.1%
Memory ~2MB
Scan Interval 500ms
Detection Method Process diff + CLI pattern matching

PreToolUse Hook

Spec Value
Language JavaScript
Hook Type PreToolUse (pre-execution)
Detection Regex + keyword analysis
False Positives Near zero
Performance Impact < 1ms per command

🚧 Known Limitations

Guardian Daemon

⚠️ Monitoring Only, Not Prevention

The guardian daemon can:

  • Detect when Claude Code is killed
  • Log all termination events with timestamps
  • Track which processes were terminated

But it cannot:

  • Prevent termination (would require kernel driver)
  • Restart Claude Code automatically
  • Recover lost session data

Why? Windows security model prevents user-space apps from blocking process termination. Only kernel-mode drivers can do this.

Best Practice

Use both layers together:

  • Hook blocks 95% of accidental kills inside Claude Code
  • Guardian provides audit trail for external terminations
  • Combined protection covers most scenarios

🗺️ Roadmap

Short-term

  • Windows Service mode (run as system service)
  • Desktop notifications on Claude Code termination
  • Whitelist mechanism (allow specific PIDs)
  • Process recovery (experimental auto-restart)

Long-term

  • Kernel driver version (true prevention)
  • Linux/macOS support
  • Cloud logging & analytics
  • Multi-user session support

Want a feature? Open an issue!


🤝 Contributing

Contributions are welcome! This project was born from frustration.

Ways to Contribute

  • 🐛 Report bugs - Open an issue
  • 💡 Suggest features - Share your ideas
  • 🔧 Submit PRs - Fix bugs or add features
  • Star the repo - Help others discover it
  • 📢 Share - Tell your fellow Claude Code users

Development Setup

# Clone the repo
git clone https://github.com/Jeffrey0117/saveclaudenode.git
cd saveclaudenode

# Install Rust
winget install Rustlang.Rustup

# Build
cargo build --release

# Test
.\target\release\saveclaudenode.exe

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

TL;DR: Free to use, modify, and distribute. Just keep the license notice.


🙏 Acknowledgments

Created by developers who got tired of accidentally killing their Claude Code sessions. 😅

Special thanks to:

  • Everyone who's ever typed taskkill /IM node.exe in frustration
  • The Rust community for amazing system programming tools
  • Claude Code team for making such an awesome tool worth protecting

📞 Support

About

🛡️ Protect Claude Code sessions from accidental Node.js process termination. Multi-layered defense with PreToolUse hooks + Guardian daemon.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors