Smart Rules Engine
The Smart Rules Engine turns your knowledge base’s MORAYA.md into an efficient, on-demand context system for the AI assistant — so large rule files no longer get hard-truncated or billed in full on every message.
The problem it solves
Earlier, MORAYA.md was injected into the AI system prompt with a fixed character limit. That caused three issues: rules could be cut off mid-sentence, big files ran up token costs on every request, and the AI had to guess when to re-read the file.
The rules engine replaces “single-file truncation” with auto-split + indexed on-demand loading. You still maintain one file; Moraya compiles it into rule segments the AI loads only when relevant.
How it works
When you save MORAYA.md, Moraya compares a hash of its contents. If it changed, the file is split by its top-level headings into segments under a derived .moraya/rules/ directory:
- Glob-tagged segments — a segment annotated with
<!-- globs: ... -->is loaded automatically whenever you edit a matching file (high-priority conditional loading). - Untagged segments — indexed by title; the AI reads the full segment on demand via its file tools.
- No heading structure — the file falls back to whole-file injection (unchanged legacy behavior), and the UI suggests adding headings.
The system prompt then carries a compact index plus any glob-matched segment content, instead of the entire file.
Design principles
| Principle | What it means |
|---|---|
| Single source of truth | You only maintain MORAYA.md; .moraya/rules/ is a generated artifact |
| Zero learning curve | No glob syntax required — just write good Markdown headings |
| Everyone wins | You see the whole document, the AI reads segments, billing loads on demand |
| Self-healing | .moraya/ isn’t committed to Git; a new machine rebuilds it automatically |
| Backward compatible | Files without headings behave exactly as before |
Adding conditional rules
To scope a rule to certain files, add a glob comment under its heading:
## API documentation style
<!-- globs: docs/api/**/*.md -->
- Every endpoint must document its error responses.
- Use `snake_case` for JSON field names.
Now that section is injected only when you’re editing a file under docs/api/. Sections without a glob comment stay in the index and are pulled in by the AI when they’re relevant to the conversation.
Tips
- Keep each rule under a clear, descriptive heading — the heading becomes the index entry the AI matches against.
- Group broadly-applicable rules (tone, terminology) without globs; scope narrow rules (per-folder conventions) with globs.
- The derived
.moraya/directory is safe to delete — it rebuilds on the next save.