Skip to content

feat: integrate claude-code-prompt-improver with sequential hooks#31

Merged
azalio merged 3 commits intomainfrom
feature/prompt-improver-integration
Nov 6, 2025
Merged

feat: integrate claude-code-prompt-improver with sequential hooks#31
azalio merged 3 commits intomainfrom
feature/prompt-improver-integration

Conversation

@azalio
Copy link
Copy Markdown
Owner

@azalio azalio commented Nov 6, 2025

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

  • File: .claude/hooks/improve-prompt.py
  • Purpose: Evaluates prompt clarity and asks grounded questions for vague prompts
  • Bypass: *, /, # prefixes

2. Sequential UserPromptSubmit Processing

Updated .claude/hooks/settings.hooks.json:

Step 1: Prompt-Improver → Disambiguate vague prompts
Step 2: Playbook Injection → Add relevant patterns
Step 3: Workflow Suggestion → Recommend MAP workflows
Step 4: Skill Suggestion → Offer 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

4. Template Synchronization

  • Synced to src/mapify_cli/templates/hooks/ for distribution via mapify init

Benefits

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

User: "fix the bug"

[Prompt-Improver Hook]
→ Research: codebase, logs, recent changes
→ Found: 3 potential errors

Which error needs fixing?
○ TypeError in src/components/Map.tsx
○ API timeout in src/services/osmService.ts
○ Other (paste error message)

Clear Prompt → Direct Execution

User: "Fix TypeError in src/Map.tsx line 127"

[Prompt-Improver]
→ Clear prompt detected → Immediate execution

[Playbook + Workflow hooks]
→ Add patterns + suggest /map-debug

Bypass

User: "* implement auth"

[Prompt-Improver]
→ Bypass detected → Pass through

Token Overhead

Scenario Overhead When
Bypass (*) 0 tokens Always
Clear prompt ~300 tokens Immediate execution
Vague prompt ~300 + research 1-6 questions

ROI: Better first-try outcomes = fewer iterations = overall efficiency

Testing

All bypass prefixes tested:

  • * prefix: Skips improvement
  • / prefix: Slash commands bypass
  • # prefix: Memorize feature bypass

Implementation Details

  • Architecture: Sequential Hooks (independent operation)
  • Tool: AskUserQuestion (Claude Code 2.0.22+)
  • Integration: Complements existing MAP hooks
  • Based on: severity1/claude-code-prompt-improver

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

  1. Test vague prompt:

    echo '{"prompt": "fix the bug"}' | python3 .claude/hooks/improve-prompt.py | jq
  2. Test bypass:

    echo '{"prompt": "* add feature"}' | python3 .claude/hooks/improve-prompt.py | jq
  3. Interactive test:

    claude "debug the error"  # Should ask clarifying questions

Disabling

Temporary: Use * prefix
Permanent: Remove Step 1 from .claude/hooks/settings.hooks.json

Related


Ready for review and testing! 🚀

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.
Copilot AI review requested due to automatic review settings November 6, 2025 07:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py hook to disambiguate vague prompts with research-based clarification
  • Restructures UserPromptSubmit hooks 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.

Comment on lines +25 to +28
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": text
}
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": text
}
"continue": True,
"additionalContext": text

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +28
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": text
}
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": text
}
"continue": True,
"additionalContext": text

Copilot uses AI. Check for mistakes.
Comment thread docs/USAGE.md Outdated
"UserPromptSubmit": [
// Comment out or remove Prompt-Improver hook
{
"description": "Inject playbook patterns only",
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
"description": "Inject playbook patterns only",
"description": "Step 2: Inject playbook patterns and suggest workflows",

Copilot uses AI. Check for mistakes.
Comment thread docs/USAGE.md Outdated
Comment on lines +1944 to +1951
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
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
azalio and others added 2 commits November 6, 2025 11:49
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.
@azalio azalio merged commit 42ea2d3 into main Nov 6, 2025
6 checks passed
azalio added a commit that referenced this pull request Nov 6, 2025
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.
azalio added a commit that referenced this pull request Nov 6, 2025
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
azalio added a commit that referenced this pull request Feb 13, 2026
…-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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants