#3238·CowAgent

[Feature] Surface the file diff of edit / write as a diff view in the Web console and Desktop app

Author: 592774447Created Sep 23, 2026Updated Sep 23, 2026

Self check

What's the problem?

The diff is already generated on the backend and then thrown away before it reaches a human.

agent/tools/edit/edit.py already computes a unified diff and returns it in the tool result:

python
# agent/tools/edit/edit.py:199-212
# Generate diff
diff_result = generate_diff_string(base_content, new_content)

result = {
    "message": message,
    "path": path,
    "diff": diff_result['diff'],
    "first_changed_line": diff_result['first_changed_line']
}

generate_diff_string() (agent/tools/utils/diff.py:280) produces a standard unified diff via difflib.unified_diff. So the expensive and error-prone part — knowing the old content, knowing the new content, producing a correct diff — is already solved upstream. What is missing is only the last mile: getting it to the screen.

Today, on the way to the UI, that field is flattened into a truncated string and rendered as a plain <pre>:

python
# channel/web/web_channel.py:1170-1178
result_str = str(result)
if len(result_str) > 2000:
    result_str = result_str[:2000] + "…"
javascript
// channel/web/static/js/console.js:7610
const resultStr = tc.result ? escapeHtml(String(tc.result)) : '';
// ...
// console.js:7626 — rendered as: <pre class="tool-detail-content">${resultStr}</pre>

So the practical outcome is:

  1. The user sees a Python repr() of a dict, not a diff. The unified diff is inside that repr, escaped and wrapped in {'message': ..., 'diff': '...', 'first_changed_line': ...} — with \n shown as literal \n, since it is str(result) of a dict, not the diff itself.
  2. It is hard-truncated at 2000 characters, so for any non-trivial file the diff is cut off exactly where it becomes interesting.
  3. Nothing distinguishes a file modification from any other tool call. The tool step is a collapsed row that says edit — the user has to expand it and squint at a dict repr to find out which file changed and what changed in it.

Why this matters more than it looks

For an agent that is allowed to edit files on a real machine, "what did it actually change?" is not a nicety — it is the precondition for trusting it with write access at all. When a user asks the agent to update a config or a script, the current UI gives them no way to confirm the change without going to the shell and reading the file by hand — which defeats the point of having the agent do it.

The write tool is worse: it returns no diff at all (agent/tools/write/write.py:101-105 returns only message, path, bytes_written), so a full-file overwrite is completely invisible.

Desired behavior

Detect that a completed edit / write tool call produced a file change and render it as a proper diff view attached to that tool step:

  • Show which files changed, per tool call and as a per-session summary ("3 files changed in this conversation").
  • Show what changed — a real diff rendering, not a dict repr: added/removed line highlighting, line numbers, context.
  • Structured data, not a string. Send path, diff, first_changed_line as separate fields so the frontend can render them; do not flatten the result dict into a truncated string.
  • No aggressive truncation. A diff is the payload here, not a trace. If a cap is needed for very large diffs, make it a rendering concern (collapse by default, "show full diff") rather than a transport-level [:2000] cut.
  • Graceful degradation — if the diff is missing or unparseable, fall back to today's plain output; never render blank or break the step row.
  • write should produce a diff too, at least for the overwrite case (it already reads previous content at write.py:78-84 to run the syntax check, so the old content is in hand at no extra cost).
  • Consistent with the existing step UI — collapsible, muted, matching the current tool-step styling; a diff card that is expanded by default only when the tool actually changed a file.

Explicitly out of scope (for the maintainers to decide, not assumed here)

  • A side-by-side / split view, inline character-level highlighting, syntax highlighting inside the diff, click-to-zoom, or an "Export" button.
  • Any write-back / revert button. A diff view should be read-only. Rolling a change back is a separate, destructive feature and should not ride along with a display change.
  • Capturing changes made via bash (sed -i, > redirects, scripts). That is a different problem class — it needs a filesystem-level mechanism rather than a tool-level one — and should not block the tool-level feature, which covers the normal edit / write path.

Current-state investigation (v2.1.9)

Anchors verified by reading the source:

