fixing-claude-export-conversations
Fixes broken line wrapping in Claude Code exported conversation files (.txt), reconstructing tables, paragraphs, paths, and tool calls that were hard-wrapped at fixed column widths. Includes an automated validation suite (generic, file-agnostic checks). Triggers when the user has a Claude Code export file with broken formatting, mentions "fix export", "fix conversation", "exported conversation", "make export readable", references a file matching YYYY-MM-DD-HHMMSS-*.txt, or has a .txt file with broken tables, split paths, or mangled tool output from Claude Code.
1How It Works & Mechanism
Instead of relying on fixed line-wrap width thresholds, the core engine uses a state machine with next-line look-ahead: whenever an apparently hard-wrapped line is encountered, it checks whether the next line resembles a continuation (starting with lowercase, CJK character, opening bracket, or continuing a hyphen/slash/underscore) to decide on joining.
Instead of asking "was this line wrapped?" (fragile threshold), the script asks "does the next line look like a continuation?" by examining its content patterns — lowercase start, CJK ideograph start, opening bracket, hyphen/slash/underscore continuation.
Separately handles 10 categories of content (user prompts, Claude responses, paragraphs, tables, tool calls, tool results, plan text, agent trees, dividers, and tree connectors), with tables receiving dedicated border reconstruction and column-width realignments.
The script handles 10 content types using a state-machine with next-line look-ahead
Applies CJK-aware spacing (pangu spacing) and mid-token detection during joins: inserts a space between ASCII and CJK boundaries, but concatenates directly without spaces when broken in the middle of identifiers, paths, or hyphens.
**Pangu spacing**: Inserts spaces between ASCII alphanumeric characters and CJK ideographs at join boundaries.
Output passes through multiple fidelity safety gates: strictly forbids overwriting the original file, enforces identical input/output counts for marker symbols (❯ ● ✻ ⎿ …), raises alerts for excessively wide lines (>500 display width), and uses strict UTF-8 with no silent fallbacks.
- Never modifies the original file - Marker counts verified: `❯`, `●`, `✻`, `⎿`, `…` must match input/output
Accompanied by a generic (file-agnostic) automated validation suite validate-claude-export-fix.py, serving as a quality gate after each fix.
Runs a comprehensive suite of checks against a fixed file and its original, reporting PASS/FAIL for each with evidence.
Purely local with zero third-party dependencies: launched via uv run, relying solely on the Python standard library.
Python 3.10+ via `uv run` — zero external packages (stdlib only: `unicodedata`, `argparse`, `re`, `pathlib`, `dataclasses`).
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | uv |
| cli | python3 (>=3.10) |
4Recommended Governance & Evidence
- scripts/: Executes local script or command: scripts/fix-claude-export.py
- 本地工作目录: File I/O and artifact output: Outputs *-fixed.txt (defaults to <input-stem>-fixed.txt, configurable via -o; main() strictly enforces that output cannot equal input)
5Second-Pass Verification
- [ok] 无网络外发(脚本层) — 全目录无 urllib/requests/http 调用,grep https?:// 零命中(连文档链接都没有)
- [ok] 无凭证读取 — 无 os.environ/getenv/API_KEY/settings.json 读取;脚本只读用户显式传入的 export 路径
- [ok] 不改原文件声明属实 — main() 先比较 resolve 后的 input/output 路径,相等即报错退出
- [ok] marker 计数保真声明属实 — _count_markers 统计 ❯/●/✻/⎿/…,输出不一致打印 WARNING
- [ok] '10 content types' 与 '53 automated checks' 能力声明 — SKILL.md What Gets Fixed 列出 10 类;validate 脚本为分 category 的断言套件,'53' 为 SKILL 侧声明数
- [ok] 零第三方依赖声明 — 脚本 import 仅标准库;uv 只作为启动器出现于 SKILL.md 命令
6Verdict & Scenarios
d5562a9e134ff1b7…