From 09cf4f6cc4b76f0a887302e7890c5406b5b123b5 Mon Sep 17 00:00:00 2001 From: Mark <1289198+valdezm@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:43:57 -0700 Subject: [PATCH] Fix update-agent-context.sh to handle files without Active Technologies/Recent Changes sections - Add section detection logic to check if required sections exist - Automatically append missing sections at end of file if they don't exist - Preserve existing manually-created content in agent files - Fix bash syntax errors in grep command handling - Improve robustness for files that don't follow template structure This fixes an issue where the script would silently fail to update agent files like CLAUDE.md that were manually created with different section structures. --- scripts/bash/update-agent-context.sh | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/scripts/bash/update-agent-context.sh b/scripts/bash/update-agent-context.sh index d3cc422e..a7fafc40 100644 --- a/scripts/bash/update-agent-context.sh +++ b/scripts/bash/update-agent-context.sh @@ -388,12 +388,25 @@ update_existing_agent_file() { new_change_entry="- $CURRENT_BRANCH: Added $NEW_DB" fi + # Check if sections exist in the file + local has_active_technologies=0 + local has_recent_changes=0 + + if grep -q "^## Active Technologies" "$target_file" 2>/dev/null; then + has_active_technologies=1 + fi + + if grep -q "^## Recent Changes" "$target_file" 2>/dev/null; then + has_recent_changes=1 + fi + # Process file line by line local in_tech_section=false local in_changes_section=false local tech_entries_added=false local changes_entries_added=false local existing_changes_count=0 + local file_ended=false while IFS= read -r line || [[ -n "$line" ]]; do # Handle Active Technologies section @@ -454,6 +467,22 @@ update_existing_agent_file() { # Post-loop check: if we're still in the Active Technologies section and haven't added new entries if [[ $in_tech_section == true ]] && [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file" + tech_entries_added=true + fi + + # If sections don't exist, add them at the end of the file + if [[ $has_active_technologies -eq 0 ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then + echo "" >> "$temp_file" + echo "## Active Technologies" >> "$temp_file" + printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file" + tech_entries_added=true + fi + + if [[ $has_recent_changes -eq 0 ]] && [[ -n "$new_change_entry" ]]; then + echo "" >> "$temp_file" + echo "## Recent Changes" >> "$temp_file" + echo "$new_change_entry" >> "$temp_file" + changes_entries_added=true fi # Move temp file to target atomically