Rules & Boundaries
Relevant source files
The following files were used as context for generating this wiki page:
Rules and Boundaries form the core governance framework for Morty's autonomous AI agents. This system ensures that while agents operate with high autonomy, they adhere to project-specific coding standards, architectural patterns, and safety constraints. By defining "Rules" (active instructions) and "Boundaries" (exclusion zones), developers can maintain control over how the AI interacts with a codebase.
These configurations are stored within the .morty/ directory (or legacy .ralphy/ directory) in a config.yaml file. These instructions are injected into every prompt sent to the AI engine, providing persistent context across multiple task iterations.
Sources: morty.sh:122-132, cli/src/config/loader.ts:10-28, README.md:46-51
Configuration Structure
The configuration for Rules and Boundaries is defined using a structured YAML schema. This schema differentiates between project metadata, executable commands, and the specific constraints applied to the AI agent.
Data Model
The configuration is validated using Zod schemas to ensure consistency between the TypeScript CLI and the shell implementation.
| Section | Field | Type | Description |
|---|---|---|---|
| Rules | rules | string[] | Active instructions the AI MUST follow during implementation. |
| Boundaries | never_touch | string[] | Glob patterns or paths the AI is strictly forbidden from modifying. |
| Project | name | string | The auto-detected or manually set project name. |
| Commands | test/lint | string | Commands the agent should execute to verify work. |
Sources: cli/src/config/types.ts:6-42, cli/src/config/writer.ts:18-54
The diagram shows the hierarchical relationship of the configuration schema used to govern agent behavior.
Sources: cli/src/config/types.ts:31-42
Rules: Active Guidance
Rules are specific instructions designed to enforce coding standards or architectural decisions. Common examples include "Always use TypeScript strict mode" or "Use server actions instead of API routes."
Rule Injection Logic
When a task is executed, Morty aggregates these rules and injects them into a high-priority section of the prompt labeled ## Rules (you MUST follow these). This ensures that the AI model considers these constraints before generating code.
This flow represents how rules are retrieved from the filesystem and prioritized within the prompt construction phase.
Sources: morty.sh:267-285, cli/src/execution/prompt.ts:47-51
Boundaries: Exclusion Zones
Boundaries define the "never-touch" list for the AI. This is critical for protecting legacy code, sensitive configuration files, or dependency lockfiles from accidental modification by the autonomous agent.
Enforcement Mechanism
Boundaries are presented to the AI as a strict prohibition. In the prompt construction phase, these are formatted under the ## Boundaries section with the explicit instruction: "Do NOT modify these files/directories".
Key features include:
- Safety: Prevents the AI from refactoring unrelated code.
- Consistency: Ensures that critical infrastructure (like migrations) is only handled by humans or specific workflows.
- Flexibility: Supports directory-wide exclusions (e.g.,
src/legacy/**).
Sources: morty.sh:287-299, cli/src/execution/prompt.ts:54-58, README.md:72-76
Implementation and Management
Morty provides both CLI commands and programmatic interfaces to manage these settings.
Management Commands
| Command | Function | Source |
|---|---|---|
morty --init | Automatically detects project settings and creates the initial config. | morty.sh:134-219 |
morty --add-rule "text" | Appends a new rule to the existing config.yaml. | cli/src/config/writer.ts:102-120 |
morty --config | Displays the current rules and project context in the terminal. | morty.sh:233-255 |
Code Implementation (Initialization)
The initialization process uses a detector to guess the project's language and framework, then writes the initial rule templates.
// cli/src/config/writer.ts:81-100
export function initConfig(workDir = process.cwd()): {
created: boolean;
detected: ReturnType<typeof detectProject>;
} {
const mortyDir = getMortyDir(workDir);
const configPath = getConfigPath(workDir);
const detected = detectProject(workDir);
if (!existsSync(mortyDir)) {
mkdirSync(mortyDir, { recursive: true });
}
const configContent = createConfigContent(detected);
writeFileSync(configPath, configContent, "utf-8");
// ...
}
Sources: cli/src/config/writer.ts:81-100, morty.sh:134-219
Conclusion
Rules and Boundaries are the primary safety and quality control mechanisms in the Morty ecosystem. By combining project-wide rules with strict file boundaries, Morty allows developers to leverage autonomous AI agents while ensuring the resulting code remains within the established technical and architectural "guardrails" of the project.