Skip to content

Latest commit

 

History

History
194 lines (154 loc) · 8.25 KB

File metadata and controls

194 lines (154 loc) · 8.25 KB

MAP Efficient Workflow Optimization - Implementation Summary

Completed: Hook-Based Context Injection System

Problem Statement

  • Original /map-efficient command: 995 lines, ~5,400 tokens
  • Issue: Attention dilution → Claude skips critical steps (20% compliance)
  • Critical symptoms:
    • Context search skipped 80% of time
    • Self-audit skipped 90% of time
    • User interventions required ~3 times per workflow

Solution Implemented

State-Machine Orchestration + PreToolUse Hook Injection

Files Created

1. Hooks

  • .claude/hooks/workflow-context-injector.py (~150 lines)
    • PreToolUse hook that injects step reminders before EVERY tool call
    • Reads .map/<branch>/step_state.json for current step
    • Injects ~150 token reminder showing: current step, progress, mandatory next action
    • Non-blocking: Always allows tool execution
    • Pattern borrowed from ralph-loop's build_loop_context()

2. State Machine

  • .map/scripts/map_orchestrator.py (~400 lines)
    • CLI interface: get_next_step, validate_step, initialize
    • Manages 8 workflow step phases (DECOMPOSE → MONITOR)
    • State file: .map/<branch>/step_state.json
    • Enforces sequential execution, prevents step skipping

3. Utilities

  • .map/scripts/map_step_runner.py (~300 lines)
    • Deterministic step executors for mechanical operations
    • Functions: update_step_state, update_plan_status, validate_checkpoint, validate_step
    • Separates file I/O from LLM reasoning

4. Optimized Command

  • .claude/commands/map-efficient.md (reduced to 394 lines)
    • Token reduction: 5,400 → 1,750 tokens (68% reduction)
    • Word count: 4,066 → 1,317 words
    • Simplified routing logic based on state machine output
    • Recursive invocation for fresh context per subtask

Configuration Updates

  • .claude/settings.json: Added workflow-context-injector.py hook registration
  • Template sync: All files copied to src/mapify_cli/templates/

Documentation Updates

  • docs/ARCHITECTURE.md: Added comprehensive "Hook-Based Context Injection" section
    • Architecture diagrams
    • Token economics analysis
    • Migration guide for v1.x → v2.0.0
  • CHANGELOG.md: Documented breaking change with migration instructions

Results (Predicted)

Metric Before (v1.x) After (v2.0.0) Improvement
Step compliance ~20% ~85% 4.25×
Command file tokens ~5,400 ~1,750 68% reduction
Context search skip rate 80% ~5% 16× improvement
Self-audit skip rate 90% ~10% 9× improvement
User interventions ~3 per workflow ~0.3 10× reduction
Total workflow tokens ~54,000 ~9,250 83% reduction

Token Economics

  • Before: 5,400 tokens × 10 invocations = 54,000 tokens
  • After: 1,750 tokens + (150 hook tokens × 50 tool calls) = 9,250 tokens
  • Net savings: 83% despite hook overhead

Testing

map_orchestrator.py initialize - Creates state file successfully ✅ map_orchestrator.py get_next_step - Returns correct first step ✅ workflow-context-injector.py - Handles no-workflow case (fail-open) ✅ Template sync tests - All 13 tests passing ✅ File permissions - Scripts marked executable

Migration Path

Breaking Change: /map-efficient now requires Python state machine

User Action:

mapify init  # Regenerates .claude/ with new hooks and scripts

No manual migration needed for in-progress workflows - state system is additive.

Architecture Pattern

User types: /map-efficient

    ↓

┌──────────────────────────────────────────────────────────────┐
│  PreToolUse Hook (workflow-context-injector.py)              │
│  Injects: "⚠️ MANDATORY: Search context BEFORE Actor"       │
└──────────────────────────────────────────────────────────────┘

    ↓

┌──────────────────────────────────────────────────────────────┐
│  map-efficient.md (~1.75K tokens)                            │
│  1. Get next step → 2. Execute → 3. Validate → 4. Recurse   │
└──────────────────────────────────────────────────────────────┘

    ↓

┌──────────────────────────────────────────────────────────────┐
│  State Machine (map_orchestrator.py)                         │
│  Determines WHAT step to execute                             │
└──────────────────────────────────────────────────────────────┘

    ↓

┌──────────────────────────────────────────────────────────────┐
│  Workflow Gate (workflow-gate.py)                            │
│  BLOCKS Edit/Write until actor+monitor done                  │
└──────────────────────────────────────────────────────────────┘

Key Innovation

Constant Reminders > Upfront Instructions

Instead of a 995-line command file that Claude "compresses" mentally, inject ~300 char reminders before EVERY tool call. This keeps the current step top-of-mind without token bloat.

Next Steps (Future Work)

  1. Integration Testing: Run full /map-efficient workflow on test project
  2. Metrics Collection: Measure actual compliance rates vs. predictions
  3. Performance Tuning: Optimize hook latency (target: <100ms)
  4. Skills Migration: Convert commands to Skills (Phase 2 per plan)
  5. Self-MoA Integration: Ensure state machine handles 3-Actor branching path

Implementation Timeline

  • Phase 1: Core State Machine (Day 1-2) ✅ COMPLETE
    • workflow-context-injector.py
    • map_orchestrator.py
    • map_step_runner.py
  • Phase 2: Optimized Command File (Day 2-3) ✅ COMPLETE
    • map-efficient.md rewrite
  • Phase 3: Integration Testing (Day 3-4) ⏳ NEXT
  • Phase 4: Migration & Deployment (Day 4) ⏳ NEXT

Files Modified

.claude/
├── hooks/
│   ├── workflow-context-injector.py (NEW)
│   └── (other hooks omitted)
├── settings.json (UPDATED)
├── commands/
│   ├── map-efficient.md (OPTIMIZED - 995→394 lines)
│   └── map-efficient.md.backup (BACKUP)

.map/scripts/
├── map_orchestrator.py (NEW)
└── map_step_runner.py (NEW)

 src/mapify_cli/templates/
 ├── hooks/workflow-context-injector.py (SYNCED)
 ├── commands/map-efficient.md (SYNCED)
 ├── map/scripts/
 │   ├── map_orchestrator.py (SYNCED)
 │   └── map_step_runner.py (SYNCED)
 └── settings.json (SYNCED)

docs/
├── ARCHITECTURE.md (UPDATED - new section)
└── CHANGELOG.md (UPDATED - breaking change entry)

IMPLEMENTATION_SUMMARY.md (NEW - this file)

Technical Debt / Known Issues

  • TODO: Add comprehensive integration tests for full workflow
  • TODO: Add unit tests for map_orchestrator.py step transitions
  • TODO: Add unit tests for map_step_runner.py utilities
  • TODO: Performance benchmarking for hook injection latency
  • TODO: Update USAGE.md with workflow examples using new system

References

  • Original plan: Plan agent output from research phase
  • LLM Council recommendations: State-gated prompting pattern
  • Ralph Loop: Context injection pattern inspiration
  • Anthropic guidance: "Each consideration handled by separate LLM call"

Status: Phase 1-2 Complete ✅ | Ready for Integration Testing Next: Test full workflow on sample project Owner: Implementation per approved plan Date: 2026-01-27