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:
| Scope | Priority | Target | Example |
|---|---|---|---|
global | 0 | All sessions | "Always use TypeScript strict mode" |
project | 10 | Current project | "Use pnpm, not npm" |
agent | 20 | Specific agent | "The reviewer agent should focus on security" |
skill | 30 | Specific skill | "The deploy skill requires staging first" |
file | 40 | File 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:
- Collects all enabled rules from
global→project→ target scope - Filters by
scopeTargetmatch (if applicable) - Sorts by priority (descending), then creation date (descending)
- 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:
bashmayros rules learn # Analyze session → propose rules
Proposed rules can be confirmed or rejected:
bashmayros 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
bashmayros 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
Related
- rules CLI — command reference
- Project Memory — conventions and decisions
- Agent Memory — per-agent persistent memory