/ scripts / optimize-memory-bank.sh
optimize-memory-bank.sh
  1  #!/usr/bin/env bash
  2  # optimize-memory-bank.sh
  3  #
  4  # This script analyzes the memory bank files and suggests optimizations
  5  # to keep the memory bank efficient while ensuring all necessary information
  6  # is included.
  7  #
  8  # Usage: ./scripts/optimize-memory-bank.sh [options]
  9  #
 10  # Options:
 11  #   --analyze-only    Only analyze the memory bank, don't suggest changes
 12  #   --check-duplicates Check for duplicate information across files (can be slow)
 13  #   --check-outdated  Check for outdated information
 14  #   --verbose         Show detailed output
 15  #
 16  # Example: ./scripts/optimize-memory-bank.sh --verbose
 17  
 18  set -e
 19  
 20  # Default options
 21  ANALYZE_ONLY=false
 22  CHECK_DUPLICATES=false
 23  CHECK_OUTDATED=false
 24  VERBOSE=false
 25  
 26  # Parse options
 27  while [[ $# -gt 0 ]]; do
 28      case $1 in
 29          --analyze-only)
 30              ANALYZE_ONLY=true
 31              shift
 32              ;;
 33          --check-duplicates)
 34              CHECK_DUPLICATES=true
 35              shift
 36              ;;
 37          --check-outdated)
 38              CHECK_OUTDATED=true
 39              shift
 40              ;;
 41          --verbose)
 42              VERBOSE=true
 43              shift
 44              ;;
 45          *)
 46              echo "Unknown option: $1"
 47              exit 1
 48              ;;
 49      esac
 50  done
 51  
 52  # Colors for output
 53  RED='\033[0;31m'
 54  GREEN='\033[0;32m'
 55  YELLOW='\033[0;33m'
 56  BLUE='\033[0;34m'
 57  NC='\033[0m' # No Color
 58  
 59  # Memory bank directory
 60  MEMORY_BANK_DIR="memory-bank"
 61  
 62  # Check if memory bank directory exists
 63  if [[ ! -d "$MEMORY_BANK_DIR" ]]; then
 64      echo -e "${RED}Error: Memory bank directory not found: $MEMORY_BANK_DIR${NC}"
 65      exit 1
 66  fi
 67  
 68  echo -e "${BLUE}Analyzing memory bank in $MEMORY_BANK_DIR...${NC}"
 69  
 70  # Get all markdown files in the memory bank
 71  MEMORY_BANK_FILES=$(find "$MEMORY_BANK_DIR" -name "*.md" | sort)
 72  FILE_COUNT=$(echo "$MEMORY_BANK_FILES" | wc -l)
 73  
 74  echo -e "${GREEN}Found $FILE_COUNT files in the memory bank.${NC}"
 75  
 76  # Calculate total size of memory bank
 77  TOTAL_SIZE=$(du -sh "$MEMORY_BANK_DIR" | cut -f1)
 78  echo -e "${GREEN}Total size of memory bank: $TOTAL_SIZE${NC}"
 79  
 80  # Check for core files
 81  CORE_FILES=("projectbrief.md" "productContext.md" "activeContext.md" "systemPatterns.md" "techContext.md" "progress.md")
 82  MISSING_CORE_FILES=()
 83  
 84  for file in "${CORE_FILES[@]}"; do
 85      if [[ ! -f "$MEMORY_BANK_DIR/$file" ]]; then
 86          MISSING_CORE_FILES+=("$file")
 87      fi
 88  done
 89  
 90  if [[ ${#MISSING_CORE_FILES[@]} -gt 0 ]]; then
 91      echo -e "${YELLOW}Warning: Missing core files:${NC}"
 92      for file in "${MISSING_CORE_FILES[@]}"; do
 93          echo -e "${YELLOW}  - $file${NC}"
 94      done
 95  else
 96      echo -e "${GREEN}All core files are present.${NC}"
 97  fi
 98  
 99  # Check file sizes
100  echo -e "\n${BLUE}File sizes:${NC}"
101  LARGE_FILES=()
102  SMALL_FILES=()
103  
104  for file in $MEMORY_BANK_FILES; do
105      FILE_SIZE=$(du -h "$file" | cut -f1)
106      LINE_COUNT=$(wc -l < "$file")
107      FILENAME=$(basename "$file")
108      
109      # Identify large files
110      if [[ $(du -k "$file" | cut -f1) -gt 50 ]]; then
111          echo -e "${YELLOW}  $FILENAME: $FILE_SIZE ($LINE_COUNT lines)${NC}"
112          LARGE_FILES+=("$file")
113      else
114          if [[ "$VERBOSE" == "true" ]]; then
115              echo -e "  $FILENAME: $FILE_SIZE ($LINE_COUNT lines)"
116          fi
117          
118          # Identify small files
119          if [[ $(du -k "$file" | cut -f1) -lt 2 && $LINE_COUNT -lt 20 ]]; then
120              SMALL_FILES+=("$file")
121          fi
122      fi
123  done
124  
125  # Check for duplicate filenames with different content
126  echo -e "\n${BLUE}Checking for similar filenames...${NC}"
127  FOUND_SIMILAR=false
128  
129  for file1 in $MEMORY_BANK_FILES; do
130      FILENAME1=$(basename "$file1" | sed 's/\.[^.]*$//')
131      
132      for file2 in $MEMORY_BANK_FILES; do
133          if [[ "$file1" != "$file2" ]]; then
134              FILENAME2=$(basename "$file2" | sed 's/\.[^.]*$//')
135              
136              # Check if filenames are similar (one is contained in the other)
137              if [[ "$FILENAME1" == *"$FILENAME2"* || "$FILENAME2" == *"$FILENAME1"* ]] && [[ "$FILENAME1" != "$FILENAME2" ]]; then
138                  FOUND_SIMILAR=true
139                  echo -e "${YELLOW}  Similar filenames: $(basename "$file1") and $(basename "$file2")${NC}"
140              fi
141          fi
142      done
143  done
144  
145  if [[ "$FOUND_SIMILAR" == "false" ]]; then
146      echo -e "${GREEN}  No similar filenames found.${NC}"
147  fi
148  
149  # Check for outdated information if requested
150  if [[ "$CHECK_OUTDATED" == "true" ]]; then
151      echo -e "\n${BLUE}Checking for potentially outdated information...${NC}"
152      
153      # Look for files that haven't been modified in a long time
154      echo -e "${YELLOW}Files not modified recently:${NC}"
155      FOUND_OUTDATED=false
156      
157      for file in $MEMORY_BANK_FILES; do
158          FILENAME=$(basename "$file")
159          
160          # Skip core files
161          if [[ " ${CORE_FILES[@]} " =~ " $FILENAME " ]]; then
162              continue
163          fi
164          
165          # Get file modification time
166          FILE_MOD_TIME=$(stat -c %Y "$file")
167          CURRENT_TIME=$(date +%s)
168          DAYS_OLD=$(( (CURRENT_TIME - FILE_MOD_TIME) / 60 / 60 / 24 ))
169          
170          if [[ "$DAYS_OLD" -gt 90 ]]; then
171              FOUND_OUTDATED=true
172              echo -e "${YELLOW}  $FILENAME - Last modified $DAYS_OLD days ago${NC}"
173          fi
174      done
175      
176      if [[ "$FOUND_OUTDATED" == "false" ]]; then
177          echo -e "${GREEN}  No significantly outdated files found.${NC}"
178      fi
179  fi
180  
181  # Check for duplicate information if requested (this can be slow)
182  if [[ "$CHECK_DUPLICATES" == "true" ]]; then
183      echo -e "\n${BLUE}Checking for potential duplicate files (based on size and line count)...${NC}"
184      
185      # Group files by similar size and line count
186      declare -A SIZE_GROUPS
187      
188      for file in $MEMORY_BANK_FILES; do
189          SIZE=$(du -k "$file" | cut -f1)
190          LINES=$(wc -l < "$file")
191          KEY="${SIZE}_${LINES}"
192          
193          if [[ -z "${SIZE_GROUPS[$KEY]}" ]]; then
194              SIZE_GROUPS[$KEY]="$file"
195          else
196              SIZE_GROUPS[$KEY]="${SIZE_GROUPS[$KEY]}|$file"
197          fi
198      done
199      
200      # Check for groups with multiple files
201      FOUND_DUPLICATES=false
202      
203      for key in "${!SIZE_GROUPS[@]}"; do
204          FILES="${SIZE_GROUPS[$key]}"
205          FILE_COUNT=$(echo "$FILES" | tr '|' '\n' | wc -l)
206          
207          if [[ "$FILE_COUNT" -gt 1 ]]; then
208              FOUND_DUPLICATES=true
209              echo -e "${YELLOW}  Files with similar size and line count:${NC}"
210              echo "$FILES" | tr '|' '\n' | while read -r file; do
211                  echo -e "${YELLOW}    - $(basename "$file")${NC}"
212              done
213          fi
214      done
215      
216      if [[ "$FOUND_DUPLICATES" == "false" ]]; then
217          echo -e "${GREEN}  No potential duplicate files found.${NC}"
218      fi
219  fi
220  
221  # Suggest optimizations
222  echo -e "\n${BLUE}Optimization suggestions:${NC}"
223  
224  # Suggest merging small, related files
225  if [[ ${#SMALL_FILES[@]} -gt 1 ]]; then
226      echo -e "${YELLOW}Consider merging these small files:${NC}"
227      for file in "${SMALL_FILES[@]}"; do
228          echo -e "${YELLOW}  - $(basename "$file")${NC}"
229      done
230  fi
231  
232  # Suggest splitting large files
233  if [[ ${#LARGE_FILES[@]} -gt 0 ]]; then
234      echo -e "${YELLOW}Consider splitting these large files:${NC}"
235      for file in "${LARGE_FILES[@]}"; do
236          echo -e "${YELLOW}  - $(basename "$file")${NC}"
237      done
238  fi
239  
240  # Check for files with version numbers in the name
241  VERSION_FILES=$(find "$MEMORY_BANK_DIR" -name "*v[0-9]*" -o -name "*-v[0-9]*" | sort)
242  if [[ -n "$VERSION_FILES" ]]; then
243      echo -e "${YELLOW}Consider consolidating these versioned files:${NC}"
244      echo "$VERSION_FILES" | while read -r file; do
245          echo -e "${YELLOW}  - $(basename "$file")${NC}"
246      done
247  fi
248  
249  echo -e "\n${GREEN}Memory bank analysis complete.${NC}"
250  echo -e "${BLUE}Remember:${NC}"
251  echo "1. Keep core files (projectbrief.md, activeContext.md, etc.) up to date"
252  echo "2. Merge related information to avoid duplication"
253  echo "3. Split large files into focused, topic-specific files"
254  echo "4. Remove or archive outdated information"
255  echo "5. Ensure all necessary information is included and easily accessible"