Automatic Conflict Resolution
Relevant source files
The following files were used as context for generating this wiki page:
Automatic Conflict Resolution is a specialized subsystem in Morty designed to handle Git merge conflicts that arise during parallel execution. When multiple AI agents work on independent tasks in isolated worktrees, merging their respective branches back into the base branch can result in conflicts. Morty utilizes AI engines to intelligently analyze conflict markers, combine changes, and complete the merge process without manual intervention.
Sources: README.md:104-106, cli/src/execution/conflict-resolution.ts:43-48
System Architecture and Workflow
The conflict resolution system is primarily triggered during the "Merge Phase" of parallel task execution. When a standard git merge command fails due to conflicts, the system identifies the affected files and delegates the resolution to the active AI engine.
Conflict Detection and Triggering
The process begins in the parallel execution module, which iterates through completed agent branches. It attempts a non-fast-forward merge into the target branch. If the merge fails with a status indicating conflicts, the system retrieves a list of conflicted files using Git status utilities.
Sources: cli/src/execution/parallel.ts:252-270, cli/src/git/merge.ts:33-46
AI-Assisted Resolution Logic
Once conflicts are detected, the system constructs a detailed prompt for the AI engine. This prompt instructs the AI to:
- Read the conflicted files and locate markers (
<<<<<<<,=======,>>>>>>>). - Understand the intent of both the
HEADchanges and the incoming branch changes. - Rewrite the files to resolve conflicts while maintaining valid syntax and logic.
- Remove all markers and stage the resolved files.
Sources: cli/src/execution/conflict-resolution.ts:8-23, morty.sh:1115-1126
The following diagram illustrates the flow from conflict detection to final resolution:
The diagram shows the logic used to transition from a failed standard merge to an AI-driven resolution. Sources: cli/src/execution/conflict-resolution.ts:43-85, cli/src/git/merge.ts:162-185
Key Components and Functions
The system is implemented across several core modules that handle Git operations and AI orchestration.
Core Implementation Functions
| Function | File Path | Description |
|---|---|---|
resolveConflictsWithAI | conflict-resolution.ts | Orchestrates the AI engine to resolve conflicts in a specific directory. |
buildConflictResolutionPrompt | conflict-resolution.ts | Generates the technical instructions sent to the AI for merging code. |
getConflictedFiles | git/merge.ts | Uses simple-git status to return a list of files in a conflicted state. |
completeMerge | git/merge.ts | Stages resolved files and executes the final merge commit. |
abortMerge | git/merge.ts | Runs git merge --abort if AI resolution fails to leave the repo clean. |
Sources: cli/src/execution/conflict-resolution.ts:43, cli/src/git/merge.ts:133, cli/src/git/merge.ts:162
Integration with Parallel Execution
In parallel mode, conflict resolution is vital for synchronizing work from multiple isolated worktrees. If skipMerge is not enabled, the system automatically attempts to reconcile agent branches.
This sequence shows the interaction between the parallel execution controller and the conflict resolution logic. Sources: cli/src/execution/parallel.ts:265-285, cli/src/execution/conflict-resolution.ts:58-75
Prompt Engineering for Conflicts
The effectiveness of the resolution depends on the prompt sent to the AI engine. The prompt is designed to ensure the AI does not create a new history but completes the existing merge transaction.
Key Prompt Constraints:
- File Reading: AI must explicitly read markers to understand the diff.
- Syntactic Integrity: The resulting code must be valid and "logically correct".
- Commit Handling: The AI is instructed to run
git commit --no-editexactly once at the end of the process to preserve the standard Git merge message.
Sources: cli/src/execution/conflict-resolution.ts:14-25, morty.sh:1115-1126
Configuration and Control
Users can control the behavior of the automatic conflict resolution system through CLI flags and configuration files.
| Option | Type | Default | Description |
|---|---|---|---|
--no-merge | Boolean | false | Skips the automatic merge phase and conflict resolution entirely. |
skipMerge | Runtime Option | false | Internal flag mapped from --no-merge to prevent resolution logic. |
--create-pr | Boolean | false | If enabled, Morty creates PRs instead of merging locally, bypassing local conflict resolution. |
Sources: cli/src/cli/args.ts:48-63, cli/src/config/types.ts:98
Conclusion
Automatic Conflict Resolution in Morty allows for a truly autonomous coding loop by removing the most common bottleneck in parallel workflows. By combining Git's structural conflict detection with AI's semantic understanding of code changes, the system can reconcile complex overlapping modifications across multiple feature branches. If the AI fails to resolve a conflict with high confidence, Morty preserves the state and notifies the developer, ensuring safety and preventing code corruption.
Sources: cli/src/execution/parallel.ts:315-320, README.md:104-106