pi-charset 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/CHANGELOG.zh-CN.md +64 -0
  3. package/CONTRIBUTING.md +33 -0
  4. package/CONTRIBUTING.zh-CN.md +33 -0
  5. package/README.md +35 -6
  6. package/README.zh-CN.md +35 -6
  7. package/docs/ACCEPTANCE.md +30 -2
  8. package/docs/COMPATIBILITY.md +6 -1
  9. package/docs/CONFIG-GUIDE.md +4 -1
  10. package/docs/DESIGN-REVIEW.md +54 -0
  11. package/docs/EDIT-ENGINE.md +46 -0
  12. package/docs/EDITING-PLAN.md +16 -0
  13. package/docs/MCP-AND-MULTI-AGENT.md +111 -0
  14. package/docs/MODEL-EVAL.md +92 -0
  15. package/docs/PROGRESS.md +311 -0
  16. package/docs/RELEASING.md +4 -2
  17. package/docs/zh-CN/ACCEPTANCE.md +30 -2
  18. package/docs/zh-CN/COMPATIBILITY.md +6 -1
  19. package/docs/zh-CN/CONFIG-GUIDE.md +4 -1
  20. package/docs/zh-CN/DESIGN-REVIEW.md +54 -0
  21. package/docs/zh-CN/EDIT-ENGINE.md +46 -0
  22. package/docs/zh-CN/EDITING-PLAN.md +415 -0
  23. package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +111 -0
  24. package/docs/zh-CN/MODEL-EVAL.md +92 -0
  25. package/docs/zh-CN/PROGRESS.md +311 -0
  26. package/docs/zh-CN/RELEASING.md +4 -2
  27. package/docs/zh-CN/editing-plan-architecture.png +0 -0
  28. package/examples/all-utf8.jsonc +1 -0
  29. package/examples/embedded-c.jsonc +1 -0
  30. package/examples/legacy-java-web.jsonc +1 -0
  31. package/examples/mixed-repo.jsonc +1 -0
  32. package/package.json +14 -2
  33. package/prompts/charset-setup.md +3 -3
  34. package/schema/pi-charset.schema.json +118 -0
  35. package/skills/charset-rules/SKILL.md +8 -2
  36. package/src/config.ts +524 -185
  37. package/src/edit/apply-patch-core.ts +275 -0
  38. package/src/edit/apply-patch.ts +78 -0
  39. package/src/edit/diagnostics.ts +48 -0
  40. package/src/edit/hashline.ts +438 -0
  41. package/src/edit/patch.ts +655 -0
  42. package/src/edit/paths.ts +42 -0
  43. package/src/edit/plan.ts +107 -0
  44. package/src/edit/tool.ts +451 -0
  45. package/src/edit-preview.ts +48 -277
  46. package/src/encoding/classify.ts +235 -65
  47. package/src/encoding/common-scripts.ts +66 -0
  48. package/src/encoding/converter.ts +114 -16
  49. package/src/encoding/fast-codec.ts +469 -0
  50. package/src/encoding/file-stamp.ts +55 -0
  51. package/src/encoding/line-endings.ts +66 -73
  52. package/src/encoding/mime.ts +12 -43
  53. package/src/encoding/stream-transcode.ts +135 -30
  54. package/src/grep-core.ts +687 -0
  55. package/src/grep.ts +25 -463
  56. package/src/index.ts +458 -49
  57. package/src/journal.ts +459 -0
  58. package/src/maintenance.ts +375 -0
  59. package/src/mcp/server.ts +651 -0
  60. package/src/operations.ts +759 -127
  61. package/src/read-view.ts +924 -0
  62. package/src/resolve.ts +113 -101
  63. package/src/scaffold.ts +9 -4
  64. package/src/shell-guard.ts +617 -0
  65. package/src/shell.ts +85 -33
  66. package/src/sysnote.ts +4 -6
  67. package/src/version.ts +1 -1
  68. package/tools/charset-core.mjs +251 -0
  69. package/tools/pi-charset-cli.mjs +369 -0
  70. package/tools/pi-charset-mcp.mjs +103 -0
  71. package/tools/scan-encoding.mjs +30 -28
