fix(engine): add cross-process merge guard to prevent concurrent merges

Multiple engine processes (dashboard + serve) share the same SQLite database
but each has its own in-memory merge queue. Without a cross-process check,
two processes can start merging different tasks simultaneously.

Added store.getActiveMergingTask() as a DB-level check before any merge
starts. The drainMergeQueue defers with pollIntervalMs delay, and both
aiMergeTask and processPullRequestMergeTask have safety-net checks.
Also moved stale merge status cleanup to run regardless of autoMerge setting.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-13 20:21:58 -07:00
parent 827c0370c3
commit f9f7aff3ec
7 changed files with 70 additions and 3 deletions

View File

@@ -1298,6 +1298,14 @@ export async function aiMergeTask(
}
// 5. Execute merge with retry logic
// Cross-process safety net: abort if another task is already mid-merge.
// The engine's drainMergeQueue also checks, but this catches direct callers.
const activeMerge = store.getActiveMergingTask(taskId);
if (activeMerge) {
throw new Error(
`Cannot merge ${taskId}: task ${activeMerge} is already merging (cross-process conflict)`,
);
}
await store.updateTask(taskId, { status: "merging" });
// Normalize explicit verification commands from settings