/** * Pattern Provider - State-Based Pattern System * Provides state-filtered patterns for Cursor to read and enforce * @requirement State-Based Pattern System (Cursor-Centric Design) * * Key Principle: Tool provides information, Cursor enforces compliance * - Tool: Organizes patterns by state, provides structured data * - Cursor: Reads patterns, validates compliance, enforces rules */ import type { WorkflowState, Task } from '@shadel/workflow-core'; /** * Pattern validation rule structure * Machine-readable validation rules for Cursor to check compliance */ export interface PatternValidation { /** * Validation type * - file_exists: Check if file exists * - command_run: Check if command was run (guidance only, Cursor checks) * - code_check: Check code compliance (guidance only, Cursor checks) * - custom: Custom validation (guidance only, Cursor checks) */ type: 'file_exists' | 'command_run' | 'code_check' | 'custom'; /** * Machine-readable rule * For file_exists: File path (supports ${task.id} interpolation) * For others: Guidance for Cursor */ rule: string; /** * Human-readable message * What to show if pattern is violated */ message: string; /** * Severity level * - error: Critical violation (Cursor should fix) * - warning: Important but not critical * - info: Informational */ severity: 'error' | 'warning' | 'info'; } /** * State-Based Pattern * Extended pattern structure with state association and validation rules */ export interface StateBasedPattern { /** * Pattern ID (e.g., "RULE-1763115867072") */ id: string; /** * Pattern title */ title: string; /** * States where this pattern applies * If pattern applies to multiple states, include all */ applicableStates: WorkflowState[]; /** * States where this pattern is mandatory * If undefined or empty, pattern is recommended but not mandatory */ requiredStates?: WorkflowState[]; /** * Short description (for context injection) */ description: string; /** * Action instruction (what Cursor should do) * Clear, actionable instruction for Cursor */ action: string; /** * Validation rule (how Cursor checks compliance) */ validation: PatternValidation; /** * Full content (for backward compatibility) * Original pattern content from Rule interface */ content: string; /** * Source project or context (optional) */ source?: string; /** * Pattern score (1-5, optional) */ score?: number; /** * Creation timestamp */ createdAt: string; } /** * State pattern map structure * Organizes patterns by state (mandatory vs recommended) */ export type StatePatternMap = { [K in WorkflowState]: { mandatory: StateBasedPattern[]; recommended: StateBasedPattern[]; }; }; /** * Validation context for pattern compliance checking */ export interface ValidationContext { task: Task; changedFiles?: string[]; } /** * Pattern validation result */ export interface PatternValidationResult { pattern: StateBasedPattern; passed: boolean; message: string; severity: 'error' | 'warning' | 'info'; } /** * Pattern Provider * Provides state-filtered patterns for context injection * * Key Principle: Tool provides, Cursor enforces * - Tool: Filters patterns by state, organizes by mandatory/recommended * - Cursor: Reads patterns, validates compliance, takes actions */ export declare class PatternProvider { private ruleManager; constructor(); /** * Get patterns for specific state * Returns patterns organized by mandatory/recommended * * @param state - Current workflow state * @returns Patterns organized by mandatory/recommended */ getPatternsForState(state: WorkflowState): Promise<{ mandatory: StateBasedPattern[]; recommended: StateBasedPattern[]; }>; /** * Get state pattern map * Groups all patterns by state * * @returns Pattern map organized by state */ getStatePatternMap(): Promise; /** * Generate context section for NEXT_STEPS.md * Creates markdown content with state-filtered patterns * * @param patterns - Patterns organized by mandatory/recommended * @param state - Current workflow state * @returns Markdown content for context injection */ generateContextSection(patterns: { mandatory: StateBasedPattern[]; recommended: StateBasedPattern[]; }, state: WorkflowState): string; /** * Convert Rule to StateBasedPattern * Handles backward compatibility for existing patterns * * @param rule - Existing rule/pattern * @returns State-based pattern */ private convertToStateBased; /** * Normalize newlines in pattern content * Handles multiple escape formats: * - \\n (double escaped) → \n (single escaped) → actual newline * - \n (single escaped) → actual newline * - Preserves actual newlines */ private normalizeNewlines; /** * Extract description from content * Gets first paragraph or first 200 characters */ private extractDescription; /** * Extract action from content * Looks for action keywords or patterns */ private extractAction; /** * Validate pattern compliance * Tool reports violations, Cursor decides what to do * * @param pattern - Pattern to validate * @param context - Validation context * @returns Validation result (DO NOT throw errors) */ validatePatternCompliance(pattern: StateBasedPattern, context: ValidationContext): Promise; /** * Validate all patterns for a state * Returns violations (DO NOT block) * * @param state - Workflow state * @param context - Validation context * @returns Array of validation results (violations only) */ validateStatePatterns(state: WorkflowState, context: ValidationContext): Promise; }