Rules Engine

The Rules Engine stores and resolves hierarchical rules in Cortex. Rules influence agent behavior by injecting context into prompts — from global conventions down to file-specific patterns.

Rule scopes

Rules are organized into five scopes, from broadest to most specific:

ScopePriorityTargetExample
global0All sessions"Always use TypeScript strict mode"
project10Current project"Use pnpm, not npm"
agent20Specific agent"The reviewer agent should focus on security"
skill30Specific skill"The deploy skill requires staging first"
file40File pattern"Files in src/api/ must have JSDoc"

Higher priority scopes override lower ones when rules conflict.

Rule structure

typescript
{
  id: string;              // Unique identifier
  content: string;         // The rule text
  scope: RuleScope;        // global | project | agent | skill | file
  scopeTarget?: string;    // Name or pattern for scoped rules
  priority: number;        // Numeric priority (higher = more specific)
  source: RuleSource;      // "learned" | "manual" | "imported"
  confidence: number;      // 0.0–1.0
  enabled: boolean;
  createdAt: string;       // ISO timestamp
  learnedFrom?: string;    // Session key (for learned rules)
}

Hierarchical resolution

When building a prompt, the engine resolves applicable rules:

  1. Collects all enabled rules from globalproject → target scope
  2. Filters by scopeTarget match (if applicable)
  3. Sorts by priority (descending), then creation date (descending)
  4. Injects into the prompt as a <rules> block
xml
<rules>
- Always use TypeScript strict mode
- Use pnpm, not npm
- Files in src/api/ must have JSDoc
</rules>

Learning rules

The engine can propose rules from observed patterns. Proposed rules start disabled with low confidence:

bash
mayros rules learn    # Analyze session → propose rules

Proposed rules can be confirmed or rejected:

bash
mayros rules list                    # Shows proposed (disabled) + active rules
mayros rules confirm <ruleId>        # Enable + set confidence to 0.8
mayros rules reject <ruleId>         # Delete proposed rule

Cortex storage

{ns}:rule:{scope}:{id} → {ns}:rule:content → "Always use strict mode"
{ns}:rule:{scope}:{id} → {ns}:rule:scope → "global"
{ns}:rule:{scope}:{id} → {ns}:rule:priority → "0"
{ns}:rule:{scope}:{id} → {ns}:rule:source → "manual"
{ns}:rule:{scope}:{id} → {ns}:rule:confidence → "1.0"
{ns}:rule:{scope}:{id} → {ns}:rule:enabled → "true"

CLI

bash
mayros rules list [--scope global]         # List rules
mayros rules add "use strict mode" --scope global
mayros rules add "focus on security" --scope agent --target reviewer
mayros rules remove <ruleId>
mayros rules learn                         # Propose rules from session
mayros rules status                        # Show engine status