Introduction

DeepSeek Harness (DSH) provides string-level edit and write tools, but lacks a tool for writing structured unified diffs to disk. The apply-patch plugin fills this gap: it parses unified diffs, applies each patch hunk, guarantees atomicity (all-or-nothing), and records reverse patches for precise undo.

Core Features

The plugin provides full git diff coverage, including multi-file, multi-hunk, renames, copies, permission changes, binary file labels, and quoted paths containing spaces.

It uses a self-implemented parser that does not rely on heavyweight libraries and strictly validates the actual line counts in patch hunk headers, raising errors for malformed input.

When locating patch hunks, it supports exact matching, line offset correction, and fuzzy matching (GNU-patch style), while ensuring that deletions are never incorrectly applied during fuzzy matching.

Operations are atomic: before anything is written, every file is parsed and validated in memory. Any patch conflict or structural violation results in zero writes.

It includes version control, using the same identifier recipe as the official DSH filesystem (dev:ino:size:mtimeNs:ctimeNs), and rechecks the identifiers at commit time, aborting if drift is detected.

It computes reverse patches before making changes and writes an undo log before writing. undo restores a byte-for-byte identical state. This applies to creation, deletion, rename, copy, and mode changes.

It supports dry-run mode, which statically validates applicability and reports conflicts without touching the disk.

It preserves CRLF/LF fidelity, models files line by line including their terminators, and ensures untouched regions round-trip byte-for-byte.

It provides binary safety: files marked as binary are skipped, text patches targeting binary targets (NUL bytes / invalid UTF-8) are detected and skipped, without corrupting files.

Install and Enable

The plugin is installed with the following command:

dsh plugin --profile <name> add github:JohnXu22786/apply-patch

After installation, four tools are immediately available in the Harness tool registry.

Typical Usage

The plugin offers three primary usage modes: DSH tools, CLI, and a library.

DSH Tools

Use the following tools in DSH configuration or prompts:
* patch_apply: apply unified diffs with atomicity and undo-log recording.
* patch_dry_run: validate applicability, report conflicts, and do not write to disk.
* patch_reverse: generate a reverse patch (after validation) without applying it.
* patch_stat: summarize a patch (files, hunks, +/-/context counts) without touching the filesystem.

CLI

Use it as a standalone command-line tool:

dsh-patch apply changes.patch --root /workspace
dsh-patch dry-run changes.patch
dsh-patch reverse changes.patch > revert.patch
dsh-patch undo .dsh-patch-undo.json

Library

Import it in code for programmatic use:

import { applyPatchText, applyUndo, parsePatch, statPatch } from 'dsh-patch-apply'

Use Cases and Considerations

The plugin has zero runtime dependencies and uses TypeScript source with the Node.js built-in test runner.

Files are modeled line by line with their respective terminators; untouched regions round-trip byte-for-byte.

Binary safety: files marked as binary are skipped; text patches targeting binary targets are detected and skipped.

Note that the plugin runs with the permissions of the current DSH process. Review the source code and license before installing.

Conclusion

apply-patch provides a reliable, atomic, and reversible solution for diff application in the DSH ecosystem.