fix: make SessionStart hook robust — no set -e, graceful fallbacks

This commit is contained in:
2026-04-02 19:34:22 +00:00
parent ebe943a67e
commit da838f4ae7

View File

@@ -2,9 +2,11 @@
# SessionStart hook for ArcheFlow plugin.
# Injects the using-archeflow skill as additional context.
set -euo pipefail
PLUGIN_ROOT="$(cd "$(dirname "$0")/.." 2>/dev/null && pwd)" || {
echo '{}'
exit 0
}
PLUGIN_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SKILL_FILE="${PLUGIN_ROOT}/skills/using-archeflow/SKILL.md"
if [ ! -f "$SKILL_FILE" ]; then
@@ -12,18 +14,28 @@ if [ ! -f "$SKILL_FILE" ]; then
exit 0
fi
CONTENT=$(awk 'BEGIN{skip=0} /^---$/{skip++; next} skip>=2{print}' "$SKILL_FILE")
# Strip YAML frontmatter (everything between first two --- lines)
CONTENT=$(awk 'BEGIN{skip=0} /^---$/{skip++; next} skip>=2{print}' "$SKILL_FILE" 2>/dev/null) || {
echo '{}'
exit 0
}
# Use node if available, fall back to printf-based JSON escaping
if [ -z "$CONTENT" ]; then
echo '{}'
exit 0
fi
# JSON-escape and output
if command -v node &>/dev/null; then
node -e "
const content = require('fs').readFileSync('/dev/stdin', 'utf8');
console.log(JSON.stringify({
hookSpecificOutput: { additionalContext: content }
}));
" <<< "$CONTENT"
node -e '
const content = require("fs").readFileSync(0, "utf8");
console.log(JSON.stringify({ hookSpecificOutput: { additionalContext: content } }));
' <<< "$CONTENT"
else
# Portable fallback: escape for JSON using sed
ESCAPED=$(printf '%s' "$CONTENT" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;$!ba;s/\n/\\n/g')
# Portable fallback
ESCAPED=$(printf '%s' "$CONTENT" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;$!ba;s/\n/\\n/g' 2>/dev/null) || {
echo '{}'
exit 0
}
printf '{"hookSpecificOutput":{"additionalContext":"%s"}}' "$ESCAPED"
fi