fix(bash): sed replacement escaping, BSD portability, dead cleanup in update-agent-context.sh#2090
Open
404prefrontalcortexnotfound wants to merge 1 commit intogithub:mainfrom
Conversation
a6604ae to
44072cb
Compare
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)
44072cb to
dd96d15
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three bugs in
scripts/bash/update-agent-context.shthat 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 characterImpact: 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 forproject_namewhich was used unescaped.Bug 2: BSD sed newline insertion fails on macOS (lines 364-366)
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\nsequences in the generated agent context files.Fix: Replace with portable
awkapproach:Bug 3: cleanup() removes non-existent files (lines 125-126)
The script never creates files matching these patterns — all temp files use
mktempwith random suffixes. The wildcard with$$(PID) in/tmpis a code smell that could theoretically match unrelated files.Fix: Remove the dead
rmcommands. Temp files frommktempare cleaned up inline after use.Testing
bash -nsyntax check passesRelated Issues
Fixes #154 —
update-agent-context.shseems to be failing on macOSFixes #293 — update-agent-context.sh script fails with sed expression errors
Related: #338 — Problems detected by shellcheck on the provided bash scripts