Skip to content

Commit 09cf4f6

Browse files
committed
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.
1 parent 321edbc commit 09cf4f6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

scripts/bash/update-agent-context.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,25 @@ update_existing_agent_file() {
388388
new_change_entry="- $CURRENT_BRANCH: Added $NEW_DB"
389389
fi
390390

391+
# Check if sections exist in the file
392+
local has_active_technologies=0
393+
local has_recent_changes=0
394+
395+
if grep -q "^## Active Technologies" "$target_file" 2>/dev/null; then
396+
has_active_technologies=1
397+
fi
398+
399+
if grep -q "^## Recent Changes" "$target_file" 2>/dev/null; then
400+
has_recent_changes=1
401+
fi
402+
391403
# Process file line by line
392404
local in_tech_section=false
393405
local in_changes_section=false
394406
local tech_entries_added=false
395407
local changes_entries_added=false
396408
local existing_changes_count=0
409+
local file_ended=false
397410

398411
while IFS= read -r line || [[ -n "$line" ]]; do
399412
# Handle Active Technologies section
@@ -454,6 +467,22 @@ update_existing_agent_file() {
454467
# Post-loop check: if we're still in the Active Technologies section and haven't added new entries
455468
if [[ $in_tech_section == true ]] && [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
456469
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
470+
tech_entries_added=true
471+
fi
472+
473+
# If sections don't exist, add them at the end of the file
474+
if [[ $has_active_technologies -eq 0 ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
475+
echo "" >> "$temp_file"
476+
echo "## Active Technologies" >> "$temp_file"
477+
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
478+
tech_entries_added=true
479+
fi
480+
481+
if [[ $has_recent_changes -eq 0 ]] && [[ -n "$new_change_entry" ]]; then
482+
echo "" >> "$temp_file"
483+
echo "## Recent Changes" >> "$temp_file"
484+
echo "$new_change_entry" >> "$temp_file"
485+
changes_entries_added=true
457486
fi
458487

459488
# Move temp file to target atomically

0 commit comments

Comments
 (0)