Location Notes
agent/tools/edit/edit.py:199-212 The diff already exists. generate_diff_string() is called and diff + first_changed_line are returned in the tool result. This is the whole reason the feature is cheap.
agent/tools/utils/diff.py:280 generate_diff_string() — difflib.unified_diff over old_content / new_content, plus a regex that extracts first_changed_line from the @@ hunk header.
agent/tools/write/write.py:101-105 The write result carries no diff — only message / path / bytes_written. But previous content is already read at write.py:78-84 for the syntax check, so the old content is available for free.
channel/web/web_channel.py:1166-1178 The tool_execution_end handler. result is stringified and truncated to 2000 chars (result_str[:2000] + "…") before being put on the SSE payload as result. This is where the diff is destroyed.
channel/web/web_channel.py:1186-1194 There is already a precedent for a structured, larger-budget field: display (for tools that write an outcome for a person) gets MAX_DISPLAY_STREAM_CHARS = 32 * 1024 (web_channel.py:1109) instead of 2000. A diff field could follow the same pattern.
channel/web/static/js/console.js:7610-7626 renderToolCallsHtml() — the historical-message renderer. Output goes into a <pre class="tool-detail-content"> as escaped text.
channel/web/static/js/console.js:6951-6998 The live tool_end handler — fills .tool-live-output via textContent = item.result, or renders item.display as markdown. Note the existing convention: a tool that produced something for a person keeps its step expanded (toolEl.classList.toggle('expanded', !!item.display)). A diff is exactly this kind of payload.
desktop/src/renderer/src/components/MessageSteps.tsx The desktop tool-step renderer (197 lines). Same collapsible-row shape as the web console.
desktop/src/renderer/src/types.ts:289-333 The desktop SSE event types — tool_end with result / display / permission_denied / permission_mode. A diff field would be added here.
desktop/src/renderer/src/components/Markdown.tsx Self-documented as aligned 1:1 with the web console, so both ends need this together to stay in sync.

No repo-wide hits for a diff renderer in the frontend — the field is generated and never consumed.

Technical considerations

  • Transport shape. Prefer a dedicated diff object on the tool_end payload ({path, diff, first_changed_line}) rather than embedding it in the truncated result string. The existing display field is the precedent for a structured field with a larger budget.
  • Rendering. A unified diff is the natural input for an off-the-shelf renderer, and keeping the backend on difflib.unified_diff means no new diff algorithm is needed. If the project prefers a dependency-free approach, a minimal renderer over +/-/ prefixed lines is enough for a first version — the data is already in the right format.
  • Streaming. Do not attempt to render a diff while the tool is still running. tool_execution_start → tool_execution_progress → tool_execution_end already gives a clean boundary; render on tool_end only. tool_progress messages can be large, so a partial diff would thrash.
  • Idempotency. The live handler and the historical renderer (renderToolCallsHtml) are two separate code paths and must both handle the new field, or the diff will appear while live and vanish on reload. (This is the same class of bug as the web/desktop drift the Mermaid work had to handle.)
  • Size. A diff can be larger than the file for a rewrite. The 2000-char cap exists for a reason; the fix is to raise it for the diff specifically (like display) and let the UI collapse it, not to send unbounded payloads.
  • Permissions / secrets. The edit tool already refuses credential paths (edit.py:82-83) and the result's diff context lines are noted in-code as a secret-exposure vector — any new field must respect the same guard.

Environment

  • CowAgent version: 2.1.9
  • Deployment: bare-metal on a Linux server (python3 app.py), channel feishu + Web console
  • Browser: Chromium

Notes

  • The core of this request is a display fix, not a diff engine. The backend already computes a correct unified diff; the feature is "stop discarding it and render it". That should keep the change small and contained.
  • Desktop is deliberately in scope, for the same reason as the Mermaid request: the desktop renderer documents itself as 1:1-aligned with the web console, so a web-only patch would let the two drift permanently.
  • bash-mediated edits are called out as out of scope on purpose. They are a real gap, but they need a different mechanism and should not block the tool-level path that covers ordinary agent edits.
  • No revert button is requested. Seeing the change and undoing the change are different features with different risk profiles.
  • Every line number, path and function name above was read from the local source; maintainers can navigate straight to each anchor.

What would you like?

No response

Contribution

  • I'd be interested in helping implement this.