cjk-edit

A file-encoding guide and safeguard for Chinese, Japanese, and Korean text files. It keeps text in UTF-8 without a BOM and handles older GBK-encoded files when reading or editing.

In plain words
What is it for?
Use it when reading, writing, or editing CJK text files, especially when working with files that may use GBK instead of UTF-8.
Why use it?
It prevents Asian-language text from appearing as garbled characters and reduces encoding mistakes when files are read or changed.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/realsharpswing/opencode-encoding-guard/skill
Any agent
npx skills add realsharpswing/opencode-encoding-guard --skill skill
Clone the repo
git clone --depth 1 https://github.com/realsharpswing/opencode-encoding-guard

Made for: Claude Code, Codex.

Per session 48 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,199 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00048 $0.01199
Opus 5 $0.00024 $0.00600
Sonnet 5 $0.00010 $0.00240
Haiku 4.5 $0.00005 $0.00120

Measured 2d ago against content hash 27dafd6ba3f3, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

cjk-edit scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skill/SKILL.md · 104 lines

How it starts

The opening of the file, as written. The whole thing — 104 lines — stays where its author put it; the contents beside it link to each section on GitHub.

0. 前提

本项目文件编码原则:所有文本文件统一为 UTF-8 无 BOM。这是读、写、编辑三者的基准编码。

encoding-guard.ts 插件(.opencode/plugins/encoding-guard.ts)自动处理编码转换:

  • 读取(A 类):拦截 read 工具输出(tool.execute.after),检查 FF FD 密度判定 GBK,异步重读后替换输出
  • 编辑(B 类):拦截 edit 工具(tool.execute.before),检测 GBK 后在匹配前一次性转为 UTF-8
  • 异步 I/O + 编码缓存(路径+mtime键控):同一文件不重复检测,零事件循环阻塞
  • 全异步非阻塞node:fs/promises,不阻塞 UI 线程,零闪屏

下列旧工具/规则已被插件替代,不再为日常路径。skill 中保留仅为 filesystem_read_text_file 场景和手动降级兜底。


1. 读取

首选 read 工具

readencoding-guard 插件加持可自动识别 GBK。始终优先使用。

filesystem_read_text_file 回退

此工具不受插件覆盖。若因工具限制需使用它读取含 CJK 的文件,且返回乱码:

  1. 手动用 tools/detect-encoding.ps1 鉴定编码(参见附录 A)
  2. 若为 GBK,改用 read 或 bash Get-Content -Encoding GBK 读取

2. 写入

首选平台工具

操作 工具 编码
写入 filesystem_write_file UTF-8 无 BOM(默认)
备份 (.md/.json) filesystem_copy_file → .bak 不改变编码

PowerShell 降级

仅当 filesystem_* 失败时使用:

. "$Env:PROJECT_ROOT/tools/write-safe.ps1"
Write-SafeFile -Path "目标文件" -Content $newContent

显式编码指定

任何 PowerShell 文件操作(Get-Content / Set-Content / Out-File)必须显式指定编码:

场景 正确
读取 -Encoding utf8-Encoding GBK
写入 -Encoding utf8[UTF8Encoding]::new($false)
禁止 不带 -Encoding 参数的 Get-Content/Set-Content

3. 编辑

edit 工具在操作前会自动检测文件编码:若为 GBK 则一次性转为 UTF-8 再执行匹配。 这由 encoding-guard 插件实现。

以下 SOP 仅在 edit 匹配失败时使用(作为兜底链路)。

3.1 首次尝试:短匹配原则

❌ 含中文标点的完整句子
   oldString: - **批判性评估**:... [cite: 2026-01-15]

✅ 纯 ASCII 关键字 + 唯一标识
   oldString: [cite: 2026-01-15]

3.2 失败恢复链路

edit 工具报 "could not find oldString"
   ├─ 步骤1:尝试 replaceAll: true
   ├─ 步骤2:用纯 ASCII 关键字重新定位
   └─ 步骤3:降级至 filesystem_edit_file → bash Get-Content + 替换

4. PS Unicode 陷阱

PowerShell 字符串操作对 CJK 字符存在 culture-sensitive 陷阱。

场景 正确做法 错误做法
CJK 字符串 IndexOf $s.IndexOf("中", [StringComparison]::Ordinal) $s.IndexOf("中")(culture-sensitive)
字面替换 $s.Replace("旧","新")(.NET 方法) $s -replace "旧"(regex,含特殊字符时出错)
文件写编码 [UTF8Encoding]::new($false) [Encoding]::UTF8(产生 BOM)

Read the full file on GitHub · 104 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 104 lines · 48 tokens per session scan A 27dafd6ba3f3

Subscribe to this mod's changes

cjk-edit is a skill published in the GitHub repository realsharpswing/opencode-encoding-guard (5 stars, last pushed 3mo ago), licensed MIT. It adds 48 tokens to every session and 1,199 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.