Skip to content

Read plain text in the encodings readers actually have — GBK .txt meets 'Preview unavailable', and the assistant answers 'import a UTF-8 copy' #160

Closed as not planned

Description

@bencode

Problem

  • ui-packages/web/src/features/reader/text-file-reader.tsx:61 decodes plain text with TextDecoder('utf-8', { fatal: true }). A GBK/GB18030-encoded .txt — still the default on many Chinese sites and services, and Shift-JIS likewise in Japan — lands on "Preview unavailable: This text file is not valid UTF-8."
  • The assistant's path fails identically, and its advice (ui-packages/web/src/features/assistant/text-source.ts:19–21) is "If it is text in another encoding, import a UTF-8 copy." Transcoding a file is not an action PRODUCT.md's reader can perform: "ordinary readers… without a technical workspace."
  • The audience is demonstrably CJK — the codebase already tunes for it (formats/docx.ts:116 picks emDelimiter because "CJK text is all letters"; composer/file-mentions.ts:8 recognises an @ after Chinese punctuation). Meanwhile the WHATWG Encoding standard, supported by every target browser, ships gb18030, shift_jis, big5 and BOM-detected UTF-16 decoders — none of them used.

Proposal

  • On fatal UTF-8 failure, retry a small fixed fallback ladder instead of failing: BOM-detected UTF-16/32 first, then gb18030, then shift_jis. UTF-8 stays strict-first, so valid UTF-8 never changes.
  • Put the ladder in decodeUtf8 (ui-packages/web/src/utils/text.ts:5), the one place both paths already meet, so the reader's tab and the assistant's read/search see the same text.
  • Replace the "import a UTF-8 copy" instruction; the file the reader already imported is the one that should become readable.

Goal

After this, a GBK or Shift-JIS plain-text file opens as readable text in its tab, and the assistant reads the same text through read and search — no "Preview unavailable" state and no transcode-the-file instruction for a .txt.

Verification

# GB18030 and Shift-JIS byte fixtures decode to their original sentences,
# and a non-UTF-8 .txt opens through the text reader instead of the error state
cd ui-packages/web && pnpm vitest run --project unit src/utils/text.test.ts src/features/reader/text-file-reader.test.tsx

Refs

  • ui-packages/web/src/features/reader/text-file-reader.tsx:61 — fatal UTF-8 decode behind the error state
  • ui-packages/web/src/features/assistant/text-source.ts:17 — the same refusal on the agent path, with the transcode advice
  • ui-packages/web/src/utils/text.ts:5 — decodeUtf8, the shared seam for the fix
  • ui-packages/web/src/core/files.ts:63 — txt/log route into this path
  • ui-packages/web/PRODUCT.md Users — ordinary readers without a technical workspace
  • CJK-aware precedent: formats/docx.ts:116, composer/file-mentions.ts:8
  • Adjacent in spirit to Teach import paths and assistant capabilities at the empty states, not only in the README #39: the repair belongs where the reader meets the failure, not in the README

Activity

  1. added
    enhancement
    source:scheduled
    on Oct 21, 2026
  2. bencode commented on Oct 21, 2026

    @bencode
    OwnerAuthor

    Independent review — posted by the scheduled issue-reviewer loop (a session separate from the one that files these issues), under the maintainer's standing instruction to evaluate, sharpen, or retire source:scheduled proposals. Audited against main@ebb0ee0.

    Evidence audit: behavior confirmed; one premise needs correcting.

    • ✅ text-file-reader.tsx:61 — new TextDecoder('utf-8', { fatal: true }) in a try whose catch sets { status: 'error', message: 'This text file is not valid UTF-8.' } (:72); the assistant path fails identically — text-source.ts:17 → decodeUtf8 → 'This file is not UTF-8 text. If it is text in another encoding, import a UTF-8 copy.' (:19-21), verbatim.
    • ✅ No gb18030/shift_jis/big5 decoder anywhere in the source; core/files.ts:63 routes .txt/.log here; CJK-aware precedents real (docx.ts:115-117 emDelimiter comment; file-mentions.ts:10-12 Chinese-punctuation comment — cited as :8, trivial drift); PRODUCT.md:10 backs the no-technical-workspace framing.
    • ⚠️ Premise correction: decodeUtf8 (utils/text.ts:5) is not "the one place both paths already meet" today. Its callers are the assistant's text-source.ts:17 and the markdown embed paths (embed-frame.tsx:104, embed-resize.ts:18); the text reader at :61 inlines its own new TextDecoder(...). The fix's first step must route text-file-reader.tsx through decodeUtf8 — the right move (one seam, and the embed paths then get the ladder for free), but as written the Refs claim is ahead of the code.

    Duplicate check: none (encoding/charset has no other issue). Sensor: lint ✓, verify ✗ — right reason: both named test files don't exist yet, so the anchor passes only when the described fixtures and tests are added. No repair needed.

    Verdict: valid, keep open. One sharpening that affects the Goal: the proposed ladder puts gb18030 before shift_jis, but gb18030 accepts nearly every byte sequence — a Shift-JIS file will "successfully" decode into mojibake at the gb18030 rung and never reach shift_jis. Since the Goal promises both GBK and Shift-JIS readability, either order the ladder by a detection heuristic (e.g. decode-and-score, or BOM/locale hint), or scope the Goal to gb18030-family first with Shift-JIS behind an explicit encoding choice. As specified, a Shift-JIS fixture in the Verification block may expose exactly this.

  3. bencode commented on Oct 25, 2026

    @bencode
    OwnerAuthor

    Re-audit — posted by the scheduled issue-reviewer loop, independent of the authoring session. Audited against main@90b046f.

    Evidence audit: anchors hold. No commit since the last review (ebb0ee0 → 90b046f) touches text-file-reader.tsx, text-source.ts, utils/text.ts or core/files.ts. The reader still inlines new TextDecoder('utf-8', { fatal: true }) (text-file-reader.tsx:61) with the rejection message now at :71 (was :72, one-line drift, no behavior change); the assistant's 'import a UTF-8 copy' message is verbatim at text-source.ts:20; still no gb18030/shift_jis/big5 decoder anywhere in the source. The premise correction from the first review (the reader does not yet route through decodeUtf8) is still true.

    Duplicate check: none — #162 covers binary files being mislabeled text-readable, an adjacent but distinct defect; worth cross-referencing if #162's fix ever lands encoding advice here.

    Sensor: lint ✓; verify ✗ for the right reason — both named test files (utils/text.test.ts, text-file-reader.test.tsx) don't exist yet.

    Verdict: valid, stays open. The ladder-order sharpening from the first review (gb18030 accepts nearly any bytes, so a Shift-JIS fixture will decode into mojibake before shift_jis is reached) still stands and should shape the implementation.

  4. bencode commented on Oct 26, 2026

    @bencode
    OwnerAuthor

    Closing as not planned. Gamma Reader reads plain text as UTF-8 only; we are not adding legacy encodings such as GBK or Shift-JIS.

    To read a GBK or Shift-JIS text file, save it as UTF-8 first (for example with Save As → UTF-8 in Notepad, TextEdit or VS Code) and import that copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementsource:scheduled

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions