fix(hooks): use Python JSON generation for reliable context injection

- Replace bash escape_json + printf with Python json.dump for
  reliable handling of Unicode and special characters in SKILL.md
- Output follows superpowers pattern: hookSpecificOutput.additionalContext
- 55KB context, 1230 lines, 8 mandatory skills injected at session start
- Tested: JSON valid, content complete
This commit is contained in:
asepharyana
2026-07-26 11:51:35 +07:00
parent a460599fc8
commit a167705769
2 changed files with 64 additions and 44 deletions
+6 -3
View File
@@ -3,15 +3,18 @@
"SessionStart": [{ "SessionStart": [{
"hooks": [{ "hooks": [{
"type": "command", "type": "command",
"command": "bash \"$CLAUDE_PLUGIN_ROOT/hooks/scripts/detect-project.sh\"", "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/scripts/detect-project.sh\"",
"timeout": 10 "shell": "bash",
"timeout": 10,
"async": false
}] }]
}], }],
"PreToolUse": [{ "PreToolUse": [{
"matcher": "Write|Edit", "matcher": "Write|Edit",
"hooks": [{ "hooks": [{
"type": "command", "type": "command",
"command": "bash \"$CLAUDE_PLUGIN_ROOT/hooks/scripts/detect-file-type.sh\" \"$TOOL_INPUT\"", "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/scripts/detect-file-type.sh\" \"$TOOL_INPUT\"",
"shell": "bash",
"timeout": 10 "timeout": 10
}] }]
}] }]
+58 -41
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# hub-guide: inject best-practice skills into context at session start # hub-guide: inject best-practice skill content at session start
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
@@ -41,48 +41,65 @@ fi
MANDATORY="engineering-principles clean-code clean-architecture testing error-handling security git-workflow api-design" MANDATORY="engineering-principles clean-code clean-architecture testing error-handling security git-workflow api-design"
SKILL_NAMES="${SKILL_NAMES#, }" SKILL_NAMES="${SKILL_NAMES#, }"
# --- read skill content --- # --- build context and output via Python for reliable JSON ---
read_skill() { python3 << PYEOF
local f="${PLUGIN_ROOT}/skills/${1}/SKILL.md" import json, os, sys
[ -f "$f" ] && cat "$f" || echo ""
}
# Summary header (visible in session) project_dir = """${PROJECT_DIR}"""
echo "📐 [hub-guide] detected: ${PROJECT_DIR}" mandatory = """${MANDATORY}"""
echo "📐 mandatory: ${MANDATORY}" skill_names = """${SKILL_NAMES}"""
[ -n "$SKILL_NAMES" ] && echo "📐 active: ${SKILL_NAMES}" plugin_root = """${PLUGIN_ROOT}"""
echo ""
# ───────────────────────────────────────────────────────────────── def read_skill(name):
# SKILL INJECTION — hub-guide best-practice rules path = os.path.join(plugin_root, "skills", name, "SKILL.md")
# This content is loaded into context before any user interaction. try:
# Claude MUST treat these as active skill instructions for every with open(path) as f:
# code decision throughout this session. return f.read()
# ───────────────────────────────────────────────────────────────── except:
echo "<EXTREMELY_IMPORTANT>" return ""
echo "You have the following hub-guide skills loaded and active. They apply to every code decision, review, and architecture discussion in this session — regardless of what language the user speaks."
# Inject full MANDATORY skill content # Build summary
for skill in $MANDATORY; do summary = f"\U0001f4d0 [hub-guide] detected: {project_dir}\n\U0001f4d0 mandatory: {mandatory}"
content=$(read_skill "$skill") if skill_names:
if [ -n "$content" ]; then summary += f"\n\U0001f4d0 active: {skill_names}"
echo "" summary += "\n\U0001f4d0 When in doubt — ask instead of assuming."
echo "=== hub-guide:${skill} ===" summary += "\n\U0001f4d0 Never assume — show evidence for everything."
echo "$content" summary += "\n\U0001f4d0 All skills work regardless of your spoken language."
fi
done
# List detected skills (their content loads on demand via Skill tool) # Build skill content
if [ -n "$SKILL_NAMES" ]; then content_parts = []
echo "" content_parts.append("<EXTREMELY_IMPORTANT>")
echo "=== hub-guide:detected ===" content_parts.append("You have the following hub-guide skills loaded and active. They apply to every code decision, review, and architecture discussion in this session — regardless of what language the user speaks.")
echo "The following skills are relevant to this project's tech stack."
echo "Load them with the Skill tool when their topics come up: ${SKILL_NAMES}"
fi
echo "" for skill in mandatory.split():
echo "For any skill not loaded above, use the Skill tool to load it." c = read_skill(skill.strip())
echo "" content_parts.append(f"\n=== hub-guide:{skill} ===\n{c}")
echo "IMPORTANT: Never assume or guess. Always find evidence in the codebase,"
echo "documentation, or by asking the user. Show your sources." if skill_names:
echo "</EXTREMELY_IMPORTANT>" content_parts.append(f"\n=== hub-guide:detected ===\nThe following skills are relevant to this project. If their topics come up, use the Skill tool to load them: {skill_names}")
content_parts.append("\nIMPORTANT: Never assume or guess. Always find evidence in the codebase, documentation, or by asking the user. Show your sources.\n</EXTREMELY_IMPORTANT>")
skill_content = "\n".join(content_parts)
full_context = f"{summary}\n\n{skill_content}"
# Output
if os.environ.get("CLAUDE_PLUGIN_ROOT") and not os.environ.get("COPILOT_CLI"):
output = {
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": full_context
}
}
json.dump(output, sys.stdout, ensure_ascii=False)
print()
else:
print(f"\U0001f4d0 [hub-guide] detected: {project_dir}")
print(f"\U0001f4d0 mandatory: {mandatory}")
if skill_names:
print(f"\U0001f4d0 active: {skill_names}")
print("\U0001f4d0 When in doubt — ask instead of assuming.")
print()
print(skill_content)
PYEOF
exit 0