Skip to content

Add text diff apply and three-way merge#275

Merged
JE-Chen merged 1 commit into
devfrom
feat/text-diff
Jun 21, 2026
Merged

Add text diff apply and three-way merge#275
JE-Chen merged 1 commit into
devfrom
feat/text-diff

Conversation

@JE-Chen

@JE-Chen JE-Chen commented Jun 21, 2026

Copy link
Copy Markdown
Member

Summary

difflib generates a unified diff but the standard library cannot apply one, and there was no three-way merge — so updating a .received artifact, replaying a recorded text edit, or merging two edits had no headless primitive. This adds the missing pieces (complements utils/json_patch for structured JSON; this is line-based text).

  • unified_diff(a, b) — wraps difflib.
  • apply_unified(text, diff) — the missing applier: walks each @@ hunk, verifies context/removed lines, raises PatchApplyError on 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. Returns MergeResult(text, conflicts, clean).

Pure stdlib (difflib+re).

Five-layer wiring

  • Headless core: je_auto_control/utils/text_diff/
  • Facade: re-exported from __init__.py + __all__
  • Executor: AC_unified_diff, AC_apply_unified, AC_three_way_merge
  • MCP: ac_unified_diff, ac_apply_unified, ac_three_way_merge
  • Script Builder: three "Text:" commands under Data

Tests & docs

  • test/unit_test/headless/test_text_diff_batch.py (11 tests: round-trip, context mismatch, clean/conflict/identical merges)
  • v67 feature docs (EN + Zh) + toctree
  • What's-new in all three READMEs

Lint clean: ruff / pylint / bandit / radon.

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.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 61 complexity · 0 duplication

Metric Results
Complexity 61
Duplication 0

View in Codacy

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.

@JE-Chen JE-Chen merged commit e076552 into dev Jun 21, 2026
16 checks passed
@JE-Chen JE-Chen deleted the feat/text-diff branch June 21, 2026 00:59
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant