Skip to main content

file_write_diff

Function file_write_diff 

Source
pub fn file_write_diff(
    tool_name: &str,
    input: &Value,
    path: &str,
    before_state: Option<&str>,
) -> Option<String>
Expand description

Compute a unified diff string for a file-write tool invocation, given the raw tool input JSON. Handles Claude’s Edit / Write / MultiEdit / NotebookEdit shapes; returns None for any unrecognised shape or if nothing to diff.

Exposed so non-Conversation derivers (e.g. toolpath-claude’s bespoke Claude-JSONL deriver, which emits its own tool.invoke steps) can populate ArtifactChange.raw without reimplementing the diff logic.

Shapes handled:

  • Edit { old_string, new_string, ... } → diff old→new
  • Write { content } → diff before_state→content (uses "" when before_state is None, producing an addition-only hunk)
  • MultiEdit { edits: [{old_string, new_string}, ...] } → hunks joined, each prefixed with # edit N/total so consumers can tell them apart.

§before_state for Write

The Write tool replaces a file’s whole contents but the JSONL log doesn’t carry the prior state. Callers that can reconstruct it out-of-band (e.g. by reading git show HEAD:<path>) should pass it as before_state; the resulting diff shows honest -/+ lines for replaced content. When None, we fall back to diffing against the empty string — correct for new files, misleading for overwrites, but the best we can do from the log alone.

before_state is ignored for Edit / MultiEdit shapes, which already carry their own old_string/new_string pre-image.