@@ -0,0 +1,311 @@
1
+ # 实施进度与基准记录
2
+
3
+ ## 第一性原理优化、精简与形式化不变量轮次 · 2026-12-05
4
+
5
+ 围绕四个目标——大幅提速降内存、删除冗余重复代码、把历史修过的每个缺陷变成属性测试、找出所有隐式不变量并形式化证明或反例修复:
6
+
7
+ - **零分配 DBCS 往返 DFA 与可编码位图(`src/encoding/fast-codec.ts`)**:直接从 `iconv-lite` 的 `DBCSCodec` 映射表编译状态机与 BMP 位图(遇到多码点组合序列如 Big5-HKSCS 或非 GB18030 的罕见 4 字节项如 GBK `U+E7C7` 时自动回退 `iconv-lite`)。在全部 7 个 CJK 编码上穷举校验全部 231,168 个 1 字节与高引导 2 字节序列,以及全部 655,360 个 BMP 码点(`test/invariants.test.ts`)。同时将模块导入时 12,000 次 `COMMON_HAN` 解码改为惰性 2 次整块解码(`src/encoding/common-scripts.ts`),行尾扫描改用 SIMD `indexOf`,消除 `loadSnapshot`、`prepareEditOutput`、`encodeForWrite` 与 `verifyWritten` 中的重复整文件编解码。
8
+ - **O(改动窗口) 编辑差异引擎(`src/edit/tool.ts`)**:大于 16 KiB 的文件通过 `buildWindowedEditDiff` 做一次零拷贝 `indexOf("\n")` 扫描(O(1) 环形缓冲内存)定位 `plan.ranges` 所在窗口,仅在改动窗口上调用 `generateDiffString` 与 `generateUnifiedPatch`。在 200 组随机单/多点编辑计划上与全文件 diff/patch 逐字节一致(`test/invariants.test.ts`)。
9
+ - **免重读 + 去 ConHost 开销的 Windows `grep`(`src/grep.ts`)**:`context=0` 且搜索趟编码家族与文件一致时直接复用 ripgrep 已解码行文本;补充 `--no-config`;对齐 Pi 原生 `grep` 移除 `windowsHide: true`(Windows 上 `CREATE_NO_WINDOW` 会为每次 `rg` 启动额外增加约 15 ms ConHost 开销);修复 `stop` 信号监听器泄漏。
10
+ - **发现并修复的反例**:
11
+ 1. `incompleteUtf8Tail`(`src/encoding/stream-transcode.ts`)此前接受非法 UTF-8 引导字节 `0xC0`、`0xC1`、`0xF5..0xFF` 及非法次字节(`0xE0 0x80`、`0xED 0xA0`),会把在 `0xC0`(如“来/老/类”)处截断的 GBK 流误判为合法 UTF-8;另外在 `sampleBytes` 边界切开多字节 CJK 字符也会把合法 CJK 流判成 `unknown`。两处均已修复并对照 RFC 3629 穷举证明。
12
+ 2. `inferFromSiblings`(`src/config.ts`)此前拼接兄弟文件前 4 KB 原始字节,超过 4 KB ASCII 文件头会丢失非 ASCII 样本且跨文件拼接可能错位;改为逐文件从首个非 ASCII 字节切片累计评分(`decideFamilyFromBuffers`)。
13
+ 3. 删除 `src/resolve.ts` 中的死函数 `bomBytes`,并收敛 `src/config.ts`、`src/resolve.ts`、`src/operations.ts`、`src/encoding/classify.ts` 与 `src/encoding/mime.ts` 中的重复配置解析、自适应规则构造、拒写模板与二进制头解析代码。
14
+
15
+ | 实测工作负载(Windows 11,Node 24.18.0,Pi 1.4.0) | 改动前 | 改动后 | 提速 / 结果 |
16
+ | --- | ---: | ---: | --- |
17
+ | 10 MiB UTF-8 编辑总耗时 / `diff` 阶段(5 处编辑) | 415 / ~357 ms | **67.6 / 6.7 ms** | **总耗时 6.1× / `diff` 约 53×** |
18
+ | 10 MiB GBK 编辑总耗时 / `encode` / `diff`(ICU 解码 + 原始字节拼接) | 879 / ~100 / 409 ms | **132.8 / 23.6 / 9.9 ms** | **总耗时 6.6× / `diff` 约 41×** |
19
+ | 10 MiB GB18030 编辑总耗时 / `encode`(每行含 4 字节辅助平面字,原始字节拼接) | 808 / ~123 ms | **184.2 / 32.4 ms** | **总耗时 4.4× / `encode` 3.8×** |
20
+ | 1 MiB UTF-8 / GBK / GB18030 / UTF-16LE 编辑总耗时(5 处编辑) | 45–96 ms | **13.1 / 18.5 / 23.1 / 13.9 ms** | **3.5–5.0× 提速**(`encode`:**0.2–1.7 ms**,`diff`:**0.6–0.8 ms**) |
21
+ | 1 MiB GBK `read` / 带校验 `write`(`io-latency`) | 25 / 100 ms | **9–12 / 29–42 ms** | **约 2.5× / 3× 提速** |
22
+ | 零配置读取:408 KB GBK / 16 个 GBK 兄弟中的 ASCII(冷态) | 8.0 / 28.0 ms | **5.7 / 13.7 ms** | **1.4× / 2.0× 提速** |
23
+ | 7 MB GBK 文件深处单命中,上下文 5(冷分类器) | 100 ms | **48.5 ms** | **2.1× 提速** |
24
+ | `grep` UTF-8 树:ASCII 字面量 / 中文 + 上下文 1 | 37.9 / 33.6 ms | **17.0 / 17.0 ms** | **追平原生 Pi grep(16.7 / 16.7 ms)** |
25
+ | `grep` GBK 树:中文字面量 / 中文 + 上下文 1 | 34.2 / 35.1 ms | **19.7 / 19.2 ms** | **比原生快 3.2×(原生 63 / 59.8 ms 且 0 命中)** |
26
+
27
+ - **商业前列四范式编辑与 OMP/Aider/Codex 增强(`src/edit/hashline.ts`、`src/edit/patch.ts`、`src/edit/apply-patch.ts`、`src/edit/tool.ts`、`src/grep.ts`)**:将 Search/Replace、Hashline(`read`/`grep` `<行号>:<2位hex>|` 锚点、`#`/`L` 方言兼容、安全回显前缀吸收、±25 行漂移提示与去重上下文的 `HASH_MISMATCH` 恢复、`NOOP_LOOP` 护栏)、Apply Patch(Codex `*** Begin Patch` / `*** Begin Patch ***`、Aider 单/多文件 `SEARCH/REPLACE` 块与 Git unified diff,含全有或全无预检与有序无死锁多路径加锁)及 Whole File 全部归一到 `PlannedText` IR。已由 `test/edit-modes.test.ts` 与 `test/invariants.test.ts`(39.3 万项 `fastEncodeDbcs`、9.8 万项 ICU `fastDecodeDbcs` 与 `spliceEditedBytes` 穷举等价证明)验证。
28
+ - **整批原子 `/charset-undo`、统一缓存证据、异步热路径与多行 `read(outline=true)`(`src/journal.ts`、`src/encoding/file-stamp.ts`、`src/read-view.ts`、`src/config.ts`、`src/grep.ts`、`src/operations.ts`、`src/shell.ts`、`src/index.ts`)**:多文件 `apply_patch` 的全部变更归入同一 `batchId`,`/charset-undo`(或 `/charset-undo b<批次号>`)在有序多路径锁下先对整批文件做全有或全无预检再原子回滚(先恢复重命名源文件再删除目标文件、拦截外部修改与乱序撤销、32 MiB 上限下整批淘汰过期批次)。四处缓存(`classify`、`config`、`siblingInference`、`grep` `LineSource`)统一采用 `FileStamp`(`dev`/`ino`/`size`/`mtimeMs`/`ctimeMs`)证据,并经由 `invalidatePathCaches` 在 `write`/`edit`/`apply_patch`/`undo` 同步失效;兄弟推断、`rg` 探测、`read`/`/charset-status` 配置读取与 Windows `chcp` 探测均切至异步非阻塞热路径,`grep` 冷命中复用已读字节避免二次读盘。升级 `read(outline=true)` 支持多行签名有界前瞻、多行/嵌套注释与 Python 文档字符串及 Markdown 代码围栏免疫,并扩展多语言覆盖,保持 O(N) 线性耗时与精确 `<行号>:<2位hex>|` 锚点(`test/undo.test.ts`、`test/token-engine.test.ts`、`test/cache-consistency.test.ts`)。
29
+
30
+ **第四次托管运行**(`277203f` 上的 [47495906387](https://github.com/ethan-blue/pi-charset/actions/runs/47495906387),搜索趟并行;`hosted-<os>-bench-round4.json`)。2048 文件目录树上的正则 Ubuntu 39 → 16 ms,托管 Windows 266 → 123 ms;上限为 1 的查询 Ubuntu 10 → 8 ms,Windows 81 → 123 ms——被停掉的两趟现在要付出启动与收尾的代价。标识符搜索 6.7 / 47 ms,60 文件冷字面量搜索 288 / 318 ms。
31
+
32
+ ## 原生工具与 pi-charset 同台对照 · 2026-12-05
33
+
34
+ `test/perf/grep-native-vs-charset.test.ts` 在两棵相同的 200 文件树(一棵 UTF-8,一棵 GBK)上用同样的四个查询运行 Pi 原生 `grep` 与编码感知 `grep`;`test/perf/results/windows-native-vs-charset.json` 保存全部记录。这里测量的改动是编码趟并行:它们是相互独立的 ripgrep 扫描,查询现在等最慢的一趟而不是总和,第一个凑满上限的趟会停掉其他趟。
35
+
36
+ | 查询,热态中位数 ms | 树 | 原生 | 串行搜索趟 | 并行搜索趟 | 原生结果 | 本扩展结果 |
37
+ | --- | --- | ---: | ---: | ---: | --- | --- |
38
+ | ASCII 标识符,字面量,上限 20 | UTF-8 | 17.6 | 38.2 | 37.9 | 20 条 | 20 条 |
39
+ | 中文字面量,上限 20 | UTF-8 | 15.8 | 31.4 | 31.4 | 20 条 | 20 条 |
40
+ | 汉字类正则,上限 20 | UTF-8 | 58.5 | 148.8 | 92.7 | 0 条 | 0 条 |
41
+ | 中文字面量,上下文 1,上限 5 | UTF-8 | 16.7 | 33.6 | 33.6 | 5 块 | 5 块 |
42
+ | ASCII 标识符,字面量,上限 20 | GBK | 26.7 | 53.0 | 48.8 | 20 条,**120 个 U+FFFD** | 20 条,无 U+FFFD |
43
+ | 中文字面量,上限 20 | GBK | 62.0 | 108.9 | 34.2 | **No matches found** | 20 条 |
44
+ | 汉字类正则,上限 20 | GBK | 63.5 | 153.1 | 97.5 | 0 条 | 0 条 |
45
+ | 中文字面量,上下文 1,上限 5 | GBK | 59.2 | 105.9 | 35.1 | **No matches found** | 5 块 |
46
+
47
+ 在 UTF-8 树上两者返回相同的行,扩展每次查询多花 15–35 ms 做分类与取行,低于人能察觉的程度。在 GBK 树上原生工具对中文无从下手,对 ASCII 标识符则把每条中文注释都返回成 U+FFFD;扩展结果正确,且在搜索趟并行之后比原生那次失败的查询还快。UTF-8 树上剩下的差距是汉字类正则:扩展总要跑一趟纯 UTF-8 树永远不需要的 GB18030,200 文件上为 93 ms 对 58 ms。
48
+
49
+ ## 可靠性轮次 · 2026-12-05
50
+
51
+ 由一条标准驱动——写错一次也不可接受——以及 Aider 用 git 而非更多机制让编辑变安全的做法([DESIGN-REVIEW.md](./DESIGN-REVIEW.md)第四节)。
52
+
53
+ **可能静默写错的那条路。** 对五个 CJK 家族的探针显示,决定家族的是候选顺序而不是字节:零配置下 Big5、Shift_JIS 和 EUC-KR 文件全被判成 GBK,显示为花码(`琌代刚祘`、`偙傟偼僥僗僩`、`捞巴篮 抛胶飘`),因为它们的字节同样能按 GBK 往返。所有门禁都沉默:未触及的字节保持原样,只有新文本会以 GBK 编码写进 Big5、日文或韩文文件,造成混合编码。分类器现在对每个能往返的家族从首个非 ASCII 字节起解码 8 KB 样本并评分:中文家族看常用与高频汉字(加载时从 GB2312 一级字与 Big5 常用区段派生,另加手写高频表),日文看假名加常用日文汉字,韩文看偏斜分布的谚文,脚本加分设有最小证据量。差距低于 0.15 即标记 `crossFamilyAmbiguous`:`read` 按最佳猜测显示并警告,`write`/`edit` 以 `AMBIGUOUS_ENCODING` 拒绝非 ASCII 内容;ASCII 替换始终允许;声明的 `encoding` 优先。兄弟推断使用同一判定,Big5 目录的新文件按 Big5 写入。17 个探针案例,包括同一句话的 GBK 与 Big5 两版、两种字体的粤语、三个音节的韩文和含假名的 GBK,全部判对(`test/family.test.ts`)。代价为每次分类 0.2–0.5 ms(五次 8 KB 样本解码),只在冷分类器搜索中可见(60 个命中文件约 +30 ms),读取路径不受影响。
54
+
55
+ **Aider 式撤销。** 每次通过验证的 `write` 和 `edit` 都把被替换的字节记入有界的会话日志。`/charset-undo` 恢复最近一条,或由 `list`、序号、路径指定的那条;扩展新建的文件则删除;文件在写入后有改动或被删除时拒绝,对应 Aider 的脏检查、父提交存在检查与推送检查。恢复后的字节成为会话的已读版本,下一次编辑不会过期。`autoCommit: true` 加上 Aider 的提交步骤:`git add` 该文件,`git commit -m … -- <file>`,其他已暂存的改动保持暂存;提交失败只产生提示,绝不影响写入结果。两者均由 `test/undo.test.ts` 覆盖;命令与其余三个一起注册。
56
+
57
+ ## 审计与优化轮次 · 2026-12-05
58
+
59
+ 在 `d2da356` 之后对源码的一次完整梳理,由五个问题驱动:提升性能、删除无用代码、找出边界条件、重新审视设计、打磨。设计层面的结论见 [DESIGN-REVIEW.md](./DESIGN-REVIEW.md);本节记录改了什么以及测量结果。
60
+
61
+ **时间花在哪里。** 分类器为每个消费者都计算穷尽的七候选列表,却只有 `/charset-init` 读取它;同一批字节还会按推断出的规则再分类一次。为 ASCII 文件和新文件推断编码的兄弟扫描在每次此类读写时都无缓存地执行。`grep` 里纯 ASCII 的命中行会被分类并读取文件,而 ASCII 在所有受支持编码里都相同。
62
+
63
+ | 改动 | 之前 | 之后 |
64
+ | --- | ---: | ---: |
65
+ | 零配置读取 GBK 文件(408 KB),冷缓存 | 36 ms | 8 ms |
66
+ | 在有 16 个 GBK 兄弟的目录里读 ASCII 文件,冷缓存 | 89 ms | 28 ms |
67
+ | 同一文件再读一次,目录缓存命中 | 每次 93 ms | 1.9 ms |
68
+ | 读取并编辑一次 GBK 文件,冷缓存 | 125 ms | 36 ms |
69
+ | 60 个 GBK 文件中的标识符搜索,ASCII 命中行,冷分类器 | 293 ms | 44 ms(ripgrep 自身的扫描) |
70
+
71
+ 机制:`classifyBuffer` 新增 `candidateOrder` 与 WeakMap 判定记忆;`inferAdaptiveRule` 按最窄优先、首个通过即停分类,并记录判定供读写计划复用;`inferFromSiblings` 跨所有传统编码兄弟逐个候选测试、最窄优先,得到与穷尽列表相同的公共编码,并按目录缓存在所选兄弟的大小与 mtime 签名之后。`grep` 对 ripgrep 给出的纯 ASCII 行直接信任,不碰文件。原始样本:`test/perf/results/windows-read-zero-config.json` 与 `windows-grep-hits.json` 的 `round2` 块;设置 `CHARSET_READ_TIMINGS` / `CHARSET_HITS_TIMINGS` 复现。
72
+
73
+ **审计发现的 bug。** 零配置下按规则的 UTF-8 偏好二次分类会把无法判定的字节标成 `config/undecidable`,于是 (a) 读取时 UNKNOWN 提示从不出现,(b) `write` 会用 UTF-8 覆盖这些字节,而闸门 3 承诺的是拒绝。复用第一次判定同时修掉两者;`test/zero-config.test.ts` 现在覆盖提示、编辑拒绝与写入拒绝。另修复:`write` 的硬链接拒绝在替换前报告,不再被通用的写入失败包裹;名为 `..notes` 的文件在 grep 输出里不再丢失相对路径。
74
+
75
+ **行为变更。** 纯 ASCII 行上的命中现在即使文件编码无法判定也会显示,因为该行在任何编码下都相同;此类文件中的非 ASCII 行仍然隐藏,`context` 退化为只输出命中行。二进制文件不受影响,因为 ripgrep 对它们不产生匹配。
76
+
77
+ **已删除。** `verdictForBuffer`(它还缺少缓存的 limited 条目规则)、`planBomBytes`、`detectLineEnding`、`detectLineEndingFromText`、`buildEncodingGroups`、`globDirPrefix`。`sysnote.ts` 改为异步遍历,不再在会话开始时阻塞宿主循环。
78
+
79
+ ## 候选版验收 · 2026-12-03
80
+
81
+ 基线为 `bed85e5`(v0.1.4)。实现从本地提交 `202fc73`(v0.1.5-rc.1)开始,本轮验收修复已提交为 `8f9e98e`。
82
+
83
+ | 任务 | 验收证据 |
84
+ | --- | --- |
85
+ | T01 宿主与契约 | 锁文件固定宿主版本,基线 SHA 已记录。兼容测试断言实际运行的 Pi 版本,并通过真实加载器加载扩展。 |
86
+ | T02–T03 规划与预览 | 共享计划在同一快照中定位唯一、不交叠的区间。预览与执行测试覆盖陈旧计划和参数变化。 |
87
+ | T04–T06 观察、提交与诊断 | 会话基线、文件身份复核及原子替换有冲突与故障注入测试。新增逐字节测试,确认会被判成二进制的编辑结果在写前拒绝,并验证 JSON 转义后的诊断摘录仍满足字节预算。恢复错误码见编辑契约。 |
88
+ | T07 正确性与宿主集成 | Windows 11 / Node 24.18.0 上,Pi 0.84.0、1.4.0、1.7.0 与全局安装的 1.8.0 均通过 `npm run verify`;`ab27a68` 上各宿主 354 项通过、6 项跳过。原生共享锁测试已执行。跳过项为两项 POSIX 测试和四项按需基准。`ab27a68` 的[托管 CI](https://github.com/ethan-blue/pi-charset/actions/runs/47492178717) 七个 job 全部通过:`ubuntu-latest` 在 Node 22、24 与 Pi 0.84.0、1.4.0、1.7.0 下每个 job 354 项通过、6 项跳过,POSIX 权限位与符号链接用例已执行;`windows-latest` 在 Node 22、24 与 Pi 1.4.0、1.7.0 下每个 job 354 项通过、6 项跳过。新增两种 UTF-16 BOM 搜索与裸 CR 行号用例。 |
89
+ | T08 性能与打包 | 以实际打包清单校验相对链接;在干净目录离线安装真实 tarball 后,Pi 1.4.0 RPC 返回全部三个 charset 命令。使用模拟 GitHub CLI 检查了稳定版和预发布在创建与更新时的参数。成对模型任务对照见 [MODEL-EVAL.md](./MODEL-EVAL.md)。 |
90
+
91
+ 成对模型任务对照在一棵天然混有 GBK 与 UTF-8 文件的真实中文竞赛工程上运行了 720 次无头 Pi 会话([MODEL-EVAL.md](./MODEL-EVAL.md)):360 次以 `glm-5.3-flash` 覆盖三个语料变体,另 360 次为思考等级、`glm-5.4` 与带配置 `.pi-charset.json` 的补充。主实验 360 次中,原生臂 180 次通过 87 次,静默破坏 81 个文件;pi-charset 180/180 通过,token 少 65%,在 UTF-8 文件上没有可测开销,也没有连带修改。补充实验在每个设置下都重现了同样的分化。负责人自己的生产语料未经测试,以该竞赛语料代替。候选版没有远程发布标签,负责人已决定不发布。Raw splice、Hashline、patch 输入和多文件事务均在本候选版范围之外。
92
+
93
+ ### 编辑耗时复测
94
+
95
+ 在 Windows 11、Node 24.18.0、Pi 1.4.0 和本机 NTFS 夹具上,使用同一套逐字节校验的基准,按基线、候选、候选、基线顺序比较 v0.1.4 与候选版。36 个场景各有每版本 60 个热态样本,首次观测单列,未清空操作系统缓存。首轮有 35 个场景满足原方案的 p95 增幅不超过 20% 的门槛。10 KiB 的 UTF-16LE 文件执行 20 处编辑时,p95 为 12.15 ms 和 14.61 ms(+20.28%)。针对该场景以每版本 240 个热态样本复测,p95 为 11.00 ms 和 12.19 ms(+10.81%)。保留原校验与两次实测。
96
+
97
+ 仓库文件 `test/perf/results/windows-edit-acceptance.json` 保存四轮逐次样本、专项复测和 12 组候选版分阶段计时。在 1 MiB、5 处编辑场景,差异生成在四种编码中均是中位数最高的阶段。解码阶段包含快照 I/O,提交阶段包含校验,不能单独归因于 CPU 或操作系统 I/O。用[基准命令](EDIT-ENGINE.md)复现时,完整轮次设置 `CHARSET_BENCH_RUNS=32`。专项复测设置 `CHARSET_BENCH_FILTER=^UTF-16LE:10:20$` 和 `CHARSET_BENCH_RUNS=122`。
98
+
99
+ 基线轮次将 `git archive bed85e5` 解包到临时目录,两边使用相同的已安装锁文件依赖。单独把 `test/perf/edit-engine.bench.test.ts` 复制到基线目录,每边的测试都会调用各自的工具注册入口。
100
+
101
+ ### 搜索的逐命中文件访问
102
+
103
+ 此前搜索对每个有命中的文件都整文件解码,并用只有 `/charset-init` 才需要的穷尽候选列表做分类。在 Windows 11、Node 24.18.0 上,对 60 个 236 KB 的 GBK 文件做首次搜索耗时 985 ms,其中 ripgrep 占 51 ms,分类约占 750 ms(每个文件七个候选编码各做一次往返)。改动两处:命中文件改用 `exhaustive: false` 分类,在首个无损候选处停止;行文本改由按文件的读取器提供,超过 64 KB 的文件只解码请求的行范围,同一文件请求满 64 行后切换为一次整文件解码,避免密集上下文输出退化。`runRgPass` 只保留每个 chunk 末尾的不完整行,不再反复切分累积缓冲。未使用的 `buildEncodingGroups` 与 `globDirPrefix` 已删除。
104
+
105
+ | 工作负载(中位数 ms) | 改动前 `05675c0` | 改动后 | 提速 |
106
+ | --- | ---: | ---: | ---: |
107
+ | 60 文件,字面量,context 0,冷分类器 | 985 | 271 | 3.6× |
108
+ | 60 文件,正则,context 3,冷分类器 | 885 | 272 | 3.3× |
109
+ | 60 文件,字面量,context 0,热分类器 | 249 | 104 | 2.4× |
110
+ | 60 文件,正则,context 0,热分类器 | 193 | 122 | 1.6× |
111
+ | 7 MB 文件,深处单命中,context 5 | 146 | 100 | 1.5× |
112
+ | 密集探针:3 文件 × 96 命中,limit 289,context 64 | 48.5 | 49.7 | — |
113
+
114
+ 剩余的冷缓存成本是每个命中文件一次 decode+encode 往返,这是 `read` 与 `edit` 所依赖的精确往返保证的代价。输出逐字节不变:密集、中文路径、裸 CR 与 UTF-16 BOM 回归均未改动。原始样本:`test/perf/results/windows-grep-hits.json`;设置 `CHARSET_HITS_TIMINGS=<json 路径>` 后运行 `npx --no-install vitest run test/perf/grep-hits.test.ts` 复现。
115
+
116
+ ### 10 MiB 压力诊断
117
+
118
+ 计划书将 10 MiB 列为诊断而非目标。在 Windows 11 / Node 24.18.0 / Pi 1.4.0 上设置 `CHARSET_BENCH_SIZES=10240`,每格均字节精确、零失败:UTF-8 p50 415 ms(1 处)与 461 ms(20 处),GBK 879 / 903 ms,GB18030 808 / 849 ms,UTF-16LE 266 / 284 ms。分阶段探针将超线性增长归因于宿主的 `generateDiffString`:它在 pi-charset 截断显示之前对整文件做 Myers diff,10 MiB 时传统编码要 357–409 ms,而 1 MiB 时为 15–34 ms;原生宿主 `edit` 同样要付这笔开销。pi-charset 自身的解码与编码阶段约每 10 倍大小增长 12 倍(`test/perf/results/windows-stress-10mib.json`)。对超大文件,按计划范围对 diff 开窗是剩下的优化途径;要满足 1 MiB 预算并不需要它。
119
+
120
+ ### 写入路径一致性
121
+
122
+ `atomicReplace` 现在拒绝硬链接数大于 1 的目标文件,与 `edit` 路径通过 `assertEditableFile` 已有的行为一致。临时文件 rename 会让写入路径指向新 inode,其他路径名则悄悄留在旧内容上。`write` 与 `edit` 均带原因拒绝并保持所有路径名不变;`test/regressions.test.ts` 在两个平台上覆盖 `write`。
123
+
124
+ ### 托管计时运行
125
+
126
+ `.github/workflows/bench.yml` 按需(`workflow_dispatch`,带 `os`、`filter`、`runs` 输入)在 `ubuntu-latest` 与 `windows-latest` 上运行全部计时探针。`6c6f1a0` 上的运行 47376712153 使用 4 vCPU runner、Node 24.22.3、ripgrep 15.1.0 / 16.0.0。36 场景编辑基准以基线/候选/候选/基线顺序对照 `bed85e5`,每格 32 次(每侧 60 个热样本)。runner 计时是对本机 Windows 数字的交叉验证,不是延迟保证。
127
+
128
+ | 1 MiB p50 ms,改动前 → 后 | Ubuntu | 托管 Windows |
129
+ | --- | ---: | ---: |
130
+ | UTF-8,1 / 5 / 20 处 | 53→42、80→42、176→45 | 78→64、100→65、193→69 |
131
+ | GBK,1 / 5 / 20 处 | 132→68、153→70、260→77 | 166→92、176→93、276→97 |
132
+ | GB18030,1 / 5 / 20 处 | 145→71、162→72、271→75 | 174→95、182→96、276→98 |
133
+ | UTF-16LE,1 / 5 / 20 处 | 39→22、52→23、108→27 | 61→43、73→47、128→47 |
134
+
135
+ 两个 runner:两侧零失败,36 格中 34 格在 +20% p95 预算内,峰值 RSS 持平或更低(每侧第二轮:Ubuntu 568→540 MiB,Windows 499→473 MiB)。超线的两格都是中位数反而更好的 10 KiB 操作。针对性复测(运行 47379722842,每格 122 次、每侧 240 个热样本)把 Windows 三格全部拉回预算内(+8.0%、−38.7%、−25.9%)。Ubuntu 上中位数持平或更好(UTF-8 1.98 对 2.04 ms,GBK 2.40 对 2.99,GB18030 2.23 对 3.14),但两条 p95 尾部在 2–3 ms 的操作上仍高 1.4 ms 和 2.9 ms;分阶段探针显示共享磁盘会给两侧都带来 40–50 ms 的提交阶段尖峰,而候选版每次编辑为快照和守卫多发出几次 `lstat`/`fstat`。两组结果均保留:`test/perf/results/hosted-<os>-bench.json` 与 `hosted-<os>-retest.json`。
136
+
137
+ 同一次运行的搜索探针。Ubuntu:60 个 GBK 文件的首次搜索 207–210 ms,热态 44–54 ms(Linux 上冷分类成本同样是 4–5 倍),7 MB 文件深处单命中 141 ms,密集上下文 5–25 ms,错趟正则首次 35 ms / 热态 17 ms,2048 文件目录树字面量 14 / 12 ms、正则 35 / 36 ms,仓库样本 grep 17.5 / 5.7 ms、read 3.6 ms、edit 6.1 ms。托管 Windows:冷态 262–300 ms 对热态 99–140 ms,深处单命中 170 ms,密集 45–74 ms,目录树字面量 74 / 73 ms、正则 225 / 218 ms。Linux 正是计划书所说的快速回归环境;Windows 数字与本机 Altior 数据在 runner 噪声内一致。
138
+
139
+ **第二次托管运行**(`aa5c30b` 上的 [47458909915](https://github.com/ethan-blue/pi-charset/actions/runs/47458909915),审计轮次之后;原始样本在 `test/perf/results/hosted-<os>-bench-round2.json`)。编辑基准在两个 runner 上均为 36 格中 0 格超出 +20% p95 预算,零失败:快照里的判定复用去掉了多余的候选往返,之前的 10 KiB 尾部随之消失。GBK 1 MiB 20 处编辑:Ubuntu 308 → 91 ms,托管 Windows 344 → 115 ms。Ubuntu 读取探针:传统编码文件冷缓存 12.9 ms,16 个 GBK 兄弟中的 ASCII 文件冷缓存 28.9 ms、目录缓存命中 0.75 ms,读取加一次编辑 52 ms;托管 Windows 为 14.8 / 38.3 / 1.8 / 60 ms。60 个 GBK 文件中的标识符搜索,冷分类器:Ubuntu 11 ms、Windows 58 ms,这就是 ripgrep 的扫描时间。2048 文件目录树上限为 1 的正则查询只启动一个 rg 子进程:Ubuntu 10 ms、Windows 81 ms,而第一次运行里三个子进程约 380–400 ms。
140
+
141
+ **第三次托管运行**(`ab27a68` 上的 [47492422939](https://github.com/ethan-blue/pi-charset/actions/runs/47492422939),可靠性轮次之后;`hosted-<os>-bench-round3.json`)。家族判定给每个命中文件增加五次 8 KB 样本解码,编辑路径不受影响:GBK 1 MiB 20 处编辑 Ubuntu 244 → 74 ms,托管 Windows 271 → 97 ms;Ubuntu 36 格 0 超预算,Windows 1 格(`GB18030:100:1` +25%,该格在第一轮的针对性复测中为 −26%)。读取探针冷缓存 11 ms,目录缓存命中 0.3 / 1.4 ms;标识符搜索 Ubuntu 7 ms、Windows 46 ms;60 文件冷字面量搜索 246 / 318 ms,第二轮为 258 / 358 ms,多出的解码在 runner 噪声之内。
142
+
143
+ ## 高密度 grep 交接 · 2026-12-02
144
+
145
+ 注册 grep 回归生成三个中文名 C 文件,分别采用 UTF-8、GBK、GB18030,各有连续 96 个有效命中,使用 CRLF,末行无换行。六组(limit,context)为 (1,1)、(95,0)、(96,1)、(97,2)、(289,0)、(289,64)。独立生成预期上下文块,逐项核对完整输出、路径、行号、顺序、相邻命中的交叠上下文和截断信息。已有逐字节分块回归继续运行。
146
+
147
+ context=64 时,288 个命中展开为 867,795 字节,宿主保留其中 51,171 字节。格式化器现在最多拼接 51,200 字节预算内的内容和首条超限记录;后续行继续计数,确保截断元数据准确。本次验证了拼接文本的上界,进程峰值内存尚未实测。
148
+
149
+ `test/perf/results/windows-dense-search.json` 包含 Pi 1.4.0 两轮基线 a1/a2 和两轮修改后 b1/b2,每轮六遍。第零遍为新测试进程首遍,操作系统缓存未清空。高上下文首次观察值前后分别为 52.51/51.79 和 53.93/52.34 ms。各轮其余五遍的热态中位数,入口总耗时由 47.28/47.98 变为 52.29/51.27 ms,子进程生命周期由 33.71/34.83 变为 34.70/34.83 ms,stdout 回调由 9.80/9.58 变为 10.27/10.08 ms,关闭后处理由 8.88/9.74 变为 10.88/11.13 ms。回调与子进程耗时有重叠。修改后 context=0 时总耗时 45.13/43.25 ms,关闭后处理 3.91/4.06 ms。大上下文热态每次读取三个夹具文件共五次;读耗时含 I/O,探测与解码尚未单独计时。
150
+
151
+ 在仓库根目录执行即可重新生成夹具和测量数据:
152
+
153
+ ```powershell
154
+ $env:CHARSET_DENSE_TIMINGS = Join-Path $env:TEMP 'dense-search.json'
155
+ npx --no-install vitest run test/host-integration.test.ts -t "checks dense grep"
156
+ Remove-Item Env:CHARSET_DENSE_TIMINGS
157
+ ```
158
+
159
+ Windows 上 Pi 0.84.0、1.4.0、1.7.0 分别通过 verify,各 318 项通过、6 项跳过。最终拼接字符串规模断言也已在工作副本通过。后续仅在代表性负载出现延迟问题时,分析首条超限记录之后的字节计数开销,同时保留精确元数据。生产样本与付费模型对照仍待授权。
160
+
161
+ ## 本地交接 · 2026-12-02
162
+
163
+ 从[历史原方案](EDITING-PLAN.md)进入,再按下文查阅实测结果。最终工作树的 0.1.5-rc.1 源码已在 Windows 分别通过 Pi 0.84.0、1.4.0、1.7.0 全部门槛。每个宿主均通过类型检查、316 项测试(跳过 6 项)以及覆盖 25 份 Markdown 的打包检查。测试会核对实际加载的宿主版本。下文的性能数据来自 Pi 1.4.0。
164
+
165
+ 新增中文路径跨块回归:通过注册的 grep 运行真实 rg,将其 JSON 输出逐字节交给现有流式解码器,覆盖文件名与命中正文中“控”的两个内部分界。断言完整输出为 `控制.c:2: 控制 NEEDLE`。复现命令为 `npx --no-install vitest run test/host-integration.test.ts -t "keeps Chinese grep"`。本轮只补测试,在 Windows Pi 1.4.0 上通过 317 项、跳过 6 项;上列三宿主数字为新增用例前的验收结果。
166
+
167
+ 1. 先执行 `git status --short --branch` 和 `git diff --check`。复制工作区候选版时,包含新增源码和测试。
168
+ 2. 正常验收运行 `npm run verify`。在已有依赖缓存的 Altior 上,从仓库根目录运行下方 PowerShell,即可在新建的本地临时副本内离线复验三个宿主。
169
+ 3. 性能复现按下文各场景命令执行,夹具由测试现场生成。八轮逐次毫秒数据保存在 `test/perf/results/windows-search.json`。lookup 是 256 文件场景,tree 是 2,048 文件的进程收尾对比(a 为修复前、b 为修复后),content 是相同 rg 命令的配对回放。每个数组首项为首次观测。
170
+ 4. 普通字面量搜索、文件名查找、有效命中上限、进程收尾和低命中多编码正则均已收口。仅在可复现的正确性失败,或有代表性的高命中、大上下文语料测出扩展侧显著开销时重开。真实生产样本与付费模型对照仍待验证。
171
+
172
+ ```powershell
173
+ $root = (Get-Location).Path
174
+ $copy = New-Item -ItemType Directory -Path (Join-Path $env:TEMP ("pi-charset-" + [guid]::NewGuid()))
175
+ git ls-files --cached --others --exclude-standard -- . ':(exclude).alma-snapshots/**' | ForEach-Object {
176
+ if (Test-Path -LiteralPath $_ -PathType Leaf) {
177
+ $target = Join-Path $copy.FullName $_
178
+ [IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($target)) | Out-Null
179
+ [IO.File]::Copy((Join-Path $root $_), $target)
180
+ }
181
+ }
182
+ Push-Location $copy.FullName
183
+ $priorHost = $env:PI_HOST_VERSION
184
+ try {
185
+ npm ci --offline --no-audit --no-fund
186
+ if ($LASTEXITCODE) { throw "npm ci failed" }
187
+ foreach ($v in '1.4.0','0.84.0','1.7.0') {
188
+ if ($v -ne '1.4.0') {
189
+ npm install --offline --no-audit --no-fund --no-save --package-lock=false "@earendil-works/pi-ai@$v" "@earendil-works/pi-tui@$v" "@earendil-works/pi-coding-agent@$v"
190
+ if ($LASTEXITCODE) { throw "install failed for $v" }
191
+ }
192
+ $env:PI_HOST_VERSION = $v
193
+ npm run verify
194
+ if ($LASTEXITCODE) { throw "verify failed for $v" }
195
+ }
196
+ } finally {
197
+ $env:PI_HOST_VERSION = $priorHost
198
+ Pop-Location
199
+ }
200
+ ```
201
+
202
+ ## 编辑引擎
203
+
204
+ [历史方案](EDITING-PLAN.md)记录了设计时的判断和验收条件。候选版 0.1.5-rc.1 已在 Windows 上完成编辑验收。搜索改动前,Pi 0.84.0、1.4.0、1.7.0 各通过 312 项、跳过 4 项。原生共享锁与合成 GBK/CRLF 用例通过,本机 Pi 1.7.0 离线加载工作副本成功。按基线、候选、候选、基线顺序实测,每版本每场景合并 60 个热样本,36 组编辑均满足 p95 增幅不超过 20% 的门槛。最高增幅对应 12.27 至 12.97 ms。真实生产样本与付费模型对照仍待验证。
205
+
206
+ 本轮搜索改动后,工作副本通过类型检查、313 项测试(4 项跳过)及发布检查。发布检查覆盖 25 份 Markdown 文档和打包产物。
207
+
208
+ ## 搜索
209
+
210
+ Pi 通过 `createEncodingGrepDefinition` 注册内容搜索工具 `grep`。扩展定位 ripgrep,按配置运行各编码轮次,结合文件分类核实命中,再输出上下文及截断提示。找不到 ripgrep 或搜索路径时委托 Pi 内置 grep。按文件名查找沿用宿主工具。
211
+
212
+ 本轮优化针对错误编码轮次出现的正则命中。原逻辑最终会拒绝这类命中,但此前会读取、解码并切分整个文件。将已有的正则判断提前后,同一查询中首次遇到此类文件即可省去这三步。字面量的原文复核、文件分类与后续编码轮次均保留。
213
+
214
+ | Windows 测量 | 首次查询(ms) | 热态 p50(ms) | 热态 p95(ms) | 热态范围(ms) |
215
+ | --- | ---: | ---: | ---: | ---: |
216
+ | 优化前 | 425.9 | 364.5 | 378.5 | 351.7–378.5 |
217
+ | 优化后 | 116.5 | 81.1 | 96.8 | 76.4–96.8 |
218
+ | 优化后复测 | 120.0 | 82.1 | 99.6 | 77.9–99.6 |
219
+
220
+ 同一台 Windows 电脑、Node 24.18.0。每个进程生成 32 个 UTF-8 文件,每个含 90,000 行 CRLF 填充和一个标记,另生成一个含真实匹配行的 GBK 文件。正则刻意使 UTF-8 文件在错误编码轮次产生候选。首次查询清空扩展缓存,操作系统磁盘缓存未控制。每轮随后执行 15 次热查询;按最近秩算法,15 个样本的 p95 为最大值。计时覆盖注册工具的执行入口、rg 启动和输出格式化。普通单轮字面量查询和文件名查找尚无本轮计时数据。
221
+
222
+ 复现时将 `CHARSET_GREP_TIMINGS` 设为新的 JSON 输出路径,运行 `npx --no-install vitest run test/perf/grep-mismatched-pass.test.ts`。数组首项为首次查询,其余为热查询,单位为毫秒。未设置变量时以小语料回归,断言输出上下文、行号、匹配上限和 GBK 原始字节。
223
+
224
+ <details>
225
+ <summary>逐次查询耗时(ms)</summary>
226
+
227
+ ```json
228
+ {"before":[425.88,378.46,376.62,358.51,355.17,370.2,363.7,359.76,356.72,369.15,364.53,378.37,351.67,353.49,371.49,365.76],"after":[116.5,79.98,78.58,76.8,76.35,79.04,79.75,81.77,78.98,81.6,96.76,81.06,85.32,88.16,91.26,89.57],"after2":[119.98,80.68,99.58,78.74,80.4,82.14,77.87,80.14,80.26,82.44,82.08,84.92,87.99,99.02,87.07,81.64]}
229
+ ```
230
+
231
+ </details>
232
+
233
+ ## 搜索上限与常用查找
234
+
235
+ GBK 字符“丂”的第二个字节是十六进制 40,与 ASCII 的 @ 相同。夹具前两行含“丂”,第 3、4 行才有真正的 @。旧版注册的 grep 在 limit=1 时返回零项。修复后逐条验证并去重,凑足有效结果即停止 rg,解码行缓存最多保留一个文件。回归覆盖上限 1、2、CRLF 上下文、源文件字节,以及 GBK、UTF-8 和 ASCII 混合搜索的排序和去重。Windows Pi 1.4.0 常规验证通过 314 项,跳过 5 项;另行启用的计时测试也通过。
236
+
237
+ 同机生成 256 个 CRLF 文件,GBK 和 UTF-8 各半。每种工具各有一个目标。两轮测量均交替调用扩展 grep 和 Pi 内置 find,每个入口各做首次查询一次、热查询 20 次;每次都核对路径,grep 还核对第 2 行正文。未控制系统文件缓存;首次调用可能包含工具发现。热态分位数按最近秩计算。
238
+
239
+ | 入口 | 两轮首次(ms) | 两轮热态 p50(ms) | 两轮热态 p95(ms) |
240
+ | --- | --- | --- | --- |
241
+ | 扩展 grep,字面量 NEEDLE | 88.4 / 93.9 | 86.6 / 88.0 | 97.1 / 108.8 |
242
+ | Pi find,文件名 needle-*.c | 189.4 / 28.1 | 24.6 / 24.0 | 28.6 / 27.3 |
243
+
244
+ ```powershell
245
+ $env:CHARSET_LOOKUP_TIMINGS = Join-Path $env:TEMP "pi-charset-lookup.json"
246
+ npx --no-install vitest run test/host-integration.test.ts -t "measures literal"
247
+ Remove-Item Env:CHARSET_LOOKUP_TIMINGS
248
+ npx --no-install vitest run test/host-integration.test.ts -t "counts only verified"
249
+ ```
250
+
251
+ ## 大目录树与子进程收尾
252
+
253
+ Altior 实机(Windows、Node 24.18.0、Pi 1.4.0)生成 2,048 个 CRLF 文件,GBK 与 UTF-8 各半,分布在三级目录的 32 个模块中。另有一个被忽略的文件,同时匹配内容和文件名。每次查询都只返回可见目标,grep 还核对第 2 行。四轮各启动独立测试进程,各入口每轮一次首次查询、十次热查询,未控制操作系统缓存。
254
+
255
+ 修复前两次 limit=1 回归都在 rg 发出 close 事件前返回(1 个子进程,0 个已关闭);修复后两次均为 1 个已关闭。达到有效命中上限后,扩展停止处理后续输出,并等待 close 事件再返回。此前 GBK 上限回归通过。Windows 常规验证通过 315 项、跳过 6 项,可选计时另行通过。
256
+
257
+ | 热态中位数(ms) | 修复前两轮 | 修复后两轮 |
258
+ | --- | --- | --- |
259
+ | 扩展字面量 grep | 152.60 / 154.96 | 151.02 / 153.10 |
260
+ | 宿主文件名 find | 26.74 / 28.62 | 27.45 / 27.94 |
261
+
262
+ 修复后,grep 首次为 164.86 / 153.07 ms,find 首次为 31.64 / 29.09 ms。两轮热样本最大值分别为 171.01 / 177.66 ms 和 30.20 / 29.99 ms。整树计时使用默认上限,只有一个有效命中,测的是普通查询。上限收尾由单独的密集命中用例检验。
263
+
264
+ 修复后各项独立探针的两轮热中位数(ms):清缓存后发现 rg 为 0.23 / 0.20,rg --version 为 8.57 / 8.22,rg --files 为 18.74 / 19.77,读取、解码并切分小型 GBK 目标文件为 0.97 / 0.89,宿主 find 入口至 fd 的 spawn 事件为 4.93 / 5.21,fd --version 为 12.30 / 12.09。版本探针包含启动和退出,列举探针包含遍历和路径输出。这些范围有重叠,不能加减得出完整内容搜索各阶段耗时。普通查询没有显示稳定加速。
265
+
266
+ ```powershell
267
+ $env:CHARSET_TREE_TIMINGS = Join-Path $env:TEMP "pi-charset-tree.json"
268
+ npx --no-install vitest run test/perf/search-tree.test.ts
269
+ Remove-Item Env:CHARSET_TREE_TIMINGS
270
+ ```
271
+
272
+ ## 内容搜索配对计时
273
+
274
+ 上述大目录基准现会捕获注册 grep 每次启动子进程的实际参数,在同一棵目录树上立即使用同一可执行文件、同一组参数回放内容查找。两次独立 Windows 进程各为每种查询运行 11 对,去掉首对,下表是两轮热态中位数(ms)。直接回放均完整扫描。探针仅在测试中启用。
275
+
276
+ | 查询 | 注册 grep | 各轮子进程从 spawn 事件到 close | 各轮直接回放 | 同步输出回调 |
277
+ | --- | --- | --- | --- | --- |
278
+ | ASCII 字面量 | 153.85 / 155.17 | 148.61 / 149.73 | 139.71 / 140.72 | 2.51 / 2.69 |
279
+ | 正则 | 466.91 / 467.47 | 151+144+150 / 151+149+155 | 144+139+139 / 139+141+144 | 0.79 / 0.83 |
280
+ | 正则,上限 1 | 407.16 / 397.53 | 147+146+99 / 149+149+98 | 141+142+136 / 141+141+139 | 2.05 / 2.39 |
281
+
282
+ 字面量只运行 UTF-8。配置下的正则依次运行 UTF-8、BIG5、GB18030,各产生一个原始候选;只有最后一轮与目标 GBK 文件的分类一致。上限为 1 时,最后一轮通过校验后即停止。被忽略的目标始终未计入结果。(2026-12-05 更新:有了 ASCII 行快速路径后,目标行在第一趟即被信任。搜索趟串行时这使上限为 1 的查询只剩一个 rg 子进程、同机 71 ms;搜索趟改为并行后三个子进程都会启动,第一个凑满上限的趟会停掉另外两趟,同一查询约 117 ms。同一目录树上默认上限的正则查询从 463 ms 降到 271 ms。)输出回调包含流式解析及候选校验,与子进程时段重叠。单独读取、解码并切分小文件为 0.93 / 0.91 ms;分类缓存命中未从回调中进一步拆分。系统磁盘缓存未控制,每对均先跑扩展再直接回放。
283
+
284
+ 这棵树的大部分耗时来自 rg 内容扫描,当前数据不足以支持继续修改匹配器或解码缓存。若后续具有代表性的合成语料出现大量真实命中、需要上下文,且回调耗时成为主项,再重启优化。上节 PowerShell 命令可复现。Windows 常规验证通过 316 项、跳过 6 项,覆盖无效正则、调用方截止时间已到、有效结果上限和子进程关闭。
285
+
286
+ ## 上下文格式化内存复核
287
+
288
+ Altior Windows,Node 24.18.0,Pi 1.4.0。脚本从固定基线或工作副本提取格式化代码,输入已解码合成行,调用已安装宿主的截断实现。每种规模按 ABBA ABBA 顺序启动八个独立进程,测量前各执行一次 GC。两种实现返回文本及元数据的摘要完全一致。仓库根目录复现命令如下。
289
+
290
+ ```powershell
291
+ node test/perf/grep-format-memory.mjs
292
+ node --expose-gc test/perf/grep-format-memory.mjs bounded heavy profile
293
+ python -X utf8 test/perf/rg-memory-win.py
294
+ ```
295
+
296
+ 下面的中位数依次为原实现和有界实现。三个文件各 96 个命中、context=64,格式化耗时 5.8 / 7.2 ms,拼接 867,795 / 51,207 字节,两组进程的 RSS 高水位增量中位数均为零,启动阶段已达到更高水位。各 384 个命中、context=128 时,耗时 71.0 / 66.7 ms,拼接 9,089,699 / 51,230 字节,进程 RSS 高水位增量为 67.01 / 30.97 MiB。拼接时 JS 已用堆相对起点的增量分别为 8.40 / 7.18 和 50.70 / 2.40 MiB。堆、external、arrayBuffers 均为时点采样,受 GC 影响,无法可靠归因外部缓冲区变化。另行运行的 rg 各编码进程观测到的峰值工作集为 7.55–8.04 MiB。
297
+
298
+ 短 CPU 剖析中匿名帧占 129 个采样点,appendLine 占 13 个,可能含 JIT 内联。该数据不足以将上轮入口延迟差额归因于精确计数,保留现有实现。后续在代表性负载的多次剖析能定位具体开销时再优化。Windows verify 318 项通过,6 项跳过。`test/perf/results/windows-format-memory.json`、`test/perf/results/windows-format-profile.json`、`test/perf/results/windows-rg-memory.json`。
299
+
300
+
301
+ ## 仓库自身样本验收
302
+
303
+ 注册工具处理当前工作副本 src/version.ts、src/grep.ts 和 docs/CONFIG-GUIDE.md 的逐字节副本。独立逐行生成普通与单行上下文 grep 的完整预期输出,核对行号、顺序、重复命中及 .ignore 排除的同样命中文件;Markdown 标题命中第一行。Pi 原生 find 恰好返回两个预期 TS 路径,按成员核对,未要求宿主遍历顺序。read 与归一化文本一致。隔离副本的一处注释编辑得到预期字节,重复旧编辑被拒且字节未变,反向编辑恢复原字节。三个来源文件都通过前后逐字节比对。
304
+
305
+ Altior Windows、Pi 1.4.0 单独运行一次,普通 grep 52.73 ms、上下文 grep 34.62 ms、原生 find 23.88 ms、Markdown grep 27.14 ms、read 6.66 ms、edit 16.20 ms。顺序执行的单次耗时仅供复现参考。完整 verify 通过 319 项、跳过 6 项,包含合成多编码 C 回归。Git 未列出已跟踪的 C 文件;生产 C 和模型对照仍未验证。
306
+
307
+ ```powershell
308
+ $env:CHARSET_SELF_TIMINGS='1'
309
+ npx --no-install vitest run test/host-integration.test.ts -t 'isolated repository samples'
310
+ Remove-Item Env:CHARSET_SELF_TIMINGS
311
+ ```
@@ -22,7 +22,7 @@
22
22
 
23
23
  此外,`npm run check:release` 还会自动校验:
24
24
  - `package.json` 的 Pi 扩展清单规范(`keywords` 含 `pi-package`、`pi.extensions` 入口文件存在);
25
- - 全仓所有 Markdown 文档中的相对路径链接是否全部有效;
25
+ - 工作区和 npm 包内 Markdown 文档的相对链接是否有效;
26
26
  - `npm pack --dry-run` 打包产物白名单是否干净(不含测试样本、临时文件或快照目录)。
27
27
 
28
28
  ## 标准发版步骤
@@ -58,4 +58,6 @@ git push origin main --follow-tags
58
58
  推送 `v*.*.*` 标签后,GitHub Actions 会自动触发 `.github/workflows/release.yml`:
59
59
  1. **跨平台验证(`verify`)**:在 Ubuntu 与 Windows 上执行 `npm ci`、`node tools/check-release.mjs --tag vX.Y.Z`、`npm run typecheck` 与 `npm test`。
60
60
  2. **创建 GitHub Release(`github-release`)**:从 `CHANGELOG.md` 自动提取对应版本的发布说明,执行 `npm pack` 生成 `.tgz` 归档包,并创建附带安装包产物的 GitHub Release。
61
- 3. **可选 npm 官方源发布(`npm-publish`)**:当仓库变量 `ENABLE_NPM_PUBLISH` 设为 `true` 时,通过 GitHub OIDC Trusted Publishing 自动执行 `npm publish --provenance --access public`。
61
+ 3. **可选 npm 官方源发布(`npm-publish`)**:当 `ENABLE_NPM_PUBLISH` 为 `true` 时,通过 GitHub OIDC Trusted Publishing 发布并附带来源证明。
62
+
63
+ 含 SemVer 预发布后缀的标签(例如 `v0.1.5-rc.1`)在 GitHub 标记为预发布,在 npm 使用 `next` 分发标签。稳定版本使用 `latest`。
@@ -1,5 +1,6 @@
1
1
  // Pure UTF-8 repository configuration: preserves UTF-16 BOM, blocks binary writes, and verifies writes on read-back.
2
2
  {
3
+ "$schema": "https://raw.githubusercontent.com/ethan-blue/pi-charset/main/schema/pi-charset.schema.json",
3
4
  "sourceEncoding": "UTF-8",
4
5
  "writeEncoding": "UTF-8"
5
6
  }
@@ -1,6 +1,7 @@
1
1
  // Embedded C project configuration: .c and .h sources use GBK + CRLF; other files use UTF-8.
2
2
  // Copy to the project root as .pi-charset.json.
3
3
  {
4
+ "$schema": "https://raw.githubusercontent.com/ethan-blue/pi-charset/main/schema/pi-charset.schema.json",
4
5
  "sourceEncoding": "UTF-8",
5
6
  "writeEncoding": "UTF-8",
6
7
  "unmappable": "error",
@@ -1,6 +1,7 @@
1
1
  // Legacy Java Web project configuration (Eclipse + Tomcat + SVN, GBK primary encoding).
2
2
  // Copy to the project root as .pi-charset.json and adjust overrides to match your directory layout.
3
3
  {
4
+ "$schema": "https://raw.githubusercontent.com/ethan-blue/pi-charset/main/schema/pi-charset.schema.json",
4
5
  "sourceEncoding": "GBK",
5
6
  "writeEncoding": "GBK",
6
7
  "unmappable": "error",
@@ -1,6 +1,7 @@
1
1
  // Multi-module mixed-encoding repository: UTF-8 by default, with a dedicated GBK legacy subdirectory.
2
2
  // If GBK and UTF-8 files coexist in the same directory, omit directory overrides and rely on per-file byte classification.
3
3
  {
4
+ "$schema": "https://raw.githubusercontent.com/ethan-blue/pi-charset/main/schema/pi-charset.schema.json",
4
5
  "sourceEncoding": "UTF-8",
5
6
  "overrides": [{ "pattern": "legacy-gbk/**", "encoding": "GBK" }]
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-charset",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Encoding-aware read, write, edit, and grep extension for Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -33,18 +33,29 @@
33
33
  "engines": {
34
34
  "node": ">=22.19.0"
35
35
  },
36
+ "bin": {
37
+ "pi-charset": "./tools/pi-charset-cli.mjs",
38
+ "pi-charset-mcp": "./tools/pi-charset-mcp.mjs"
39
+ },
36
40
  "files": [
37
41
  "src",
38
42
  "skills",
39
43
  "prompts",
44
+ "schema/pi-charset.schema.json",
40
45
  "README.md",
41
46
  "README.zh-CN.md",
42
47
  "CHANGELOG.md",
43
48
  "CHANGELOG.zh-CN.md",
49
+ "CONTRIBUTING.md",
50
+ "CONTRIBUTING.zh-CN.md",
44
51
  "LICENSE",
45
52
  "docs/**/*.md",
53
+ "docs/zh-CN/editing-plan-architecture.png",
46
54
  "examples",
47
- "tools/scan-encoding.mjs"
55
+ "tools/charset-core.mjs",
56
+ "tools/scan-encoding.mjs",
57
+ "tools/pi-charset-cli.mjs",
58
+ "tools/pi-charset-mcp.mjs"
48
59
  ],
49
60
  "publishConfig": {
50
61
  "access": "public"
@@ -57,6 +68,7 @@
57
68
  "verify": "npm run typecheck && npm run test && npm run check:release",
58
69
  "fixtures": "node test/fixtures/gen.ts",
59
70
  "scan": "node tools/scan-encoding.mjs",
71
+ "check:encoding": "node tools/pi-charset-cli.mjs check",
60
72
  "version": "node tools/sync-version.mjs && git add src/version.ts README.md README.zh-CN.md",
61
73
  "prepublishOnly": "npm run verify"
62
74
  },
@@ -4,6 +4,6 @@ argument-hint: "[target-dir]"
4
4
  ---
5
5
  Inspect the file-encoding and line-ending profile of `${1:-.}` before making any configuration changes:
6
6
 
7
- 1. Run the bundled read-only scanner (`tools/scan-encoding.mjs`) against `${1:-.}` and check for `UNKNOWN` files or pre-existing `U+FFFD` / mojibake damage (`--damaged`).
8
- 2. Run `--init --dry-run` against `${1:-.}` to preview the recommended `.pi-charset.json` rules without writing to disk.
9
- 3. Summarize the detected encodings, line-ending styles (`CRLF` vs `LF`), any damaged or `UNKNOWN` files, and the proposed `.pi-charset.json` configuration, then wait for confirmation before writing `.pi-charset.json`.
7
+ 1. Check existing project declarations (`.editorconfig`, `.gitattributes`, `pom.xml`, `CMakeLists.txt`) and run `/charset-scan` + `/charset-scan --damaged` (or `npx pi-charset scan ${1:-.} --damaged`) to detect `UNKNOWN` files or pre-existing `U+FFFD` / mojibake damage.
8
+ 2. Preview the recommended `.pi-charset.json` without writing to disk via `/charset-init --dry-run` (or preset templates: `embedded-c`, `legacy-java-web`, `mixed-repo`, `all-utf8`).
9
+ 3. Summarize the detected encodings, line-ending styles (`CRLF` vs `LF` vs `MIXED`), any damaged or `UNKNOWN` files, and the proposed `.pi-charset.json` (plus optional `npx pi-charset check --staged` CI gate), then wait for confirmation before writing `.pi-charset.json`.
@@ -0,0 +1,118 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://raw.githubusercontent.com/ethan-blue/pi-charset/main/schema/pi-charset.schema.json",
4
+ "title": "pi-charset Configuration (.pi-charset.json)",
5
+ "description": "Encoding, line-ending, write-gate, and shell-guard configuration for pi-charset.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["sourceEncoding"],
9
+ "properties": {
10
+ "$schema": {
11
+ "type": "string",
12
+ "description": "Optional JSON Schema reference for editor validation and autocomplete."
13
+ },
14
+ "sourceEncoding": {
15
+ "type": "string",
16
+ "description": "Default source encoding for this directory tree (e.g. GBK, GB18030, UTF-8, Big5, Shift_JIS, EUC-KR, EUC-JP, UTF-16LE)."
17
+ },
18
+ "writeEncoding": {
19
+ "type": "string",
20
+ "description": "Target encoding for writes/new files. Defaults to sourceEncoding; set differently only for explicit encoding migration."
21
+ },
22
+ "readStrategy": {
23
+ "type": "string",
24
+ "enum": ["auto", "config"],
25
+ "default": "auto",
26
+ "description": "auto classifies bytes deterministically; config forces read display using sourceEncoding while keeping byte-safe writes."
27
+ },
28
+ "unmappable": {
29
+ "type": "string",
30
+ "enum": ["error", "escape", "drop-to-gb18030"],
31
+ "default": "error",
32
+ "description": "Gate 1 policy when text contains characters unrepresentable in the target encoding."
33
+ },
34
+ "verifyWrite": {
35
+ "type": "boolean",
36
+ "default": true,
37
+ "description": "Gate 2 post-write read-back verification against expected bytes and encoding classification."
38
+ },
39
+ "protectUtf8": {
40
+ "type": "boolean",
41
+ "default": true,
42
+ "description": "Keep valid UTF-8 files as UTF-8 even inside a legacy sourceEncoding workspace."
43
+ },
44
+ "preferSourceOverUtf8": {
45
+ "type": "boolean",
46
+ "default": false,
47
+ "description": "On BOM-less files whose bytes satisfy both UTF-8 and the declared multi-byte CJK sourceEncoding (e.g. tiny GBK headers), classify as sourceEncoding instead of UTF-8."
48
+ },
49
+ "autoCandidates": {
50
+ "type": "array",
51
+ "items": { "type": "string" },
52
+ "default": ["GB18030", "GBK", "GB2312", "Big5", "Shift_JIS", "EUC-KR", "EUC-JP"],
53
+ "description": "Ordered multi-byte candidate encodings tested during deterministic byte round-trip classification."
54
+ },
55
+ "transcodeBash": {
56
+ "oneOf": [
57
+ { "type": "boolean" },
58
+ { "type": "string" }
59
+ ],
60
+ "default": false,
61
+ "description": "Stream-transcode bash/powershell output to UTF-8: false, true/\"auto\", or an explicit encoding name."
62
+ },
63
+ "bashFileDump": {
64
+ "type": "boolean",
65
+ "default": true,
66
+ "description": "When transcodeBash is enabled, prefer the target file's classified encoding for single-file dump commands (cat/type/Get-Content)."
67
+ },
68
+ "autoCommit": {
69
+ "type": "boolean",
70
+ "default": false,
71
+ "description": "Opt-in Aider-style path-limited git commit after each verified write/edit/apply_patch."
72
+ },
73
+ "hashlines": {
74
+ "type": "boolean",
75
+ "default": false,
76
+ "description": "Annotate text lines returned by read/grep with <line>:<2-hex>| Hashline anchors by default."
77
+ },
78
+ "shellGuard": {
79
+ "type": "string",
80
+ "enum": ["block", "warn", "off"],
81
+ "default": "block",
82
+ "description": "Intercept shell commands that write/transcode source files outside pi-charset gates (python -c, iconv, sed -i, Set-Content, redirects)."
83
+ },
84
+ "locale": {
85
+ "type": "string",
86
+ "enum": ["auto", "en", "zh-CN"],
87
+ "default": "auto",
88
+ "description": "Diagnostic and command message language (auto, en, or zh-CN)."
89
+ },
90
+ "newFileLineEnding": {
91
+ "type": "string",
92
+ "enum": ["CRLF", "LF"],
93
+ "description": "Optional line-ending style applied to newly created files."
94
+ },
95
+ "overrides": {
96
+ "type": "array",
97
+ "description": "Path-glob overrides relative to this .pi-charset.json directory (most specific pattern wins).",
98
+ "items": {
99
+ "type": "object",
100
+ "additionalProperties": false,
101
+ "required": ["pattern"],
102
+ "properties": {
103
+ "pattern": { "type": "string", "description": "Glob pattern relative to config directory (e.g. docs/**, *.properties)." },
104
+ "encoding": { "type": "string", "description": "Override read/default encoding for matching paths." },
105
+ "writeEncoding": { "type": "string", "description": "Override write encoding for matching paths." },
106
+ "force": { "type": "boolean", "default": false, "description": "Bypass byte round-trip detection (required for single-byte encodings like ISO-8859-1 / windows-1252)." },
107
+ "readStrategy": { "type": "string", "enum": ["auto", "config"] },
108
+ "unmappable": { "type": "string", "enum": ["error", "escape", "drop-to-gb18030"] },
109
+ "autoCandidates": { "type": "array", "items": { "type": "string" } },
110
+ "protectUtf8": { "type": "boolean" },
111
+ "preferSourceOverUtf8": { "type": "boolean" },
112
+ "verifyWrite": { "type": "boolean" },
113
+ "newFileLineEnding": { "type": "string", "enum": ["CRLF", "LF"] }
114
+ }
115
+ }
116
+ }
117
+ }
118
+ }
@@ -10,7 +10,7 @@ Follow these rules whenever working in a repository with non-UTF-8 files (`GBK`,
10
10
 
11
11
  ## 1. File Tool Boundaries
12
12
 
13
- - **Always read, edit, and write source files via Pi's `read`, `edit`, and `write` tools**, and search via `grep`.
13
+ - **Always read, edit, and write source files via Pi's `read`, `edit`, `apply_patch`, and `write` tools**, and search via `grep`. On large files, prefer `read(outline=true)` / `read(ranges=[[a,b],...])` and `grep(filesOnly=true)` / `grep(hashlines=true)` paired with `edit(hashlines=...)` or `apply_patch` (`*** Hashline File:`) to minimize tokens and round-trips.
14
14
  - **Never write or rewrite source files through shell commands** (`echo >`, `cat <<EOF >`, `sed -i`, `perl -pi`, `Set-Content`, `Out-File`, or one-off Python/Node scripts). Shell writes bypass `pi-charset`'s byte classifier, line-ending preservation, and write safety gates, silently converting `GBK` files to `UTF-8` or corrupting non-ASCII bytes.
15
15
  - **Never run `iconv` or manually convert file encodings** unless the user explicitly asks to migrate a file's encoding.
16
16
 
@@ -36,5 +36,11 @@ Follow these rules whenever working in a repository with non-UTF-8 files (`GBK`,
36
36
  - **Gate 3 (`UNKNOWN`, `BINARY`, or Lossy Re-Encoding)**:
37
37
  - Never set `"force": true` merely to silence a Gate 3 error.
38
38
  - Inspect the file via `node ../../tools/scan-encoding.mjs <path> --damaged` and confirm the intended encoding with the user.
39
+ - **`AMBIGUOUS_ENCODING` (several encoding families round-trip the bytes)**:
40
+ - Keep replacements ASCII when possible; otherwise ask the user which encoding the file really is and add an `encoding` override for that path. Never guess a family on the user's behalf.
41
+ - **Undo**: every verified `write`/`edit` is journaled for the session. If a change turns out wrong, `/charset-undo` restores the previous bytes (it refuses if the file changed since).
39
42
  - **Mixed Line Endings (`CRLF` + `LF`)**:
40
- - `read` succeeds, while `edit` and `write` refuse to normalize the whole file silently. Ask the user before normalizing line endings across the file.
43
+ - `read` succeeds, while `edit` and `write` refuse with `[MIXED_EOL; not_applied]` to avoid silently normalizing the whole file. Ask the user to run `/charset-normalize-eol <path> --to crlf|lf` (never `dos2unix` via shell) before retrying.
44
+ - **Controlled Transcoding & Rule Inspection**:
45
+ - Use `/charset-explain <path>` to trace why a file resolves to a given encoding/override/EOL.
46
+ - Use `/charset-convert <path> --to <encoding> [--bom|--no-bom]` when an explicit encoding migration is requested (Gate 1/2 verified and `/charset-undo` journaled; never disable `shellGuard` to run `iconv`).