Fix: Claude Code hooks template variable issue - {{user_prompt}} not being substituted
- Dominant language
- TypeScript
- Stars
- 72.6k
- Forks
- 8.6k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 85
Description
## Issue
When using Claude Code hooks with Claude Flow, the `{{user_prompt}}` template variable in git commit messages appears literally as `{{user_prompt}}` instead of being replaced with the actual user prompt.
## Root Cause
Claude Code doesn't perform template variable substitution in hooks. Instead, it passes data to hooks via stdin as JSON with the following structure:
```json
{
"prompt": "the actual user prompt text",
"other_fields": "..."
}
```
## Solution
Updated the UserPromptSubmit hook in `.claude/settings.json` to read from stdin and extract the prompt field using `jq`:
### Before (incorrect):
```bash
TASK=$(echo "{{user_prompt}}" | head -c 100 | tr "\n" " ")
```
### After (correct):
```bash
INPUT=$(cat);
TASK=$(echo "$INPUT" | jq -r ".prompt // empty" 2>/dev/null | head -c 100 | tr "\n" " ");
if [ -z "$TASK" ]; then
TASK="Task checkpoint";
fi
```
## Files Updated
- `.claude/settings.json` - Fixed UserPromptSubmit hook to read from stdin
- `src/cli/simple-commands/init/templates/settings.json` - Template already uses correct approach
## Key Changes
1. **Read JSON from stdin**: `INPUT=$(cat)` captures the JSON data passed by Claude Code
2. **Extract prompt field**: `jq -r ".prompt // empty"` safely extracts the prompt field
3. **Fallback handling**: If prompt is empty or missing, defaults to "Task checkpoint"
4. **Error suppression**: `2>/dev/null` prevents jq errors from appearing in output
## Testing
The fix has been tested and confirmed working. Git commits now show the actual user prompt instead of the literal `{{user_prompt}}` text.
## Impact
This fix ensures that:
- Git commit messages accurately reflect the user's task
- GitHub releases have meaningful descriptions
- Checkpoint tracking is more informative
- The hook system works as intended with Claude Code
## Related
- Part of alpha.87 release
- Affects all Claude Code hook configurations
- Important for users tracking work via git commits
Labels: bug, documentation, hooks, claude-code-integration
Contributor guide
Assessment
This issue has not been assessed yet.