# Context Compaction Schema # Handles context window management with pre/post compact hooks # Ensures AI can resume work after context compaction # ============================================================================ # CONTEXT STATE (Saved before compaction) # ============================================================================ context_state: # Unique session ID session_id: string # compact_ # When state was captured captured_at: timestamp # Context usage at capture time context_usage: tokens_used: integer tokens_max: integer percentage: float # 0.0 - 1.0 threshold_triggered: float # e.g., 0.75 for 75% # Current workflow position workflow_position: workflow_id: string # Reference to workflow_state current_phase: string # DESIGNING, IMPLEMENTING, etc. active_task_id: string | null layer: integer # Current execution layer # What was being worked on active_work: entity_id: string # model_user, api_create_user, etc. entity_type: string # model, api, page, component action: string # creating, implementing, reviewing file_path: string | null # File being edited progress_notes: string # What was done so far # Pending actions (what to do next) next_actions: - action: string # create, implement, test, review target: string # Entity or file priority: integer # 1 = immediate context_needed: [string] # Files/entities needed for context # Files that were recently modified modified_files: - path: string action: string # created, modified, deleted summary: string # What changed # Important decisions made in this session decisions: - topic: string decision: string reasoning: string timestamp: timestamp # Blockers or issues encountered blockers: - issue: string status: string # unresolved, workaround, resolved notes: string # ============================================================================ # COMPACTION TRIGGERS # ============================================================================ compaction_triggers: # Automatic triggers based on context usage auto_triggers: warning_threshold: 0.70 # Show warning at 70% save_threshold: 0.80 # Auto-save state at 80% compact_threshold: 0.90 # Recommend compaction at 90% # Manual triggers manual_triggers: - command: "/compact" # User requests compaction - command: "/save-state" # User requests state save - command: "/resume" # User requests resume from state # ============================================================================ # PRE-COMPACT HOOK # ============================================================================ pre_compact_hook: description: "Executed before context compaction" actions: # 1. Capture current state - action: capture_workflow_state output: .workflow/context_state.json # 2. Save progress notes - action: generate_progress_summary include: - completed_tasks - current_task_status - next_steps - blockers # 3. Commit any uncommitted work - action: git_checkpoint message: "WIP: Pre-compaction checkpoint" # 4. Generate resume prompt - action: generate_resume_prompt output: .workflow/resume_prompt.md output_files: - .workflow/context_state.json # Full state - .workflow/resume_prompt.md # Human-readable resume - .workflow/modified_files.json # Recent changes # ============================================================================ # POST-COMPACT HOOK (Resume Injector) # ============================================================================ post_compact_hook: description: "Executed after compaction to restore context" # Resume prompt template (injected into context) resume_prompt_template: | ## Context Recovery - Resuming Previous Session ### Session Info - **Original Session**: {{session_id}} - **Captured At**: {{captured_at}} - **Context Usage**: {{percentage}}% (triggered at {{threshold_triggered}}%) ### Workflow Position - **Phase**: {{current_phase}} - **Active Task**: {{active_task_id}} - **Layer**: {{layer}} of {{total_layers}} ### What Was Being Worked On - **Entity**: {{entity_id}} ({{entity_type}}) - **Action**: {{action}} - **File**: {{file_path}} - **Progress**: {{progress_notes}} ### Next Actions (Priority Order) {{#each next_actions}} {{priority}}. **{{action}}** {{target}} - Context needed: {{context_needed}} {{/each}} ### Recent Changes {{#each modified_files}} - `{{path}}` - {{action}}: {{summary}} {{/each}} ### Key Decisions Made {{#each decisions}} - **{{topic}}**: {{decision}} {{/each}} ### Current Blockers {{#each blockers}} - {{issue}} ({{status}}): {{notes}} {{/each}} --- **Action Required**: Continue from the next action listed above. # ============================================================================ # CONTEXT MONITOR # ============================================================================ context_monitor: description: "Monitors context usage and triggers hooks" # Check frequency check_interval: after_each_tool_call # Warning messages by threshold warnings: - threshold: 0.70 message: "Context at 70%. Consider saving state soon." action: log_warning - threshold: 0.80 message: "Context at 80%. Auto-saving workflow state..." action: auto_save_state - threshold: 0.90 message: "Context at 90%. Compaction recommended. Run /compact to save state and clear context." action: recommend_compact - threshold: 0.95 message: "CRITICAL: Context at 95%. Forcing state save before potential truncation." action: force_save_state # ============================================================================ # RESUME WORKFLOW # ============================================================================ resume_workflow: description: "Steps to resume after compaction" steps: # 1. Check for existing state - step: check_state_exists file: .workflow/context_state.json if_missing: "No previous state found. Starting fresh." # 2. Load state - step: load_state parse: context_state # 3. Validate state currency - step: validate_state checks: - git_status_matches # Ensure no unexpected changes - workflow_exists # Workflow files still present - task_valid # Active task still exists # 4. Inject resume context - step: inject_resume_prompt template: post_compact_hook.resume_prompt_template # 5. Display summary - step: display_summary show: - progress_percentage - next_action - files_to_read # ============================================================================ # EXAMPLE STATE FILE # ============================================================================ example_context_state: session_id: compact_20250118_143022 captured_at: "2025-01-18T14:30:22Z" context_usage: tokens_used: 85000 tokens_max: 100000 percentage: 0.85 threshold_triggered: 0.80 workflow_position: workflow_id: workflow_20250118_100000 current_phase: IMPLEMENTING active_task_id: task_create_api_auth_login layer: 2 active_work: entity_id: api_auth_login entity_type: api action: implementing file_path: app/api/auth/login/route.ts progress_notes: "Created route handler, added validation. Need to implement JWT generation." next_actions: - action: implement target: JWT token generation in login route priority: 1 context_needed: - app/api/auth/login/route.ts - app/lib/auth.ts - action: implement target: api_auth_register priority: 2 context_needed: - .workflow/versions/v001/contexts/api_auth_register.yml modified_files: - path: app/api/auth/login/route.ts action: created summary: "Basic route structure with validation" - path: prisma/schema.prisma action: modified summary: "Added User model" decisions: - topic: Authentication method decision: "Use JWT with httpOnly cookies" reasoning: "More secure than localStorage, works with SSR" timestamp: "2025-01-18T14:00:00Z" blockers: []