---
name: "Mergemaster marks stories as done without squash-merging code"
---

# Bug 205: Mergemaster marks stories as done without squash-merging code

## Description

The mergemaster agent consistently moves stories from 4_merge to 5_done without actually squash-merging the feature branch code onto master. This was observed across stories #199, #200, #201, #202, #203, #204 — all marked done with unmerged commits still on their feature branches.

## Root Cause

The new merge-queue worktree approach (introduced ~24 hours ago) has a race condition with the filesystem watcher:

1. `run_squash_merge()` creates a `merge-queue/{story_id}` branch at master's current HEAD
2. It creates a temporary worktree, runs `git merge --squash`, commits, and runs quality gates — all in the merge-queue worktree
3. Meanwhile, the **filesystem watcher auto-commits pipeline file moves** to master (e.g. "huskies: queue X for merge", "huskies: done Y")
4. By the time step 7 tries `git merge --ff-only merge-queue/{story_id}` on master, master has diverged and the fast-forward fails
5. `run_squash_merge()` returns `success: false` (correctly) but `gates_passed: true` (misleading)
6. The `merge_agent_work` caller correctly reports failure — but the mergemaster is a Claude Code agent, not the Rust pipeline. The agent sees the failure, gives up on the squash merge, and instead manually moves the story file to done without the code

The old approach (direct squash-merge on master) didn't have this problem because it was atomic — no separate branch/worktree and no fast-forward step that could be foiled by concurrent master commits.

## Key code locations

- `server/src/agents.rs:2561` — `run_squash_merge()` function
- `server/src/agents.rs:2837-2865` — fast-forward step that fails when master diverges
- `server/src/agents.rs:2694-2708` — "nothing to commit" early return also skips fast-forward

## Additional issue

The stale `.story_kit/merge_workspace` directory from failed merges blocks subsequent `merge_agent_work` calls with a "directory already exists" error, though there is cleanup code at line 2572-2573 that should handle this.

## How to Reproduce

1. Have multiple stories developed in parallel on feature branches from different points of master
2. Queue them for merge via 4_merge
3. Let mergemaster process them

## Actual Result

Stories are moved to 5_done with pipeline commit messages like "huskies: done ..." but the feature branch code is never squash-merged onto master. Feature branches retain unmerged commits.

## Expected Result

Mergemaster should squash-merge feature branch code onto master before marking stories as done. If the merge fails (conflicts, empty diff, etc.) it should report the failure rather than silently skipping the merge and marking the story as done.

## Acceptance Criteria

- [ ] Mergemaster performs git merge --squash of the feature branch onto master before moving a story to done
- [ ] If the squash merge results in conflicts, the story is NOT moved to done — it stays in 4_merge with an error reported
- [ ] If the squash merge results in an empty diff (code already on master), the story is moved to done but the feature branch is cleaned up
- [ ] Stale .story_kit/merge_workspace directories are cleaned up before attempting a new merge
- [ ] The mergemaster agent must use MCP actions (merge_agent_work, move_story_to_merge, etc.) to drive its process — it should never manually move story files between pipeline stages or mark stories as done by direct file manipulation
- [ ] Provide MCP actions for merge failure handling (e.g. reporting failure, retrying, leaving the story in 4_merge with an error status) so the agent doesn't need to improvise with manual file moves when merge_agent_work fails
