Skip to content

feat(cli): unified diff display for edit_file / write_file / multi_edit (#1251)#2173

Merged
esengine merged 1 commit into
esengine:mainfrom
Bernardxu123:feat/cli-write-file-diff
May 29, 2026
Merged

feat(cli): unified diff display for edit_file / write_file / multi_edit (#1251)#2173
esengine merged 1 commit into
esengine:mainfrom
Bernardxu123:feat/cli-write-file-diff

Conversation

@Bernardxu123

Copy link
Copy Markdown
Collaborator

Summary

Add configurable diff display mode for file-editing tools in CLI, addressing issue #1251.

Problem

Users had to run git diff after every file edit to see what changed — especially painful when working without git.

Solution

1. Enhanced write_file output with unified diff

write_file now returns diff-enriched output matching edit_file's format:

  • New file: created path (N chars)
  • Overwrite (no changes): edited path (0→0 chars)
  • Overwrite (with diff): edited path (oldLen→newLen chars) + unified diff
  • Large file (>5000 lines / >100KB): summary only, no diff (performance protection)

Desktop's existing parseEditResult can now parse write_file results too — bonus.

2. /diff command

/diff → show current mode (default: summary) /diff summary → path + stats (edited path +N -M) /diff full → unified diff with colored +/- lines /diff none → checkmark only

3. ToolCard diff-aware rendering

In ull mode, the ToolCard renderer parses unified diff from the tool output and renders with colored +/- lines, hunk headers, and file stats in the card meta. In summary and
one modes, existing behavior is preserved.

Files Changed

File Change
src/tools/fs/edit.ts Add generateWriteDiff() function
src/tools/filesystem.ts write_file uses generateWriteDiff for diff-enriched output
src/config.ts Add DiffDisplay type + loadDiffDisplay / saveDiffDisplay
src/cli/ui/cards/ToolCard.tsx Diff-aware rendering based on config
src/cli/ui/slash/handlers/diff.ts New /diff command handler
src/cli/ui/slash/commands.ts Register /diff command
src/cli/ui/slash/dispatch.ts Import diffHandlers
src/i18n/EN.ts English translations for /diff
src/i18n/zh-CN.ts Chinese translations for /diff

Design Decisions

  • No new Card type — ToolCard is kept as-is with enhanced rendering; avoids breaking dashboard messages, session hydration, and transcript replay
  • Large file protection — LCS diff is O(n*m); skipped for files >5000 lines or >100KB
  • Config persistence — diffDisplay saved to ~/.reasonix/config.json via /diff command
  • Format alignment — write_file output matches edit_file format so Desktop's parseEditResult also works

Verification

pm run lint — passed

pm run typecheck — passed

pm run test — 3851 tests passed (299 files), 0 failures

Add configurable diff display mode for file-editing tools in CLI,
addressing issue esengine#1251 (edit_file/write_file diff without git).

## Changes

### Tool output enhancement
- write_file now returns unified diff format matching edit_file:
  - New file: \created path (N chars)\
  - Overwrite: \edited path (old→new chars)\ + unified diff
  - Large file (>5000 lines / >100KB): summary only, no diff
- All three edit tools (edit_file, write_file, multi_edit) now produce
  consistent output that Desktop's parseEditResult can also parse

### /diff command
- \/diff\ — show current mode (default: summary)
- \/diff full\ — render unified diff with +/- colors in ToolCard
- \/diff summary\ — show path + stats line (edited path +N -M)
- \/diff none\ — show only tool name and checkmark

### Config
- New DiffDisplay type in config.ts (\summary | full | none\)
- Persisted to ~/.reasonix/config.json via /diff command
- loadDiffDisplay / saveDiffDisplay functions

### ToolCard renderer
- In full mode, parse diff from tool output and render with colored
  +/- lines, hunk headers, and file stats in the card meta
- Summary and none modes preserve existing behavior

### Files modified
- src/tools/fs/edit.ts — add generateWriteDiff()
- src/tools/filesystem.ts — write_file uses generateWriteDiff
- src/config.ts — DiffDisplay type + load/save
- src/cli/ui/cards/ToolCard.tsx — diff-aware rendering
- src/cli/ui/slash/handlers/diff.ts — new /diff handler
- src/cli/ui/slash/commands.ts — register /diff command
- src/cli/ui/slash/dispatch.ts — import diffHandlers
- src/i18n/EN.ts — diff handler + slash translations
- src/i18n/zh-CN.ts — Chinese translations
@Bernardxu123 Bernardxu123 force-pushed the feat/cli-write-file-diff branch from c174152 to 7ff2069 Compare May 28, 2026 15:43

@esengine esengine left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Solid feature: write_file now emits the same unified-diff output format as edit_file (consistency), the large-file guard (WRITE_DIFF_MAX_LINES=5000 / MAX_BYTES=100KB[diff too large] summary) prevents dumping huge diffs into the transcript/context, the /diff summary|full|none command + loadDiffDisplay/saveDiffDisplay config defaults to the concise summary, and parseDiffFromOutput colorizes the card only in full mode. Tests + CI green.

One non-blocking note for later: diffDisplay is consumed UI-side only (ToolCard) — the tool output always includes the diff (model-facing, capped by the large-file guard) regardless of mode. So /diff none hides the card but the model still receives the diff. That's consistent with edit_file's existing behavior and arguably desirable (the model benefits from seeing its change), but the name none could read as 'no diff at all'; if you ever want a true token-saving mode, gating the tool output on diffDisplay would be the follow-up. Merging as-is.

@esengine esengine merged commit 8131e24 into esengine:main May 29, 2026
7 checks passed
esengine pushed a commit that referenced this pull request May 29, 2026
- auto-git-rollback: write_file output changed from 'wrote N chars' to
  'edited ... (M→N chars)' unified-diff format (#2173); update regex.
- slash + ui-slash-suggestions: Telegram /telegram command added by #2168
  brings the release command count from 43 to 44; update both assertions.
@Bernardxu123 Bernardxu123 deleted the feat/cli-write-file-diff branch May 29, 2026 23:30
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.

2 participants