/ai-cli-task merge — Merge Task Branch to Main
Merge a completed task's branch into main, with automated conflict resolution and verification.
Usage
code
/ai-cli-task merge <task_module_path>
Prerequisites
- •Task status must be
executing - •Latest
.analysis/file must contain an ACCEPT verdict (fromcheck --checkpoint post-exec) - •Dependency gate: All
depends_onmodules must meet their required status — simple string entries requirecomplete, extended{ module, min_status }entries require at-or-pastmin_status(see depends_on Format incommands/ai-cli-task.md). If any dependency is not met, merge REJECTS with error listing blocking dependencies and their current statuses
Merge Strategy
Phase 1: Pre-Merge Refactoring
- •Review code changes on task branch for cleanup opportunities (dead code, naming, duplication)
- •Commit cleanup:
-- ai-cli-task(<module>):refactor cleanup before merge
Phase 2: Merge Attempt
- •If worktree:
cd <project-root>first (worktree is locked to task branch) - •Checkout main (non-worktree) or already on main (worktree, from main worktree)
- •Attempt merge:
bash
git merge task/<module> --no-ff -m "-- ai-cli-task(<module>):merge merge completed task"
Phase 3: Conflict Resolution (if merge fails)
If merge conflict detected:
- •Analyze conflict markers in affected files
- •Resolve conflicts by applying the task branch's intent while preserving main's changes
- •Run verification: build check, test suite,
lsp_diagnostics - •If verification passes: commit merge resolution, proceed to Phase 4
- •If verification fails: abort merge (
git merge --abort), retry from Phase 2 with different resolution strategy - •Max 3 resolution attempts — after 3 failures → stay
executing, report unresolvable conflicts (user can manually resolve then re-run merge)
Phase 4: Post-Merge Cleanup
On successful merge:
- •Update
.index.mdstatus →complete, update timestamp - •Write
.summary.mdwith final task summary: completion status, plan overview, key changes, verification outcome, lessons learned (integrate from directory summaries) - •If worktree exists:
git worktree remove .worktrees/task-<module> - •Delete merged branch:
git branch -d task/<module> - •Commit state:
-- ai-cli-task(<module>):merge task completed
Execution Steps
- •Read
.index.md— validate status isexecuting - •Validate dependencies: read
depends_onfrom.index.md, check each dependency module's.index.mdstatus against its required level (simple string →complete, extended object → at-or-pastmin_status). If any dependency is not met, REJECT with error listing blocking dependencies - •Verify ACCEPT verdict: check latest
.analysis/file forpost-exec-accept - •Read
.summary.mdfor task context (plan overview, completed steps, key decisions) - •Phase 1: Task-level refactoring on task branch
- •Phase 2: Attempt merge to main
- •If conflict (Phase 3):
a. Parse conflict files
b. Attempt resolution (up to 3 tries)
c. Each resolution: fix conflicts → verify (build + test) → if pass commit, if fail abort and retry
d. If all 3 attempts fail → stay
executing, abort merge, report unresolvable conflicts - •Phase 4: Post-merge cleanup (status update →
complete, write.summary.md, worktree removal, branch deletion) - •Write
.auto-signalto the main worktree'sAiTasks/<module>/directory (NOT the task worktree's copy) — MUST be written AFTER Phase 4 status update tocomplete, so the daemon reads correct status when routing toreport. In worktree mode, the task directory exists in both locations; writing to main ensures the signal survives worktree removal. The daemon'sfs.watchMUST monitor the main worktree path - •Report merge result
State Transitions
| Current Status | After Merge | Condition |
|---|---|---|
executing | complete | Merge successful (with or without conflict resolution) |
executing | executing | Merge conflict unresolvable after 3 attempts (stays executing so merge can be retried after manual conflict resolution) |
Git
| Action | Commit Message |
|---|---|
| Pre-merge cleanup | -- ai-cli-task(<module>):refactor cleanup before merge |
| Merge commit | -- ai-cli-task(<module>):merge merge completed task |
| Conflict resolution | -- ai-cli-task(<module>):merge resolve merge conflict |
| State update | -- ai-cli-task(<module>):merge task completed |
.auto-signal
| Result | Signal |
|---|---|
| Success | { "step": "merge", "result": "success", "next": "report", "checkpoint": "", "timestamp": "..." } |
| Conflict | { "step": "merge", "result": "conflict", "next": "(stop)", "checkpoint": "", "timestamp": "..." } |
Notes
- •Merge is separated from
checkto isolate conflict resolution logic - •The 3-attempt limit prevents infinite resolution loops
- •Each resolution attempt includes full verification (build + test) to ensure resolved code is correct
- •On merge failure, status stays
executing(notblocked) so merge can be retried. The user should manually resolve conflicts and then run/ai-cli-task mergeagain - •After manual resolution, if the user has already merged manually, they can update
.index.mdstatus tocompletedirectly - •Pre-merge refactoring is optional — if no cleanup needed, skip directly to merge
- •Worktree signal race prevention: In worktree mode,
.auto-signalis written to the main worktree'sAiTasks/<module>/path (not the task worktree), ensuring the daemon can read it after worktree removal. The daemon MUST watch the main worktree path for all signal files - •Concurrency: Merge acquires
AiTasks/<module>/.lockbefore proceeding and releases on completion (see Concurrency Protection incommands/ai-cli-task.md)