Add text diff apply and three-way merge#275
Merged
Merged
Conversation
difflib generates a unified diff but the stdlib cannot apply one, and there was no three-way merge. Add apply_unified (walks @@ hunks, verifies context, raises on mismatch) and a line-based three_way_merge (clean for non-overlapping edits, conflict markers otherwise). Complements json_patch for line-based text. Wired through the facade, AC_unified_diff/ AC_apply_unified/AC_three_way_merge executor commands, MCP tools and the Script Builder.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 61 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
difflibgenerates a unified diff but the standard library cannot apply one, and there was no three-way merge — so updating a.receivedartifact, replaying a recorded text edit, or merging two edits had no headless primitive. This adds the missing pieces (complementsutils/json_patchfor structured JSON; this is line-based text).unified_diff(a, b)— wrapsdifflib.apply_unified(text, diff)— the missing applier: walks each@@hunk, verifies context/removed lines, raisesPatchApplyErroron mismatch.three_way_merge(base, ours, theirs)— line-based: non-overlapping edits combine cleanly; overlapping divergent edits emit<<<<<<< / ======= / >>>>>>>conflict markers (clean=False); trivial cases (one side unchanged, identical edits) auto-resolve. ReturnsMergeResult(text, conflicts, clean).Pure stdlib (
difflib+re).Five-layer wiring
je_auto_control/utils/text_diff/__init__.py+__all__AC_unified_diff,AC_apply_unified,AC_three_way_mergeac_unified_diff,ac_apply_unified,ac_three_way_mergeTests & docs
test/unit_test/headless/test_text_diff_batch.py(11 tests: round-trip, context mismatch, clean/conflict/identical merges)Lint clean: ruff / pylint / bandit / radon.