Skip to content

fix(bash): sed replacement escaping, BSD portability, dead cleanup in update-agent-context.sh#2090

Open
404prefrontalcortexnotfound wants to merge 1 commit intogithub:mainfrom
404prefrontalcortexnotfound:fix/update-agent-context-sed-portability
Open

fix(bash): sed replacement escaping, BSD portability, dead cleanup in update-agent-context.sh#2090
404prefrontalcortexnotfound wants to merge 1 commit intogithub:mainfrom
404prefrontalcortexnotfound:fix/update-agent-context-sed-portability

Conversation

@404prefrontalcortexnotfound
Copy link
Copy Markdown

Summary

Three bugs in scripts/bash/update-agent-context.sh that cause silent failures on macOS and potential data corruption with special characters in project/technology names.

Bug 1: sed escaping targets wrong side (lines 318-320)

The escaping function escapes regex pattern characters ([, ., *, ^, $, +, {, }, |) but these variables are used as sed replacement strings, not patterns. In sed replacements, only two characters are special:

  • & — inserts the entire matched pattern
  • \ — escape character

Impact: A technology name containing & (e.g., "AT&T", "R&D") silently corrupts the agent context file. The & gets replaced with the entire matched text from the left side of the substitution.

Fix: Escape only & and \ in replacement strings. Also adds escaping for project_name which was used unescaped.

Bug 2: BSD sed newline insertion fails on macOS (lines 364-366)

# Current — fails silently on BSD sed (macOS)
newline=$(printf '\n')
sed -i.bak2 "s/\\\\n/${newline}/g" "$temp_file"

BSD sed does not support literal newlines in replacement strings via variable expansion. GNU sed (Linux) does. This means the \n → newline conversion silently fails on macOS, leaving literal \n sequences in the generated agent context files.

Fix: Replace with portable awk approach:

awk '{gsub(/\\n/,"\n")}1' "$temp_file" > "$temp_file.tmp" && mv "$temp_file.tmp" "$temp_file"

Bug 3: cleanup() removes non-existent files (lines 125-126)

rm -f /tmp/agent_update_*_$$
rm -f /tmp/manual_additions_$$

The script never creates files matching these patterns — all temp files use mktemp with random suffixes. The wildcard with $$ (PID) in /tmp is a code smell that could theoretically match unrelated files.

Fix: Remove the dead rm commands. Temp files from mktemp are cleaned up inline after use.

Testing

  • bash -n syntax check passes
  • Tested on macOS 15 (Darwin, BSD sed) — all three fixes verified

Related Issues

Fixes #154update-agent-context.sh seems to be failing on macOS
Fixes #293 — update-agent-context.sh script fails with sed expression errors
Related: #338 — Problems detected by shellcheck on the provided bash scripts

@404prefrontalcortexnotfound 404prefrontalcortexnotfound force-pushed the fix/update-agent-context-sed-portability branch from a6604ae to 44072cb Compare April 4, 2026 08:15
Three bugs in update-agent-context.sh:

1. **sed escaping targets wrong side** (line 318-320): The escaping
   function escapes regex pattern characters (`[`, `.`, `*`, `^`, `$`,
   `+`, `{`, `}`, `|`) but these variables are used as sed
   *replacement* strings, not patterns. Only `&` (insert matched text),
   `\` (escape char), and `|` (our sed delimiter) are special in the
   replacement context. Also adds escaping for `project_name` which
   was used unescaped.

2. **BSD sed newline insertion fails on macOS** (line 364-366): Uses
   bash variable expansion to insert a literal newline into a sed
   replacement string. This works on GNU sed (Linux) but fails silently
   on BSD sed (macOS). Replaced with portable awk approach that works
   on both platforms.

3. **cleanup() removes non-existent files** (line 125-126): The
   cleanup trap attempts `rm -f /tmp/agent_update_*_$$` and
   `rm -f /tmp/manual_additions_$$` but the script never creates files
   matching these patterns — all temp files use `mktemp`. The wildcard
   with `$$` (PID) in /tmp could theoretically match unrelated files.

Fixes github#154 (macOS sed failure)
Fixes github#293 (sed expression errors)
Related: github#338 (shellcheck findings)
@404prefrontalcortexnotfound 404prefrontalcortexnotfound force-pushed the fix/update-agent-context-sed-portability branch from 44072cb to dd96d15 Compare April 4, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

update-agent-context.sh script fails with sed expression errors and incorrect template path update-agent-context.sh seems to be failing on macOS

1 participant