Skip to content

💫 Toolkit to help you get started with SDD or any other process!

Fix update-agent-context.sh to handle files without Active Technologies/Recent Changes sections

Mark09cf4f6cc4b7
diff --git a/scripts/bash/update-agent-context.sh b/scripts/bash/update-agent-context.sh
index d3cc422ed..a7fafc402 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