Management

From masharratt/claude-flow-novice · View interactive version

Management is an AI agent skill from the GitHub repository masharratt/claude-flow-novice. Prevent and detect memory leaks in Claude CLI operations through proactive monitoring, limits, and profiling. Use when managing memory limits for long-running agents, detecting memory leaks, profiling heap usage, or implementing recovery procedures.

How to use this skill

Skill file

CFN Memory Management Skill

Purpose: Prevent and detect memory leaks in Claude CLI operations through proactive monitoring, limits, and profiling.

Overview

This skill implements memory leak prevention strategies based on real-world memory spike analysis. It provides automated memory monitoring, profiling, and safe defaults to prevent Claude CLI from exhausting system memory.

Features

Configuration

Default Memory Limits

# Safe default (reduced from 16GB)
export NODE_OPTIONS="--max-old-space-size=8192"

# For production environments
export CLAUDE_MEMORY_LIMIT="8GB"
export CLAUDE_MEMORY_PROFILE_DIR="/tmp/claude-memory-profiles"

Environment Variables

Usage

Start Claude with Memory Profiling

# Use the memory leak prevention script
./scripts/memory-leak-prevention.sh profile --limit 6144

# Or set environment manually
export NODE_OPTIONS="--max-old-space-size=6144 --inspect=0.0.0.0:9229 --heap-prof"
npx claude-flow-novice

Monitor Existing Process

./scripts/memory-leak-prevention.sh monitor --pid 12345 --duration 600

Install Debug Tools

./scripts/memory-leak-prevention.sh install-tools

Analyze Memory Profiles

./scripts/memory-leak-prevention.sh analyze --output ./profiles

Memory Leak Detection Patterns

High-Risk Indicators

Automatic Detection

The skill monitors for these patterns and triggers alerts:

# Check for memory spikes
if [[ $rss_mb -gt 8192 ]]; then
    log "⚠️  High memory usage detected: ${rss_mb}MB RSS"
    # Trigger profiling dump
    kill -USR2 $target_pid 2>/dev/null || true
fi

Integration with CFN Loop

Memory-Safe CFN Loop Execution

# Before starting CFN Loop
export NODE_OPTIONS="--max-old-space-size=4096"
export CLAUDE_MEMORY_MONITORING=true

# Execute with memory bounds
/cfn-loop-cli "task description" --mode=standard

Agent Memory Management

Agents spawned via CLI automatically inherit memory limits:

# Agent inherits parent process memory limits
npx claude-flow-novice agent-spawn \
  --agent-type backend-developer \
  --memory-limit 2048 \
  --enable-profiling

Debugging Tools

Heap Profile Analysis

// Analyze heap profile data
const fs = require('fs');
const profile = JSON.parse(fs.readFileSync('profile.heapprofile', 'utf8'));

// Find largest allocations
const allocations = profile.heapProfile.samples
  .sort((a, b) => b.size - a.size)
  .slice(0, 10);

allocations.forEach((alloc, i) => {
  console.log(`${i+1}. ${alloc.functionName}: ${(alloc.size/1024/1024).toFixed(2)}MB`);
});

Memory Usage Monitoring

# Real-time memory monitoring
watch -n 5 'ps aux | grep claude | grep -v grep'

# Network connection monitoring
netstat -an | grep ESTABLISHED | grep -c

# File descriptor monitoring
lsof -p <PID> | wc -l

Recovery Procedures

When Memory Leak Detected

  1. Immediate Action: Kill the process to prevent system exhaustion

```bash

kill -9 <PID>

```

  1. Save Diagnostic Data:

```bash

# Save memory map

cat /proc/<PID>/smaps > memory-smaps.log

# Save process info

ps auxf > process-tree.log

```

  1. Restart with Limits:

```bash

export NODE_OPTIONS="--max-old-space-size=4096"

./scripts/memory-leak-prevention.sh profile

```

Automatic Recovery

The skill includes automatic recovery mechanisms:

Performance Impact

Monitoring Overhead

Profiling Overhead

Best Practices

Development Environment

# Use profiling for development
export NODE_OPTIONS="--max-old-space-size=4096 --inspect --heap-prof"

Production Environment

# Use conservative limits
export NODE_OPTIONS="--max-old-space-size=6144"
export CLAUDE_MEMORY_MONITORING=false

WSL Specific

# WSL memory optimization
export NODE_OPTIONS="--max-old-space-size=4096"
echo 1 | sudo tee /proc/sys/vm/overcommit_memory  # Enable memory overcommit

Troubleshooting

Common Issues

  1. High memory usage: Reduce memory limit, restart with profiling
  2. Slow performance: Disable profiling in production
  3. Connection issues: Check WSL memory limits
  4. Profile corruption: Use larger memory limits for profiling

Debug Mode

# Enable verbose debugging
export CLAUDE_MEMORY_DEBUG=true
export CLAUDE_MEMORY_LOG_LEVEL=trace

./scripts/memory-leak-prevention.sh profile --debug

Integration Hooks

Pre-Execution Hook

#!/bin/bash
# .claude/hooks/cfn-pre-execution/memory-check.sh

$HOME/.claude/skills/cfn-memory-persistence/lib/management/check-memory.sh
if [[ $? -ne 0 ]]; then
    echo "❌ Memory check failed. Please free memory before proceeding."
    exit 1
fi

Post-Execution Hook

#!/bin/bash
# .claude/hooks/cfn-post-execution/memory-cleanup.sh

$HOME/.claude/skills/cfn-memory-persistence/lib/management/cleanup-memory.sh

Metrics and Monitoring

Key Metrics

Alert Thresholds

Dependencies

Security Considerations

Version History

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.