From 5fe1f40f36f4c92a4ba7fa96309047fadcd50332 Mon Sep 17 00:00:00 2001 From: catlog22 Date: Wed, 10 Sep 2025 18:59:36 +0800 Subject: [PATCH] fix: Add safety checks and auto-recovery to update-memory commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix execution order in update-memory-related: detect changes before staging - Add safety checks to prevent unintended source code modifications - Implement automatic staging recovery if non-CLAUDE.md files are modified - Ensure both update commands have consistent safety mechanisms 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .claude/commands/update-memory-full.md | 15 +++++++++++++-- .claude/commands/update-memory-related.md | 22 +++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.claude/commands/update-memory-full.md b/.claude/commands/update-memory-full.md index a4ce985d..9922152e 100644 --- a/.claude/commands/update-memory-full.md +++ b/.claude/commands/update-memory-full.md @@ -83,7 +83,17 @@ FOR depth FROM max_depth DOWN TO 0: Bash(~/.claude/scripts/update_module_claude.sh "$module" "full" &) wait_all_jobs() -# Step 6: Display changes → Final status +# Step 6: Safety check and restore staging state +non_claude=$(Bash(git diff --cached --name-only | grep -v "CLAUDE.md" || true)) +if [ -n "$non_claude" ]; then + Bash(git restore --staged .) + echo "⚠️ Warning: Non-CLAUDE.md files were modified, staging reverted" + echo "Modified files: $non_claude" +else + echo "✅ Only CLAUDE.md files modified, staging preserved" +fi + +# Step 7: Display changes → Final status Bash(git status --short) ``` @@ -111,7 +121,8 @@ subagent_type: "memory-gemini-bridge" - **Separated Commands**: Each bash operation is a discrete, trackable step - **Intelligent Complexity Detection**: Model analyzes project context for optimal strategy - **Depth-Parallel Execution**: Same depth modules run in parallel, depths run sequentially -- **Git Integration**: Auto-cache changes before, show status after +- **Git Integration**: Auto-cache changes before, safety check and show status after +- **Safety Protection**: Automatic detection and revert of unintended source code modifications - **Module Detection**: Uses get_modules_by_depth.sh for structure discovery - **User Confirmation**: Clear plan presentation with approval step - **CLAUDE.md Only**: Only updates documentation, never source code \ No newline at end of file diff --git a/.claude/commands/update-memory-related.md b/.claude/commands/update-memory-related.md index 5ac021bf..88c40d76 100644 --- a/.claude/commands/update-memory-related.md +++ b/.claude/commands/update-memory-related.md @@ -17,17 +17,19 @@ Context-aware documentation update for modules affected by recent changes. #!/bin/bash # Context-aware CLAUDE.md documentation update -# Step 1: Cache git changes +# Step 1: Detect changed modules (before staging) +changed=$(Bash(~/.claude/scripts/detect_changed_modules.sh list)) + +# Step 2: Cache git changes (protect current state) Bash(git add -A 2>/dev/null || true) -# Step 2: Detect changed modules -changed=$(Bash(~/.claude/scripts/detect_changed_modules.sh list)) +# Step 3: Use detected changes or fallback if [ -z "$changed" ]; then changed=$(Bash(~/.claude/scripts/get_modules_by_depth.sh list | head -10)) fi count=$(echo "$changed" | wc -l) -# Step 3: Analysis handover → Model takes control +# Step 4: Analysis handover → Model takes control # BASH_EXECUTION_STOPS → MODEL_ANALYSIS_BEGINS # Pseudocode flow: @@ -88,7 +90,17 @@ FOR depth FROM max_depth DOWN TO 0: Bash(~/.claude/scripts/update_module_claude.sh "$module" "related" &) wait_all_jobs() -# Step 6: Display changes → Final status +# Step 6: Safety check and restore staging state +non_claude=$(Bash(git diff --cached --name-only | grep -v "CLAUDE.md" || true)) +if [ -n "$non_claude" ]; then + Bash(git restore --staged .) + echo "⚠️ Warning: Non-CLAUDE.md files were modified, staging reverted" + echo "Modified files: $non_claude" +else + echo "✅ Only CLAUDE.md files modified, staging preserved" +fi + +# Step 7: Display changes → Final status Bash(git diff --stat) ```