Three-way merge for things that are not text files
Elastic ships several hundred detection rules with its security product. A detection rule is a saved query that runs on a schedule against your logs and raises an alert when it matches, along the lines of "a shell process spawned from a Microsoft Office document on Windows". That is the SIEM side of the product, SIEM meaning security information and event management. Elastic writes those rules, ships them as a package, and updates them as attacker behaviour and log field names change. They are called prebuilt rules, and you browse and edit them in Kibana, the web application on top of Elasticsearch.
A rule written for a generic environment produces false positives in a specific one, so users wanted to narrow the query, add an exception, lower the severity, or point the rule at different indices. Until the end of 2024 they could not, not without paying a price. Prebuilt rules were read-only. You could clone one and edit the clone, but from then on you owned it forever, because a clone never received another update from Elastic.
I worked on the Rule Management area of Kibana Security Solution from August 2022 to December 2024, and removing that trade-off was the last large thing I did there. It means taking on an obligation. The user edits a record, we keep shipping new versions of the same record, and both sets of changes have to survive. That is git merge, except a rule is not a text file.
All of it is public. There is a design document, and the implementation is a run of merged pull requests in elastic/kibana. The work finished in December 2024 and the code has moved since, so treat the specifics as a snapshot.
Why you cannot just diff the document
The obvious idea is to serialise all three versions of the rule to JSON and run a line-based three-way merge over the text. It fails for two separate reasons.
A rule has roughly forty fields, and which fields exist depends on the rule type. A rule written in EQL, Elastic's Event Query Language for matching ordered sequences of events, has event_category_override and tiebreaker_field. A plain query rule has saved_id. A machine learning rule has neither and carries machine_learning_job_id. A text merge has no idea these are different shapes. It sees indentation.
And it will happily produce output that is syntactically fine and semantically wrong. Interleaving two versions of a severity enum can yield a value that is not in the enum. Nothing in the merge knows that risk_score: 73 is one value rather than two digits to be taken from different sides.
So the merge runs field by field, and each field type needs its own definition of what a change is and what a conflict is. In the design document that section ran 831 lines, about a third of the whole thing, which says where the argument sat.
Three versions, one of which may not exist
Every field has three versions. The original version is what Elastic first shipped, before the user touched anything. The user's version is what they have now, including their edits. The incoming version is the one Elastic is publishing. In the API these are spelled BASE, CURRENT and TARGET, and BASE matches git's merge base.
The general heuristic is short:
incomingVersion === originalVersion && userVersion !== originalVersion
keep the user's version, no conflict
userVersion === originalVersion && incomingVersion !== originalVersion
take the incoming version, no conflict
originalVersion, userVersion and incomingVersion all differ
conflict, and the field type decides what the merge proposes
Those first two cases cover most fields on a real upgrade. The third is the whole problem.
The model also has to carry the case where the original version is missing. A user who upgraded from a Kibana version predating our storage of the shipped rule has nothing to compare against, and a two-way comparison is a different problem with different answers. So the diff-algorithm rework added a has_base_version flag to each field's diff, rather than pretending the original equals the user's copy.
A conflict is not a boolean
The first model had has_conflict: boolean on each field diff. That is the natural thing to write and it is not enough. The same diff-algorithm rework replaced it with three states. NONE means the two sides did not collide. SOLVABLE means they collided, the algorithm produced an answer it stands behind, and the user should look at it anyway. NON_SOLVABLE means both sides changed the same thing and a human has to choose.
The detail I still like is that the permitted values differ by algorithm. A single-line string can only be NONE or NON_SOLVABLE, because there is no honest way to merge two different one-line values. An array of scalars can only be NONE or SOLVABLE, because set union is always defined. Multi-line strings are the only type where all three are reachable. The field type decides which outcomes are expressible at all, and putting that in the contract lets a client reason about what it will be shown.
One rule per field type
Single-line strings, for example name and query. No merging, ever. Merging keyword-ish values risks producing something that is not a legal value at all. A changed rule name usually signals that the rule's meaning changed, so if both sides renamed the same rule, the two versions have drifted apart and the user should read both. The conflict is the feature.
Multi-line strings, for example description, setup and the investigation guide. The only case where ordinary text merging applies. We used node-diff3, a JavaScript implementation of the diff3 algorithm behind command-line merges. If the user rewrote the first line and Elastic extended the second, the library returns a clean merge, marked SOLVABLE so it surfaces without blocking the upgrade. If both sides rewrote the same lines, the library returns conflict regions, we mark the field NON_SOLVABLE and propose the user's version.
Numbers, for example risk_score and max_signals. A number is one unit and cannot be broken up by digit, so the only possible conflict is the case where all three differ, and it is never solvable. We keep the user's value, on the reasoning that someone who set a risk score meant it.
Arrays of scalars, for example index, tags and references. A real merge is always available here, because the array behaves like a set. Compute what each side added and removed relative to the original, combine, and apply the result to the original. difference and union below are lodash's set helpers. This is the corrected snippet, for a reason I get to in a moment.
const addedByUser = difference(userVersion, originalVersion);
const addedByElastic = difference(incomingVersion, originalVersion);
const removedByEither = union(
difference(originalVersion, userVersion),
difference(originalVersion, incomingVersion)
);
const merged = difference(
union(originalVersion, addedByUser, addedByElastic),
removedByEither
);
With an original of ['linux', 'network'], a user version of ['windows', 'host'] and an incoming version of ['linux', 'ml'], that gives ['windows', 'host', 'ml']. Both sides' additions survive, and anything either side dropped stays dropped.
A confession about that snippet. The version inside the merged design document has a bug: it takes the union with only one side's additions, so it does not produce the output the surrounding prose claims. Nobody caught it in four months of review, me included. The tables of worked cases, and later the unit tests, carried far more weight than the sample code did.
Arrays of objects, for example related_integrations and required_fields. Here the object is the unit, not the fields inside it. Take a required field entry of { name: 'host.os.type', type: 'unknown' }. If Elastic changes type to keyword while the user changed name to something else, applying the type change to the renamed field is a guess, because nothing says the new type still describes the field the user now means. So we index each array by a key, name for required fields, package plus integration for related integrations, and handle additions and removals the way the scalar case does. An object modified on both sides conflicts unless both modified it identically.
Fields that must never merge at all
Not every field belongs in the algorithm. enabled always keeps the user's value, because Elastic has no opinion about whether a rule is switched on in your deployment. So do actions and exceptions_list. In the other direction, version and updated_by always take Elastic's, and nobody is offered a choice.
Those decisions came from a table with one row per rule field, and that table turned out to be the real specification of the endpoint. The change that pulled fields out of the diff entirely is a net deletion of 679 lines. Deciding a field is not our business is cheaper than writing a rule for it.
The list of fields that do take part is derived from the schemas rather than written out by hand. A hand-written list would have been correct the day it was written and quietly wrong the first time anyone added a rule type.
The API has to express all of this in one call
The upgrade endpoint takes a pick_version at the rule level, with values BASE, CURRENT, TARGET, MERGED and RESOLVED, plus per-field overrides. A client can say "take Elastic's version of everything except the query, where I want mine, and the severity, where here is a value I typed". That implementation came to +3,202 / -268 across 43 files and stayed open for seven weeks.
Rule upgrade moved from PATCH semantics to PUT semantics. Under PATCH, a field that disappears from the new Elastic version is left alone in the user's rule. Under PUT, it goes. A user who added a timeline_id to a prebuilt rule that never had one, and then accepts a full upgrade without resolving field by field, loses it. That is correct for a merge, and it is also a surprise, which is why the pull request body spells it out.
The other cost is diffing on a shape that is not the storage shape. The API contract groups related fields. index and data_view_id become one data_source field with a type discriminator, and query plus language plus filters become eql_query or esql_query depending on the rule type. Grouping is right for the merge, because those fields only mean anything together. But what you write back is the flat, type-specific document, so there is a mapping layer in both directions.
That layer is where the bug landed. A merged upgrade could produce a payload carrying both index and data_view, which is not a legal rule, so the upgrade failed. The fix was fifty-nine added lines and zero deleted. The merge logic was fine. The translation back from the grouped shape did not check which branch of the discriminated union it was on, so it never cleared the other field. The grouped view was worth having. The round trip out of it needed its own tests, and what it got was a code review.
What I would keep
What generalises is the contract, more than the algorithm. The field type decides the merge rule, so the set of possible outcomes differs per type, and that difference belongs somewhere clients can read it. A boolean has_conflict flattens it and leaves the interface guessing.
The other thing I would keep is the table. One row per field, an explicit decision in each, including the decision that a field takes no part in the merge at all. It did more work than the code did, and it is the artefact I would write first next time.
Once the feature was switched on, the endpoint's default pick moved from 0 to 1 . That default is the whole project in one line. Your changes and ours should both survive, and where they cannot, we will name the exact fields.