synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / docs / D1_MERGE_TIER_T1.md
| 1 | # D1: Merge tier T1 (region) |
| 2 | |
| 3 | ADR 0003 section 9 step D1 (issue aien-dev/synapse#39), section 5.5 merge tiers: a T1 merge combines revisions that change different regions of the same file. Path-level merging (T0, B5) refuses them. |
| 4 | |
| 5 | Code: |
| 6 | |
| 7 | * `crates/synapse-core/src/merge.rs`: `region_merge`, the line-level three-way merge. |
| 8 | * `crates/synapse-core/src/derivation.rs`: `MergeTier::T1Region`, tier tag 1, `t1_mutations`, and the `DerivationTrees` blob methods. |
| 9 | * `crates/synapse-api/src/derivation.rs`: blob preload for derivation, admission and verification. |
| 10 | * `crates/synapse-store/src/revision.rs`: the store writes the merged blob with the derived revision. |
| 11 | |
| 12 | Tests: |
| 13 | |
| 14 | * `merge::tests::d1_*` in synapse-core. |
| 15 | * `crates/synapse-api/tests/d1_merge_tier_t1.rs` (`d1_api_*`). |
| 16 | * `merge_t1::tests::d1_*` in synapse-bench: the comparison with Git. |
| 17 | |
| 18 | Evidence: `evidence/d1-merge-tier-t1/`. |
| 19 | |
| 20 | ## 1. The merge rule |
| 21 | |
| 22 | `region_merge(base, versions)` merges the versions of one file that several revisions wrote against the same base version. The result does not depend on which line diff is chosen. |
| 23 | |
| 24 | 1. **Forced lines.** For one version, a base line is *forced* when every longest common subsequence of base and version matches it, and always to the same version line. It is computed from a prefix table and a suffix table of LCS lengths. Base line `i` is forced when: |
| 25 | * no version position `j` gives `prefix[i][j] + suffix[i+1][j] == total`, so skipping the line always loses a match; and |
| 26 | * exactly one `j` gives `prefix[i][j] + 1 + suffix[i+1][j+1] == total`. |
| 27 | 2. **Anchors.** The anchors are the base lines forced for every version. |
| 28 | 3. **Segments.** The anchors cut the base and every version into matching segments. In each segment at most one version may differ from the base, and the merged file takes that version's segment. If two versions change one segment, the merge is refused (`RegionConflict::Overlap`). |
| 29 | |
| 30 | Why it agrees with Git: |
| 31 | |
| 32 | * Every minimal diff matches every anchor, so one version's changed regions stay inside its changed segments. |
| 33 | * Two versions' regions are then always separated by at least one unchanged anchor line. That is the condition Git's three-way merge needs for a clean result, and the clean result is the same file. |
| 34 | |
| 35 | The brute-force test `d1_forced_matches_are_exactly_the_pairs_every_optimal_alignment_shares` checks the forced-line computation against every optimal alignment on small inputs. |
| 36 | |
| 37 | A first design used one Myers diff and then widened each change to cover the lines the diff could have slid. The differential test against `git merge-file` found merges Git refuses, because a different minimal diff can re-pair lines far from the change. The forced-anchor rule replaced it; `d1_edits_that_some_minimal_diff_places_together_conflict` holds the counterexample. |
| 38 | |
| 39 | ## 2. In the derivation rule |
| 40 | |
| 41 | `DerivationRule::Merge { tier: MergeTier::T1Region }` is encoded with tier tag 1. The rule is the same B5 rule: the server derives it, the store recomputes it at admission (`check_derivation`), and replicas recompute it at verification. The attestation records the tier and the canonical merge order (strictly ascending `ChangesetId`), as for T0. |
| 42 | |
| 43 | Per path, `t1_mutations`: |
| 44 | |
| 45 | * takes a mutation that every source wrote identically once; |
| 46 | * refuses a path that lies below a path another source wrote; |
| 47 | * refuses a delete against a change ("deleted by one revision and changed by another"); |
| 48 | * refuses a file that several sources added with different content; |
| 49 | * refuses any version whose mode differs from the base mode (checked against the base, so the order of sources does not matter); |
| 50 | * otherwise runs `region_merge` on the base blob and each version's blob, and stores the merged blob through `DerivationTrees::put_blob`. |
| 51 | |
| 52 | The merged blob is new content that no source carried. The API preloads the blobs the rule reads (each upsert blob, and the base file at each mutated path, for T1 only). `admit_derived_revision` takes merged blobs that are not yet visible from `produced_blob`, checks their digest, and writes them in the same WAL record as the derived revision. Replay and verification recompute the same bytes. A forged T1 output, with a blob other than the recomputed one, is refused (`d1_api_forged_t1_merge_output_is_refused`). |
| 53 | |
| 54 | T0 and T1 merges of the same sources share one `ChangeId` (`merge_change_id`). They are revisions of the same change. |
| 55 | |
| 56 | ## 3. Acceptance |
| 57 | |
| 58 | | Acceptance (issue #39) | Result | |
| 59 | |---|---| |
| 60 | | The 3 disjoint-hunk pairs of the Gate 4 conflict scenario, refused by T0, now merge | 3 of 3 merged (`d1_gate4_conflict_scenario_t1_merges_match_git_on_all_253_pairs`); with the T1 arm switched to T0 (mutation `t1-is-t0`) the test fails | |
| 61 | | Every merged tree equals Git's merge result | 245 merged pairs, 0 differ from `git merge-tree --write-tree` on any touched path (blob digest and mode) | |
| 62 | | No pair `git merge-tree` reports as conflicting is merged (all 253 pairs) | 0 merged Git conflicts, 0 merge-tree errors. Per class, as (pairs, Synapse merged, Git clean): distinct paths (240, 240, 240), disjoint hunks (3, 3, 3), identical change (2, 2, 2), hotspot rewrite (6, 0, 0), modify/delete (1, 0, 0), file/directory collision (1, 0, 0) | |
| 63 | | Each result records its tier and merge order in its `DerivationAttestation` | 0 attestations without tier T1 and the canonical order; the API test checks the attestation's rule and inputs, and `verify_revision_log` checks every signature and recomputes every T1 record, also after a reopen and after checkpoint plus compaction | |
| 64 | |
| 65 | Also covered: |
| 66 | |
| 67 | * **Differential test against `git merge-file`.** `d1_region_merge_never_merges_what_git_merge_file_refuses` runs 10,000 random three-way cases (seed `0xD1`): |
| 68 | * 3,947 merged, every result byte-identical to Git's; |
| 69 | * 0 merged where Git conflicts; |
| 70 | * 1,255 refused that Git merges cleanly (see Limits). |
| 71 | * **Refusals.** Touching regions, modify/delete, and a mode change are refused end to end (`d1_api_t1_refuses_touching_regions_and_modify_delete`). Binary content, a missing final newline, and oversized files are refused (`d1_binary_missing_final_newline_and_oversized_files_are_refused`). |
| 72 | |
| 73 | Red before green: `evidence/d1-merge-tier-t1/red-green/red.txt` holds 10 mutations from `run.sh`, one per behaviour, and each turns its tests red. `green.txt` holds the passing runs. The behaviours: |
| 74 | |
| 75 | * forced-line check: unmatched lines, unique partner; |
| 76 | * one version per segment; |
| 77 | * tier tag decoding; |
| 78 | * T1 not being T0; |
| 79 | * identical mutations taken once; |
| 80 | * delete check; |
| 81 | * mode check; |
| 82 | * the store writing the merged blob; |
| 83 | * the API blob preload. |
| 84 | |
| 85 | ## 4. Limits |
| 86 | |
| 87 | * **The proof covers minimal diffs.** Git's default Myers diff is minimal except when its cost limit cuts in on very large inputs, and its histogram diff is not always minimal. Agreement there rests on the tests, not on the argument. |
| 88 | * **Conservative.** T1 refuses some merges Git makes cleanly (1,255 of 10,000 random cases): |
| 89 | * the same edit made by two versions in one region, unless the whole file is byte-identical; |
| 90 | * changes separated only by lines that some minimal alignment leaves unmatched (for example repeated lines), where Git's chosen diff keeps them apart. |
| 91 | |
| 92 | The refused cases were counted, not sorted by cause. |
| 93 | |
| 94 | A refusal is always safe: the revisions stay separate, and a later tier or a human resolves them. |
| 95 | * **Refused inputs.** Binary files (any NUL byte), files without a final newline, and base/version pairs larger than `MAX_ALIGNMENT_CELLS` (2^22 table cells, about 2,047 by 2,047 lines). |
| 96 | * **Modes and paths.** A mode change on a merged path, a delete against a change, and an add/add with different content are refused, never merged. |
| 97 | * **No performance figures.** The alignment is quadratic in the line count of each file, bounded by `MAX_ALIGNMENT_CELLS`. D1 has no section 6 hot-path acceptance, and nothing was measured. |
| 98 | * **`.crumb` not recompiled.** The orchestrator recompiles it at rebase. |