/ scripts / maintain-memory-bank-noninteractive.sh
maintain-memory-bank-noninteractive.sh
  1  #!/usr/bin/env bash
  2  # maintain-memory-bank-noninteractive.sh
  3  #
  4  # This is a non-interactive version of the maintain-memory-bank.sh script
  5  # for testing and automation purposes.
  6  #
  7  # Usage: ./scripts/maintain-memory-bank-noninteractive.sh [options]
  8  #
  9  # Options:
 10  #   --analyze        Analyze the memory bank for optimization opportunities
 11  #   --consolidate    Consolidate similar files
 12  #   --clean          Remove duplicate information and outdated files
 13  #   --backup         Create a backup of the memory bank
 14  #   --all            Perform all maintenance tasks
 15  #
 16  # Example: ./scripts/maintain-memory-bank-noninteractive.sh --all
 17  
 18  set -e
 19  
 20  # Default options
 21  ANALYZE=false
 22  CONSOLIDATE=false
 23  CLEAN=false
 24  BACKUP=false
 25  ALL=false
 26  
 27  # Parse options
 28  while [[ $# -gt 0 ]]; do
 29      case $1 in
 30          --analyze)
 31              ANALYZE=true
 32              shift
 33              ;;
 34          --consolidate)
 35              CONSOLIDATE=true
 36              shift
 37              ;;
 38          --clean)
 39              CLEAN=true
 40              shift
 41              ;;
 42          --backup)
 43              BACKUP=true
 44              shift
 45              ;;
 46          --all)
 47              ALL=true
 48              shift
 49              ;;
 50          *)
 51              echo "Unknown option: $1"
 52              exit 1
 53              ;;
 54      esac
 55  done
 56  
 57  # If no specific tasks are requested, show usage
 58  if [[ "$ANALYZE" == "false" && "$CONSOLIDATE" == "false" && "$CLEAN" == "false" && "$BACKUP" == "false" && "$ALL" == "false" ]]; then
 59      echo "Usage: $0 [--analyze] [--consolidate] [--clean] [--backup] [--all]"
 60      exit 1
 61  fi
 62  
 63  # If --all is specified, enable all tasks
 64  if [[ "$ALL" == "true" ]]; then
 65      ANALYZE=true
 66      CONSOLIDATE=true
 67      CLEAN=true
 68      BACKUP=true
 69  fi
 70  
 71  # Colors for output
 72  RED='\033[0;31m'
 73  GREEN='\033[0;32m'
 74  YELLOW='\033[0;33m'
 75  BLUE='\033[0;34m'
 76  NC='\033[0m' # No Color
 77  
 78  # Memory bank directory
 79  MEMORY_BANK_DIR="memory-bank"
 80  
 81  # Check if memory bank directory exists
 82  if [[ ! -d "$MEMORY_BANK_DIR" ]]; then
 83      echo -e "${RED}Error: Memory bank directory not found: $MEMORY_BANK_DIR${NC}"
 84      exit 1
 85  fi
 86  
 87  # Function to analyze memory bank
 88  function analyze_memory_bank() {
 89      echo -e "${BLUE}Analyzing memory bank...${NC}"
 90      
 91      # Run the optimize-memory-bank.sh script
 92      ./scripts/optimize-memory-bank.sh
 93  }
 94  
 95  # Function to consolidate similar files (non-interactive)
 96  function consolidate_memory_bank() {
 97      echo -e "${BLUE}Consolidating similar files (non-interactive mode)...${NC}"
 98      
 99      # Create a temporary directory for consolidation
100      TEMP_DIR=$(mktemp -d)
101      trap 'rm -rf "$TEMP_DIR"' EXIT
102      
103      # Copy all non-backup files to the temporary directory
104      echo -e "${BLUE}Copying files to temporary directory (excluding backup directories)...${NC}"
105      find "$MEMORY_BANK_DIR" -maxdepth 1 -type f -name "*.md" | while read -r file; do
106          # Skip files in backup directories
107          if [[ "$(basename "$file")" != backup-* ]]; then
108              cp "$file" "$TEMP_DIR/"
109          fi
110      done
111      
112      echo -e "${GREEN}Would consolidate similar files in the memory bank (skipped in non-interactive mode)${NC}"
113      echo -e "${YELLOW}In interactive mode, this would run: ./scripts/consolidate-memory-bank.sh --similar-files${NC}"
114  }
115  
116  # Function to clean memory bank (non-interactive)
117  function clean_memory_bank() {
118      echo -e "${BLUE}Cleaning memory bank (non-interactive mode)...${NC}"
119      
120      # Check for empty files
121      EMPTY_FILES=$(find "$MEMORY_BANK_DIR" -maxdepth 1 -name "*.md" -size 0)
122      if [[ -n "$EMPTY_FILES" ]]; then
123          echo -e "${YELLOW}Found empty files (would be removed in interactive mode):${NC}"
124          echo "$EMPTY_FILES"
125      else
126          echo -e "${GREEN}No empty files found.${NC}"
127      fi
128      
129      # Check for old backup directories
130      OLD_BACKUPS=$(find "$MEMORY_BANK_DIR" -name "backup-*" -type d -mtime +30)
131      if [[ -n "$OLD_BACKUPS" ]]; then
132          echo -e "${YELLOW}Found old backup directories (would be removed in interactive mode):${NC}"
133          echo "$OLD_BACKUPS"
134      else
135          echo -e "${GREEN}No old backup directories found.${NC}"
136      fi
137      
138      # Check for duplicate content
139      echo -e "${BLUE}Checking for duplicate content...${NC}"
140      
141      # Create a temporary directory for analysis
142      TEMP_DIR=$(mktemp -d)
143      trap 'rm -rf "$TEMP_DIR"' EXIT
144      
145      # Create MD5 hashes of file content
146      for file in "$MEMORY_BANK_DIR"/*.md; do
147          if [[ -f "$file" ]]; then
148              FILENAME=$(basename "$file")
149              
150              # Skip backup directories and files
151              if [[ "$FILENAME" == backup-* ]]; then
152                  continue
153              fi
154              
155              # Create MD5 hash of file content
156              md5sum "$file" | awk '{print $1}' > "$TEMP_DIR/$FILENAME.md5"
157          fi
158      done
159      
160      # Find duplicate hashes
161      DUPLICATE_HASHES=$(sort "$TEMP_DIR"/*.md5 | uniq -d)
162      
163      if [[ -n "$DUPLICATE_HASHES" ]]; then
164          echo -e "${YELLOW}Found files with duplicate content:${NC}"
165          
166          for hash in $DUPLICATE_HASHES; do
167              DUPLICATE_FILES=$(grep -l "$hash" "$TEMP_DIR"/*.md5 | sed 's/\.md5$//')
168              echo -e "${YELLOW}Duplicate content in:${NC}"
169              echo "$DUPLICATE_FILES"
170          done
171          
172          echo -e "${YELLOW}In interactive mode, you would be prompted to review these files.${NC}"
173      else
174          echo -e "${GREEN}No duplicate content found.${NC}"
175      fi
176      
177      # Update the cleanup log
178      echo -e "${BLUE}Updating cleanup log...${NC}"
179      
180      CLEANUP_LOG="$MEMORY_BANK_DIR/cleanup-log.md"
181      
182      # Create cleanup log if it doesn't exist
183      if [[ ! -f "$CLEANUP_LOG" ]]; then
184          cat > "$CLEANUP_LOG" << EOF
185  # Memory Bank Cleanup Log
186  
187  ## Cleanup History
188  
189  EOF
190      fi
191      
192      # Add entry to cleanup log
193      cat >> "$CLEANUP_LOG" << EOF
194  ### $(date +"%Y-%m-%d %H:%M:%S") (Non-interactive Test)
195  
196  - Analyzed memory bank
197  - Checked for empty files
198  - Checked for old backup directories
199  - Checked for duplicate content
200  
201  EOF
202      
203      echo -e "${GREEN}Cleanup log updated.${NC}"
204  }
205  
206  # Function to backup memory bank
207  function backup_memory_bank() {
208      echo -e "${BLUE}Creating backup of memory bank...${NC}"
209      
210      # Create backup directory
211      BACKUP_DIR="$MEMORY_BANK_DIR/backup-$(date +%Y%m%d%H%M%S)-test"
212      mkdir -p "$BACKUP_DIR"
213      
214      # Copy all files to backup directory (excluding existing backup directories)
215      find "$MEMORY_BANK_DIR" -maxdepth 1 -type f -name "*.md" | while read -r file; do
216          # Skip files in backup directories
217          if [[ "$(basename "$file")" != backup-* ]]; then
218              cp "$file" "$BACKUP_DIR/"
219          fi
220      done
221      
222      echo -e "${GREEN}Backup created in $BACKUP_DIR${NC}"
223  }
224  
225  # Run the maintenance tasks
226  if [[ "$BACKUP" == "true" ]]; then
227      backup_memory_bank
228  fi
229  
230  if [[ "$ANALYZE" == "true" ]]; then
231      analyze_memory_bank
232  fi
233  
234  if [[ "$CONSOLIDATE" == "true" ]]; then
235      consolidate_memory_bank
236  fi
237  
238  if [[ "$CLEAN" == "true" ]]; then
239      clean_memory_bank
240  fi
241  
242  echo -e "${GREEN}Memory bank maintenance complete (non-interactive mode).${NC}"
243  echo -e "${BLUE}Remember:${NC}"
244  echo "1. Keep core files (projectbrief.md, activeContext.md, etc.) up to date"
245  echo "2. Merge related information to avoid duplication"
246  echo "3. Split large files into focused, topic-specific files"
247  echo "4. Remove or archive outdated information"
248  echo "5. Ensure all necessary information is included and easily accessible"