Per review: CJK and legacy single-byte encodings already passed the old
control-char heuristic (their bytes are all >= 0x80, never NUL), so the
full Encoding.detect pass in isBinaryFile was solving a non-problem for
them. The only realistic regression in the old heuristic is UTF-16 with
BOM, where the second byte of every ASCII character is 0x00 and fires
the NUL-byte early-return.
Replace the detect call with a 2-byte BOM check via a new
Encoding.hasUtf16Bom helper. Also reword the move/add inline markers in
patch/index.ts to note that Encoding.write handles mkdir.
- Encoding.detect: lowercase-compare UTF-32 rejection so jschardet's
uppercase 'UTF-32'/'UTF-32BE'/'UTF-32LE' labels no longer slip through
(iconv-lite happens to have utf-32 codecs that would then be accepted).
- Encoding.read: drop redundant Buffer.from wrap around readFile's
already-Buffer return value.
- EncodedIO: switch to Effect.tryPromise so I/O failures surface as
typed errors that .pipe(Effect.catch(...)) can recover from; the
apply_patch delete branch relies on this to translate read errors
into 'apply_patch verification failed'.
- Shrink kilocode_change blocks in edit.ts, write.ts, apply_patch.ts,
patch/index.ts, and read.ts to per-line inline markers where each
block was only wrapping 1-3 changed lines.
- Collapse the 'if (exists) { let contentOld; let encoding; ... }'
blocks in edit.ts and write.ts into a single ternary + destructure
that keeps all three lines self-contained and each independently
annotated.
- Remove the unused 'latin1' sample and fix the 'providEncoded' typo
in the encoding integration test file.
- Strengthen the apply_patch Shift_JIS test to assert exact bytes
instead of the weak 'does not contain these two UTF-8 bytes' check.
- Drop the stray .kilo/ and .kilocode/ lockfiles that were re-added
by an upstream 'wip' commit.
- Wire Readable.from through a named `stream` variable so the `lines`
helper can destroy it in the finally block, matching the previous
createReadStream pattern.
- Restore the 'Create parent directories', 'Handle file move', 'Regular
update', 'Read original file content', and 'For delete, we need to read
the current content' comments that were dropped when switching to
Encoding.write / Encoding.read. The explicit fs.mkdir calls are no
longer needed because Encoding.write mkdirs recursively, but the intent
comments still apply.
Drop manual BOM parsing, UTF-32, and UTF-16-without-BOM detection: rely on
TextDecoder + jschardet + iconv-lite which already handle BOM round-tripping
for UTF-16 LE/BE. Move the encoding helpers and Effect wrappers into
packages/opencode/src/kilocode/ so shared tool files only carry targeted
kilocode_change markers.
Introduce kilocode Encoding module and integrate into IO paths
implement readEncoded and writeEncoded in filesystem for
encoding-aware file IO
propagate encoding through patching workflows (patch, apply_patch, edit, write)
add tests for encoding utilities
The patch tool now works seamlessly alongside other file editing tools with improved
error handling and a more intuitive permission system. Users will experience:
- More reliable patch application with better error messages
- Consistent permission prompts that match other editing tools
- Smoother integration when applying complex multi-file changes
- Better feedback on what changes are being made before applying patches
This refactoring leverages the robust patch parsing engine while making the tool
feel native to the opencode workflow, reducing friction when making bulk changes
to your codebase.