Skip to content

An agentic skills framework & software development methodology that works.

fix: replace O(n^2) escape_for_json with parameter substitution

Jesse Vincent038abed02617
diff --git a/hooks/session-start.sh b/hooks/session-start.sh
index f5d94497d..8c029afd8 100755
--- a/hooks/session-start.sh
+++ b/hooks/session-start.sh
@@ -17,23 +17,17 @@ fi
# Read using-superpowers content
using_superpowers_content=$(cat "${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md" 2>&1 || echo "Error reading using-superpowers skill")
-# Escape outputs for JSON using pure bash
+# Escape string for JSON embedding using bash parameter substitution.
+# Each ${s//old/new} is a single C-level pass - orders of magnitude
+# faster than the character-by-character loop this replaces.
escape_for_json() {
- local input="$1"
- local output=""
- local i char
- for (( i=0; i<${#input}; i++ )); do
- char="${input:$i:1}"
- case "$char" in
- $'\\') output+='\\' ;;
- '"') output+='\"' ;;
- $'\n') output+='\n' ;;
- $'\r') output+='\r' ;;
- $'\t') output+='\t' ;;
- *) output+="$char" ;;
- esac
- done
- printf '%s' "$output"
+ local s="$1"
+ s="${s//\\/\\\\}"
+ s="${s//\"/\\\"}"
+ s="${s//$'\n'/\\n}"
+ s="${s//$'\r'/\\r}"
+ s="${s//$'\t'/\\t}"
+ printf '%s' "$s"
}
using_superpowers_escaped=$(escape_for_json "$using_superpowers_content")