dsh-encoding-guard: Encoding Guard

Introduction

DeepSeek Harness (dsh) has three specific issues with its official file tools when handling non-UTF-8 files: reading non-UTF-8 files fails without reporting the encoding; editing files with a UTF-8 BOM strips the BOM; and overwriting a CRLF file converts line endings to LF. These behaviors cause the Agent to corrupt file encoding when modifying files, leading to garbled text or massive diffs. dsh-encoding-guard extends the tools and hooks to preserve file encoding, BOM, and line endings at the byte level, and intercepts mojibake before writing.

Plugin Scope

This is a client-side plugin for DeepSeek Harness, maintained by qinghuidaqzp, designed to preserve file encoding integrity when the Agent modifies files. It addresses the problem of “non-UTF-8 / Windows projects easily having their encoding corrupted under the official file tools.”

Problems Solved

In testing with dsh 0.2.2-rc.1, the official file tools have three failure points for non-UTF-8 / Windows projects:

  • When reading/writing GBK / Big5 / Shift_JIS / EUC-KR / UTF-16 files, read only reports invalid UTF-8 text. Agents often fall back to shell commands, causing files to be converted to UTF-8 or written with garbled content. The plugin provides encoded_read / encoded_edit / encoded_write to read and write in the original encoding, and includes the encoding in error messages (e.g., GBK, CRLF line endings) along with a pointer to the encoded_* tools.
  • When editing files with a UTF-8 BOM (e.g., C# source, VS C++ source, PowerShell 5 scripts, CSV files for Excel), edit / write / apply_patch strip the BOM. The plugin automatically restores the BOM after modification and registers the new version with the official read-before-write policy to prevent subsequent edit calls from failing due to version mismatch.
  • When using write to fully overwrite a CRLF file, the file is converted to LF, causing git diff to show every line as changed. The plugin automatically preserves CRLF during write overwrites.
  • The plugin also intercepts mojibake writes: if the new content contains � (U+FFFD), 锟斤拷/烫烫烫, 涓枃娉ㄩ噴 (UTF-8 misdecoded as GBK), or é/’ (UTF-8 misdecoded as Latin-1), and these were newly introduced by the current modification, the write is rejected (configurable to prompt for approval or disable).

Core Features

Tools (callable by the model)

  • encoded_read: Reads non-UTF-8 or BOM-prefixed text files, returning line-numbered content along with reports of encoding, BOM, and line endings. Supports an encoding parameter to force a specific encoding.
  • encoded_edit: Performs literal string replacement on a file previously read via encoded_read, writing back in the original encoding, original BOM, and original line endings. Rejects the operation if the file was modified after reading, old_string is not found, or it matches multiple locations. Reports an error if the new content contains characters that cannot be represented in the target encoding (e.g., writing an emoji into a GBK file), rather than writing ?.
  • encoded_write: Creates or fully overwrites a file. Existing files default to the original encoding / BOM / line endings; new files require specifying encoding.

Automatic Behaviors (implemented via tools/pre-execute and tools/post-execute hooks, no model cooperation needed)

  • When read / edit / write / apply_patch fail due to non-UTF-8 content, a hint line is appended to the error, stating the encoding (e.g., GBK, CRLF line endings) and pointing to the encoded_* tools.
  • After edit / write / apply_patch succeed, a stripped UTF-8 BOM is restored; CRLF is preserved when write overwrites a CRLF file. The new version is registered with the official read-before-write policy so that the next edit will not fail due to version mismatch.
  • Mojibake interception covers edit / write / apply_patch / encoded_edit / encoded_write.
  • When bash output contains 3 or more � characters, a one-time per-session hint is shown: “This is likely GBK/Big5 text—use encoded_read.”

Supported encodings: UTF-8 (with BOM), GBK, GB18030, Big5, Shift_JIS, EUC-JP, EUC-KR, UTF-16LE/BE (including detection without BOM).

Installation & Activation

Requires dsh ≥ 0.2.0. Install from GitHub (this plugin is pure JavaScript—no build step required, no allowBuilds authorization needed):

dsh plugin --profile web add github:qinghuidaqzp/dsh-encoding-guard

Or install from npm (once published):

dsh plugin --profile web add dsh-encoding-guard

For other profiles such as headless or tui, simply replace web with the desired profile. Verify it has loaded:

dsh --profile web --dump-config | grep -A3 encoding-guard

Typical Usage

  • Read a GBK file and report encoding and line endings:
  encoded_read(path: "data.txt", encoding: "gbk")

Returns line-numbered content with encoding information.

  • Perform a literal replacement in a GBK file (writes back in the original encoding):
  encoded_edit(path: "data.txt", old_string: "old", new_string: "new")

Will not fail due to BOM or line-ending removal, and will not write an emoji into GBK causing mojibake.

  • Create a new file with a specified encoding:
  encoded_write(path: "new.txt", content: "...", encoding: "utf-8")

Use Cases & Caveats

  • Suitable for dsh projects that need to handle non-UTF-8 files such as GBK, Big5, Shift_JIS, EUC-KR, and UTF-16, especially Windows projects or legacy projects.
  • The plugin runs with the current dsh process’s permissions. Review the source code and license before installing.
  • Encoding detection is heuristic: for very short files or files with little CJK text, it may produce a low-confidence result (output will indicate low confidence). In such cases, use the encoding parameter to specify explicitly.
  • encoded_edit / encoded_write write directly to the local disk (dsh’s file API only supports UTF-8 text writes), so only the local filesystem is supported—remote backends such as SSH are not supported. By default, only files within the session working directory may be written.
  • BOM / CRLF repair only applies to the official dsh-tool-fs edit / write / apply_patch; modifications caused by shell commands (e.g., sed -i, redirects) are out of scope.
  • Mojibake interception only detects signals “newly introduced by the current modification”; pre-existing mojibake in the file will not trigger interception.

Conclusion

dsh-encoding-guard preserves file encoding, BOM, and line endings at the byte level and intercepts mojibake before writing, reducing the risk of encoding corruption when the Agent modifies files. To view the directory page and source code, visit:
- Directory page: https://www.skillhub.cn/plugins/qinghuidaqzp/dsh-encoding-guard
- GitHub: https://github.com/qinghuidaqzp/dsh-encoding-guard

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye