Conflict Sentinels: Catching Clashes Between Merge Requests Before Main Does
Here is a failure every growing team hits. Alice renames get_user() to fetch_user() and updates every caller. Her merge request is green. Bob, on another branch, adds a new call to get_user() in a new file. His merge request is also green. Git merges both without a single conflict marker, and main is broken.
CI tests each branch against the main it started from, not against the other open branches. Git only detects textual conflicts, where two changes touch the same lines. A semantic conflict, where two changes are each correct but wrong together, passes straight through.
Everyone writes about CI pipelines. Very few people write about the layer above them: a conflict sentinel that watches all open merge requests together and warns about clashes before anything reaches main.
Three kinds of conflict
| Kind | Example | Who catches it today |
|---|---|---|
| Textual | Both branches edit the same lines | Git, at merge time |
| Build or semantic | A renames a function, B adds a call to the old name | Nobody, until main breaks |
| Behavioural | A changes a default timeout, B adds code that relies on the old value | Nobody, until production |
A sentinel cannot catch everything in the third row, but it can catch most of the first two days earlier than you would otherwise.
Layer 1: merge queues fix it at the end
The simplest fix is a merge queue: GitHub's merge queue, or GitLab's merge trains. Each merge request is tested on top of main plus everything queued ahead of it, so Alice and Bob's combination is tested before it lands. If you do not have one, turn it on first.
Merge queues have one weakness: they find the problem at the last moment. By then Bob has finished, moved on and lost context. A sentinel moves the warning to when the work is still fresh.
Layer 2: test-merge open branches against each other
Since Git 2.38, git merge-tree --write-tree can merge two commits without touching your working directory. That makes pairwise checks cheap:
import itertools
import subprocess
def merge_conflicts(base: str, a: str, b: str) -> list[str]:
"""Merge a into b in memory. Returns conflicted paths, or [] for a clean merge."""
r = subprocess.run(
["git", "merge-tree", "--write-tree", "--name-only", "--no-messages", a, b],
capture_output=True, text=True,
)
if r.returncode == 0:
return []
return r.stdout.splitlines()[1:] # first line is the tree id
def textual_clashes(branches: list[str]) -> list[tuple[str, str, list[str]]]:
hits = []
for a, b in itertools.combinations(branches, 2):
paths = merge_conflicts("origin/main", f"origin/{a}", f"origin/{b}")
if paths:
hits.append((a, b, paths))
return hits
Exit code 0 means a clean merge, 1 means conflicts. With 40 open merge requests that is 780 pairs, which still takes seconds, because no files are checked out.
Layer 3: find semantic conflicts with a symbol map
Textual checks miss the rename example entirely. For that you need to know which symbols each branch changes and uses:
- For each open branch, diff it against its merge base with main.
- Parse the changed files (tree-sitter works for most languages) and record two sets: symbols the branch defines, renames, deletes or changes the signature of, and symbols it newly uses.
- For every pair, flag it when one branch's changed definitions overlap the other branch's new uses.
def semantic_pairs(changes: dict[str, dict[str, set[str]]]):
"""changes[branch] = {"changed_defs": {...}, "new_uses": {...}}"""
for a, b in itertools.permutations(changes, 2):
overlap = changes[a]["changed_defs"] & changes[b]["new_uses"]
if overlap:
yield a, b, overlap
This also solves the n² problem: you only need to build and test the few pairs whose symbols overlap, not all 780.
Layer 4: build and test the risky pairs
For each flagged pair, create a throwaway merge commit of main + A + B and run a targeted job on it. Run the compiler or type checker (this alone catches most renames), plus the tests that cover the overlapping symbols. If you have test-impact data (which tests touch which files), use it. If not, the test files next to the changed modules are a good start.
Run this on a schedule (every hour, say) and whenever a merge request gets new commits. Cache results by the pair of commit SHAs, so nothing is tested twice.
Layer 5: tell people, in the right place
A sentinel nobody reads is useless. Post one comment on both merge requests:
Conflict sentinel: this MR and !482 (Bob, "Add audit export") break the build when merged together. !482 adds a new call to
get_user(), which this MR renames tofetch_user(). Failing check:mypy src/audit/export.py.
Name the other merge request, the symbol and the failing command. Update the comment instead of posting new ones, and resolve it automatically when the pair turns green.
What it costs and what it catches
On a mid-size repository, the textual layer and the symbol map are nearly free. The expensive part is building the flagged pairs, and there are usually only a handful each day. In my experience, the type checker on combined pairs catches most of the real problems: renames, signature changes, deleted helpers, moved modules.
What it will not catch is the behavioural row: a changed default, a different ordering, a new side effect. For those you still need good tests on main and a merge queue as the final gate. The sentinel is not a replacement for either. It turns "main is red, whose fault is it" into a comment two days earlier that says exactly which two changes to talk about.