diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a6f50be..bc161212 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,7 @@ name: CI on: + workflow_dispatch: push: branches: [main] pull_request: @@ -33,7 +34,7 @@ jobs: run: bash scripts/preflight.sh --base "${{ github.event.pull_request.base.sha }}" --skip-release-state - name: Run main preflight - if: github.event_name == 'push' + if: github.event_name != 'pull_request' run: bash scripts/preflight.sh --skip-payload-source --skip-release-state windows-install-smoke: @@ -76,6 +77,10 @@ jobs: - name: Verify Linux native baseline run: node scripts/verify-upstream-compat.js --skip-latest --native-linux-x64 --fail-on-skip --json --verify-install + - name: Verify upgrade through the installed launcher + shell: bash + run: bash scripts/verify-native-upgrade.sh + windows-native-compat: name: windows-native-compat (${{ matrix.shard }}) runs-on: windows-2022 @@ -95,6 +100,11 @@ jobs: - name: Install native verification dependency run: npm install --global node-lief@1.3.2 + - name: Verify upgrade through the installed launcher + if: matrix.shard == 0 + shell: bash + run: bash scripts/verify-native-upgrade.sh + - name: Verify Windows native support window shell: powershell env: @@ -122,3 +132,7 @@ jobs: VERSIONS="$(node -p 'require("./scripts/upstream-compat.config.json").support.macosNativeExperimental.representatives.filter(v => Number(v.split(".")[2]) >= 242).join(",")')" test -n "$VERSIONS" node scripts/verify-upstream-compat.js --baseline "$VERSIONS" --skip-latest --native-macos-arm64 --fail-on-skip --json --verify-install + + - name: Verify upgrade through the installed launcher + shell: bash + run: bash scripts/verify-native-upgrade.sh diff --git a/.github/workflows/native-latest-candidate.yml b/.github/workflows/native-latest-candidate.yml index f2359a78..8f805bdd 100644 --- a/.github/workflows/native-latest-candidate.yml +++ b/.github/workflows/native-latest-candidate.yml @@ -458,3 +458,66 @@ jobs: name: linux-native-latest-candidate path: ${{ steps.verify.outputs.json_path }} if-no-files-found: error + + propose-update: + name: Open verified native support PR + needs: [verify, verify-windows, verify-linux] + if: ${{ github.ref == 'refs/heads/main' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') }} + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + actions: write + concurrency: + group: native-support-pr + cancel-in-progress: false + steps: + - uses: actions/checkout@v6 + with: + ref: main + - uses: actions/setup-node@v6 + with: + node-version: 24 + - uses: actions/download-artifact@v6 + with: + pattern: '*native-latest-candidate*' + path: /tmp/native-candidates + merge-multiple: true + - name: Promote the three verified reports + env: + GH_TOKEN: ${{ github.token }} + shell: bash + run: | + set -euo pipefail + shopt -s nullglob + mac=(/tmp/native-candidates/native-latest-candidate-*.json) + win=(/tmp/native-candidates/windows-native-latest-candidate-*.json) + linux=(/tmp/native-candidates/linux-native-candidate-*.json) + test "${#mac[@]}" = 1 && test "${#win[@]}" = 1 && test "${#linux[@]}" = 1 + node scripts/promote-native-candidate.js --candidate "${mac[0]}" --platform macos --write + node scripts/promote-native-candidate.js --candidate "${win[0]}" --platform windows --write + node scripts/promote-native-candidate.js --candidate "${linux[0]}" --platform linux --write + if git diff --quiet; then exit 0; fi + VERSION="$(node -e 'const s=require("./scripts/upstream-compat.config.json").support;const v=[s.macosNativeExperimental.ceiling,s.windowsNativeExperimental.ceiling,s.linuxNativeExperimental.ceiling];if(new Set(v).size!==1)process.exit(1);process.stdout.write(v[0]);')" + BRANCH="codex/native-latest-${VERSION}" + if [ -n "$(gh pr list --head "$BRANCH" --state open --json number --jq '.[].number')" ]; then exit 0; fi + node scripts/generate-plugin-support-window.js --write + node scripts/generate-support-matrix.js + node scripts/sync-readme-support-window.js --write + node scripts/prepare-native-release-closeout.js --native-version "$VERSION" + node scripts/sync-doc-derived-counts.js --write + git switch -c "$BRANCH" + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git add scripts/upstream-compat.config.json plugin/support-window.json docs/support-matrix.md README.md plugin/manifest.json plugin/.claude-plugin/plugin.json CHANGELOG.md AGENTS.md CLAUDE.md + git commit -m "chore: verify Claude Code ${VERSION} on three platforms" + bash scripts/preflight.sh --base origin/main --skip-release-state + git push origin "$BRANCH" + cat > /tmp/native-pr.md <
**Windows native .exe 用户先装 node-lief**:如果当前 Claude Code 是 2.1.113+ native `.exe`,请先运行 `npm install -g node-lief` 再装插件。未安装时 Layer 4 CLI Patch 会跳过,Layer 1~3 不受影响。也可以继续通过 [WSL](https://learn.microsoft.com/zh-cn/windows/wsl/install) 使用 `install.sh`。 -Claude Code 在 Windows 更新后,插件不会现场改写正在运行并被系统锁定的 `claude.exe`。先照常使用;方便时关闭所有 Claude Code 窗口,再回到本项目目录重跑上面的 `install.ps1`,安装器会完成补丁、启动自检和失败回滚。 +Claude Code 在 Windows 更新后,插件不会现场改写正在运行并被系统锁定的 `claude.exe`。关闭占用窗口后,再通过已安装的 `claude` 启动器启动,会在启动前完成补丁和自检,无需重装插件。 ### 各安装方式的中文化程度 @@ -226,13 +226,13 @@ Claude Code 在 Windows 更新后,插件不会现场改写正在运行并被 | 安装方式 | 中文化程度 | |---------|-----------| | `npm install -g @anthropic-ai/claude-code@2.1.112` | 最完整(推荐) | -| `npm install -g @anthropic-ai/claude-code`(latest) | macOS / Windows native 新版先本机自检;Linux native 仅启用已发布窗口 | +| `npm install -g @anthropic-ai/claude-code`(latest) | macOS / Windows native 新版先本机自检;Linux x64 glibc 新版先本机自检 | | `curl -fsSL https://claude.ai/install.sh \| bash -s 2.1.112` | 官方安装器指定已验证旧版本(需要 `node-lief`) | -| `curl -fsSL https://claude.ai/install.sh \| sh`(latest) | macOS 新版先本机自检;Linux x64 glibc 仅启用支持矩阵列出的实际版本 | +| `curl -fsSL https://claude.ai/install.sh \| sh`(latest) | macOS 新版先本机自检;Linux x64 glibc 新版先本机自检 | | `curl -fsSL https://claude.ai/install.sh \| bash -s 2.1.265` | Linux x64 glibc 已验证版本(需要 `node-lief >=1.3.0`);不含 arm64、musl 或未验证版本 | | `powershell -File install.ps1` | Windows:旧 npm cli.js 最完整;native .exe `2.1.113 - 2.1.265` 内已验证版本需 `node-lief`;Claude 更新后关闭所有窗口并重跑 | -> **native binary 说明**:官方安装器和新版 npm 包安装的是原生程序。插件会按容器格式翻译:源码构建提取并写回 JS,字节码构建在原字符串占位内写入中文;译文超过占位长度时保留英文。两条路径均在启动自检通过后记录成功,macOS 还会重新签名。已验证版本见[支持矩阵](./docs/support-matrix.md),不代表完整中文覆盖。Linux x64 glibc 仅启用矩阵列出的版本,需要 `node-lief >=1.3.0`。Windows 更新 Claude Code 后,请关闭所有 Claude Code 窗口,再重跑 `install.ps1`。 +> **native binary 说明**:官方安装器和新版 npm 包安装的是原生程序。插件会按容器格式翻译:源码构建提取并写回 JS,字节码构建在原字符串占位内写入中文;译文超过占位长度时保留英文。两条路径均在启动自检通过后记录成功,macOS 还会重新签名。已验证版本见[支持矩阵](./docs/support-matrix.md),不代表完整中文覆盖。Linux x64 glibc 未收录版本须通过本机验证,需要 `node-lief >=1.3.0`。Windows 更新 Claude Code 后,请关闭所有 Claude Code 窗口,再通过已安装的 `claude` 启动器启动。 安装脚本会自动检测安装方式,无需手动选择。 @@ -268,7 +268,7 @@ powershell -NoProfile -ExecutionPolicy Bypass -File .\doctor.ps1 # Windows ### 更新 -Claude Code 更新后,npm / macOS native 安装会在首次会话启动时**自动检测版本变更并重新 patch**;Windows native 会保留原版可用,并提示关闭窗口后重跑安装器。新版先本机自检;只有格式、依赖、提取或自检失败才跳过原生 Layer 4,不会让 CLI 失效。 +通过安装器安装启动入口后,Claude Code 更新或同版本重装会在下次运行 `claude` 时**先检测文件变化,再验证并补汉化**;Windows 文件被其他窗口占用时,需要关闭占用窗口后再次启动。新版先本机自检;只有格式、依赖、提取或自检失败才跳过原生 Layer 4,不会让 CLI 失效。 插件本体发布新 Release 后,正式安装态由 Claude Code 插件管理器更新。独立兜底安装只做限时检查并提示,不会在会话启动途中原地覆盖自身;本地源码安装用户在会话结束后运行 `git pull && ./install.sh`(Windows:`git pull` 后重跑 `install.ps1`)。 @@ -280,15 +280,15 @@ Claude Code 更新后,npm / macOS native 安装会在首次会话启动时** | macOS / Linux / WSL · npm 全局安装 | `2.1.92 - 2.1.112` | 翻译最完整;launcher 启动前自修复 + `session-start` 兜底 | | macOS · 官方安装器(native) | `2.1.110 - 2.1.112` | 需要 `node-lief` | | macOS · native binary(arm64) | `2.1.113 - 2.1.265` 内的已验证版本 | 需要 `node-lief`;个别版本未收录,见支持矩阵 | -| Linux · native binary(x64 glibc) | `2.1.220 - 2.1.265` | 需要 `node-lief >=1.3.0`;仅支持矩阵列出的版本,不含 arm64、musl | +| Linux · native binary(x64 glibc) | `2.1.220 - 2.1.265` | 需要 `node-lief >=1.3.0`;未收录版本先本机验证,不含 arm64、musl | | Windows · npm(PowerShell) | `2.1.92 - 2.1.112` | 用 install.ps1,需 PowerShell 5.1+ | | Windows · native .exe(x64) | `2.1.113 - 2.1.265` 内的已验证版本 | 需要 `node-lief`;个别版本未收录,见支持矩阵 | | Linux · 其他官方安装器形态 | 暂无已验证版本 | 仅 Layer 1~3 生效 | > - **macOS / Windows 版本号不是运行门禁**:高于已知 native 下限、且仍能被识别的新版会先在本机临时副本上翻译并执行启动自检;通过后才替换。已有词条继续中文,新文案原样保留英文。 -> - **Linux 不走 provisional**:Linux x64 glibc 仅启用支持矩阵列出的已验证版本;arm64、musl 和未验证的新版本不执行 CLI Patch。 +> - **Linux 本机验证**:Linux x64 glibc 的未收录版本先按程序结构和启动自检验证;不支持 arm64、musl,不代表未来版本全量兼容。 > - **失败不伤 CLI**:补丁、重打包或启动自检任一步失败,都会保留或恢复原文件;失败只影响中文覆盖,不影响 Claude Code 使用。 -> - **Windows 不热改运行中的 exe**:Claude Code 更新后先保持原版可用;关闭所有 Claude Code 窗口,再按 Windows 安装命令重跑 `install.ps1`,由安装器补丁并自检。 +> - **Windows 不热改运行中的 exe**:Claude Code 更新后先保持原版可用;关闭占用窗口后通过已安装的 `claude` 启动器再次启动,自动补丁并自检,无需重装。 > - **格式变化才停手**:如果未来版本不再是可识别的 native 格式、依赖缺失、提取失败或启动自检失败,只跳过 Layer 4,Layer 1~3 继续生效。 > - **矩阵只记录证据**:纯上游兼容证据可以更新支持矩阵,不要求插件升版;只有插件代码、翻译或 manifest 变化才发布新版。 > - **已验证版本完整清单**(含个别未收录版本)见 [docs/support-matrix.md](./docs/support-matrix.md),由脚本自动生成。 diff --git a/compute-patch-revision.sh b/compute-patch-revision.sh index 4c93c463..41da28c9 100644 --- a/compute-patch-revision.sh +++ b/compute-patch-revision.sh @@ -17,6 +17,7 @@ const files = [ "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", + "scripts/native-repair.js", ]; const hash = crypto.createHash("sha256"); diff --git a/docs/support-matrix.md b/docs/support-matrix.md index aa0588bf..35d15188 100644 --- a/docs/support-matrix.md +++ b/docs/support-matrix.md @@ -18,8 +18,8 @@ - 验证等级只表示公开证据和翻译覆盖程度,不是运行门禁。 - `stable`:代表版本段已通过 compat matrix,且 npm 路径具备启动前自修复。 -- `experimental`:已有 native 运行和显示面证据;更高可识别版本也会先本机自检,已知文案继续中文,未知文案保留英文(仅适用于 macOS / Windows)。 -- Linux 仅启用已发布的 x64 glibc 版本,不尝试 provisional latest;arm64 与 musl 不在支持窗口内。 +- `experimental`:已有 native 运行和显示面证据;更高可识别版本也会先本机自检,已知文案继续中文,未知文案保留英文(平台范围见下表)。 +- Linux x64 glibc 未收录版本也可启动前本机验证(provisional);不支持 arm64、musl,不代表未来版本全量兼容。 - `unsupported`:该平台或二进制格式不执行原生 Layer 4;正式插件的 Layer 1~3 继续可用。 ## Current Support @@ -28,11 +28,11 @@ | --- | --- | --- | --- | --- | | npm global install | stable | 2.1.92 - 2.1.112 | 2.1.92 PASS · 2.1.97 PASS · 2.1.104 PASS · 2.1.107 PASS · 2.1.110 PASS · 2.1.112 PASS | PATH 优先 launcher + session-start 二层兜底,适用于旧 cli.js npm 包形态;2.1.113+ native binary wrapper 暂不支持旧 CLI Patch。 | | macOS official installer | experimental | 2.1.110 - 2.1.112 | 2.1.110 PASS(native 1245) · 2.1.111 PASS(native 1241) · 2.1.112 PASS(native 1241) | 官方安装器指定旧版本仍走 native binary;macOS arm64 已离线验证 extract/patch/repack/--version,插件可用 native patch experimental 处理,需要 node-lief;稳定使用仍建议 npm pinned。 | -| macOS native binary | experimental | 2.1.113 - 2.1.265 (不含未纳入本轮支持的 2.1.115, 2.1.125, 2.1.127, 2.1.130, 2.1.134, 2.1.135, 2.1.147, 2.1.149, 2.1.151, 2.1.154, 2.1.155, 2.1.157, 2.1.163, 2.1.164, 2.1.166, 2.1.171, 2.1.172, 2.1.174, 2.1.176, 2.1.180, 2.1.181, 2.1.182, 2.1.184, 2.1.186, 2.1.187, 2.1.188, 2.1.189, 2.1.191, 2.1.192, 2.1.193, 2.1.194, 2.1.195, 2.1.196, 2.1.197, 2.1.198, 2.1.199, 2.1.200, 2.1.201, 2.1.202, 2.1.203, 2.1.204, 2.1.206, 2.1.207, 2.1.208, 2.1.209, 2.1.210, 2.1.213, 2.1.225, 2.1.227, 2.1.228, 2.1.229, 2.1.230, 2.1.231, 2.1.232, 2.1.233, 2.1.234, 2.1.235, 2.1.236, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.113 PASS(native 1358, display 11/11) · 2.1.114 PASS(native 1358, display 11/11) · 2.1.116 PASS(native 1351, display 11/11) · 2.1.117 PASS(native 1334, display 11/11) · 2.1.118 PASS(native 1323, display 11/11) · 2.1.119 PASS(native 1328, display 11/11) · 2.1.120 PASS(native 1331, display 11/11) · 2.1.121 PASS(native 1334, display 11/11) · 2.1.122 PASS(native 1334, display 11/11) · 2.1.123 PASS(native 1334, display 11/11) · 2.1.124 PASS(native 1331, display 11/11) · 2.1.126 PASS(native 1331, display 11/11) · 2.1.128 PASS(native 1331, display 11/11) · 2.1.129 PASS(native 1333, display 11/11) · 2.1.131 PASS(native 1333, display 11/11) · 2.1.132 PASS(native 1323, display 11/11) · 2.1.133 PASS(native 1323, display 11/11) · 2.1.136 PASS(native 1322, display 11/11) · 2.1.137 PASS(native 1322, display 11/11) · 2.1.138 PASS(native 1322, display 11/11) · 2.1.139 PASS(native 1324, display 11/11) · 2.1.140 PASS(native 1324, display 11/11) · 2.1.141 PASS(native 1324, display 11/11) · 2.1.142 PASS(native 1320, display 11/11) · 2.1.143 PASS(native 1326, display 11/11) · 2.1.144 PASS(native 1324, display 11/11) · 2.1.145 PASS(native 1324, display 11/11) · 2.1.146 PASS(native 1335, display 11/11) · 2.1.148 PASS(native 1333, display 11/11) · 2.1.150 PASS(native 1333, display 11/11) · 2.1.152 PASS(native 1343, display 11/11) · 2.1.153 PASS(native 1343, display 11/11) · 2.1.156 PASS(native 1385, display 11/11) · 2.1.158 PASS(native 1385, display 11/11) · 2.1.159 PASS(native 1392, display 11/11) · 2.1.160 PASS(native 1392, display 11/11) · 2.1.161 PASS(native 1387, display 11/11) · 2.1.162 PASS(native 1385, display 11/11) · 2.1.165 PASS(native 1370, display 11/11) · 2.1.167 PASS(native 1405, display 11/11) · 2.1.168 PASS(native 1405, display 11/11) · 2.1.169 PASS(native 1392, display 11/11) · 2.1.170 PASS(native 1389, display 11/11) · 2.1.173 PASS(native 1381, display 11/11) · 2.1.175 PASS(native 1381, display 11/11) · 2.1.177 PASS(native 1381, display 11/11) · 2.1.178 PASS(native 1473, display 11/11) · 2.1.179 PASS(native 1463, display 11/11) · 2.1.183 PASS(native 1462, display 11/11) · 2.1.185 PASS(native 1462, display 11/11) · 2.1.190 PASS(native 1452, display 11/11) · 2.1.205 PASS(native 1395, display 11/11) · 2.1.211 PASS(native 1390, display runtime 11/11, coverage PARTIAL 4) · 2.1.212 PASS(native 1386, display runtime 11/11, coverage PARTIAL 8) · 2.1.214 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.215 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.216 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.217 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.218 PASS(native 1383, display runtime 11/11, coverage PARTIAL 13) · 2.1.219 PASS(native 1382, display runtime 11/11, coverage PARTIAL 13) · 2.1.220 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.221 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.222 PASS(native 1491, display runtime 11/11, coverage PARTIAL 15) · 2.1.223 PASS(native 1490, display runtime 11/11, coverage PARTIAL 15) · 2.1.224 PASS(native 1490, display runtime 11/11, coverage PARTIAL 20) · 2.1.226 PASS(native 1490, display runtime 11/11, coverage PARTIAL 20) · 2.1.237 PASS(native 1720, display runtime 11/11, coverage PARTIAL 29) · 2.1.238 PASS(native 1717, display runtime 11/11, coverage PARTIAL 39) · 2.1.239 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.240 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.241 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.242 PASS(native 1562, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1139, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1139, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1151, display runtime 11/11, coverage PARTIAL 232) | macOS arm64 native binary experimental;需要 node-lief;已验证 2.1.113 - 2.1.114、2.1.116 - 2.1.124、2.1.126、2.1.128 - 2.1.129、2.1.131 - 2.1.133、2.1.136 - 2.1.146、2.1.148、2.1.150、2.1.152 - 2.1.153、2.1.156、2.1.158 - 2.1.162、2.1.165、2.1.167 - 2.1.170、2.1.173、2.1.175、2.1.177 - 2.1.179、2.1.183、2.1.185、2.1.190、2.1.205、2.1.211 - 2.1.212、2.1.214 - 2.1.224、2.1.226、2.1.237 - 2.1.242、2.1.252、2.1.260、2.1.263、2.1.265 的原生汉化、--version 启动自检和 11 个稳定显示面的运行审计;2.1.265 的运行边界通过,但展示文案覆盖为 PARTIAL(232 个警告),不代表完整中文覆盖;2.1.115、2.1.125、2.1.127、2.1.130、2.1.134、2.1.135、2.1.147、2.1.149、2.1.151、2.1.154、2.1.155、2.1.157、2.1.163、2.1.164、2.1.166、2.1.171、2.1.172、2.1.174、2.1.176、2.1.180、2.1.181、2.1.182、2.1.184、2.1.186、2.1.187、2.1.188、2.1.189、2.1.191、2.1.192、2.1.193、2.1.194、2.1.195、2.1.196、2.1.197、2.1.198、2.1.199、2.1.200、2.1.201、2.1.202、2.1.203、2.1.204、2.1.206、2.1.207、2.1.208、2.1.209、2.1.210、2.1.213、2.1.225、2.1.227、2.1.228、2.1.229、2.1.230、2.1.231、2.1.232、2.1.233、2.1.234、2.1.235、2.1.236、2.1.243、2.1.244、2.1.245、2.1.246、2.1.247、2.1.248、2.1.249、2.1.250、2.1.251、2.1.253、2.1.254、2.1.255、2.1.256、2.1.257、2.1.258、2.1.259、2.1.261、2.1.262、2.1.264 未发布或未纳入支持;不代表未来 latest 自动稳定。 | +| macOS native binary | experimental | 2.1.113 - 2.1.265 (不含未纳入本轮支持的 2.1.115, 2.1.125, 2.1.127, 2.1.130, 2.1.134, 2.1.135, 2.1.147, 2.1.149, 2.1.151, 2.1.154, 2.1.155, 2.1.157, 2.1.163, 2.1.164, 2.1.166, 2.1.171, 2.1.172, 2.1.174, 2.1.176, 2.1.180, 2.1.181, 2.1.182, 2.1.184, 2.1.186, 2.1.187, 2.1.188, 2.1.189, 2.1.191, 2.1.192, 2.1.193, 2.1.194, 2.1.195, 2.1.196, 2.1.197, 2.1.198, 2.1.199, 2.1.200, 2.1.201, 2.1.202, 2.1.203, 2.1.204, 2.1.206, 2.1.207, 2.1.208, 2.1.209, 2.1.210, 2.1.213, 2.1.225, 2.1.227, 2.1.228, 2.1.229, 2.1.230, 2.1.231, 2.1.232, 2.1.233, 2.1.234, 2.1.235, 2.1.236, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.113 PASS(native 1358, display 11/11) · 2.1.114 PASS(native 1358, display 11/11) · 2.1.116 PASS(native 1351, display 11/11) · 2.1.117 PASS(native 1334, display 11/11) · 2.1.118 PASS(native 1323, display 11/11) · 2.1.119 PASS(native 1328, display 11/11) · 2.1.120 PASS(native 1331, display 11/11) · 2.1.121 PASS(native 1334, display 11/11) · 2.1.122 PASS(native 1334, display 11/11) · 2.1.123 PASS(native 1334, display 11/11) · 2.1.124 PASS(native 1331, display 11/11) · 2.1.126 PASS(native 1331, display 11/11) · 2.1.128 PASS(native 1331, display 11/11) · 2.1.129 PASS(native 1333, display 11/11) · 2.1.131 PASS(native 1333, display 11/11) · 2.1.132 PASS(native 1323, display 11/11) · 2.1.133 PASS(native 1323, display 11/11) · 2.1.136 PASS(native 1322, display 11/11) · 2.1.137 PASS(native 1322, display 11/11) · 2.1.138 PASS(native 1322, display 11/11) · 2.1.139 PASS(native 1324, display 11/11) · 2.1.140 PASS(native 1324, display 11/11) · 2.1.141 PASS(native 1324, display 11/11) · 2.1.142 PASS(native 1320, display 11/11) · 2.1.143 PASS(native 1326, display 11/11) · 2.1.144 PASS(native 1324, display 11/11) · 2.1.145 PASS(native 1324, display 11/11) · 2.1.146 PASS(native 1335, display 11/11) · 2.1.148 PASS(native 1333, display 11/11) · 2.1.150 PASS(native 1333, display 11/11) · 2.1.152 PASS(native 1343, display 11/11) · 2.1.153 PASS(native 1343, display 11/11) · 2.1.156 PASS(native 1385, display 11/11) · 2.1.158 PASS(native 1385, display 11/11) · 2.1.159 PASS(native 1392, display 11/11) · 2.1.160 PASS(native 1392, display 11/11) · 2.1.161 PASS(native 1387, display 11/11) · 2.1.162 PASS(native 1385, display 11/11) · 2.1.165 PASS(native 1370, display 11/11) · 2.1.167 PASS(native 1405, display 11/11) · 2.1.168 PASS(native 1405, display 11/11) · 2.1.169 PASS(native 1392, display 11/11) · 2.1.170 PASS(native 1389, display 11/11) · 2.1.173 PASS(native 1381, display 11/11) · 2.1.175 PASS(native 1381, display 11/11) · 2.1.177 PASS(native 1381, display 11/11) · 2.1.178 PASS(native 1473, display 11/11) · 2.1.179 PASS(native 1463, display 11/11) · 2.1.183 PASS(native 1462, display 11/11) · 2.1.185 PASS(native 1462, display 11/11) · 2.1.190 PASS(native 1452, display 11/11) · 2.1.205 PASS(native 1395, display 11/11) · 2.1.211 PASS(native 1390, display runtime 11/11, coverage PARTIAL 4) · 2.1.212 PASS(native 1386, display runtime 11/11, coverage PARTIAL 8) · 2.1.214 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.215 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.216 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.217 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.218 PASS(native 1383, display runtime 11/11, coverage PARTIAL 13) · 2.1.219 PASS(native 1382, display runtime 11/11, coverage PARTIAL 13) · 2.1.220 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.221 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.222 PASS(native 1491, display runtime 11/11, coverage PARTIAL 15) · 2.1.223 PASS(native 1490, display runtime 11/11, coverage PARTIAL 15) · 2.1.224 PASS(native 1490, display runtime 11/11, coverage PARTIAL 20) · 2.1.226 PASS(native 1490, display runtime 11/11, coverage PARTIAL 20) · 2.1.237 PASS(native 1720, display runtime 11/11, coverage PARTIAL 29) · 2.1.238 PASS(native 1717, display runtime 11/11, coverage PARTIAL 39) · 2.1.239 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.240 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.241 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.242 PASS(native 1562, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1139, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1139, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1151, display runtime 11/11, coverage PARTIAL 232) | 已验证版本见本矩阵;同平台未收录版本可在启动前按程序结构进行本机验证,不代表完整中文覆盖。 | | Linux official installer (other shapes) | unsupported | - | - | Linux 官方安装器仅支持矩阵列出的 linux-x64 glibc native 版本;arm64、musl 和未验证版本不执行 CLI Patch。 | -| Linux native binary (x64 glibc) | experimental | 2.1.220 - 2.1.265 (不含未纳入本轮支持的 2.1.221, 2.1.222, 2.1.223, 2.1.224, 2.1.225, 2.1.226, 2.1.227, 2.1.228, 2.1.229, 2.1.230, 2.1.231, 2.1.232, 2.1.233, 2.1.234, 2.1.235, 2.1.236, 2.1.237, 2.1.238, 2.1.239, 2.1.240, 2.1.241, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.220 PASS(native ELF x64 glibc) · 2.1.242 PASS(native 1561, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1145, display runtime 11/11, coverage PARTIAL 232) | Linux x64 glibc native binary experimental;需要 node-lief;已验证 2.1.220、2.1.242、2.1.252、2.1.260、2.1.263、2.1.265 的原生汉化、--version 启动自检和 11 个稳定显示面的运行审计;2.1.265 的运行边界通过,但展示文案覆盖为 PARTIAL(232 个警告),不代表完整中文覆盖;2.1.221、2.1.222、2.1.223、2.1.224、2.1.225、2.1.226、2.1.227、2.1.228、2.1.229、2.1.230、2.1.231、2.1.232、2.1.233、2.1.234、2.1.235、2.1.236、2.1.237、2.1.238、2.1.239、2.1.240、2.1.241、2.1.243、2.1.244、2.1.245、2.1.246、2.1.247、2.1.248、2.1.249、2.1.250、2.1.251、2.1.253、2.1.254、2.1.255、2.1.256、2.1.257、2.1.258、2.1.259、2.1.261、2.1.262、2.1.264 未发布或未纳入支持;仅启用已验证版本;不支持 arm64、musl 或 provisional latest。 | +| Linux native binary (x64 glibc) | experimental | 2.1.220 - 2.1.265 (不含未纳入本轮支持的 2.1.221, 2.1.222, 2.1.223, 2.1.224, 2.1.225, 2.1.226, 2.1.227, 2.1.228, 2.1.229, 2.1.230, 2.1.231, 2.1.232, 2.1.233, 2.1.234, 2.1.235, 2.1.236, 2.1.237, 2.1.238, 2.1.239, 2.1.240, 2.1.241, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.220 PASS(native ELF x64 glibc) · 2.1.242 PASS(native 1561, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1145, display runtime 11/11, coverage PARTIAL 232) | 已验证版本见本矩阵;同平台未收录版本可在启动前按程序结构进行本机验证,不代表完整中文覆盖。 | | Windows / npm global install (PowerShell) | stable | 2.1.92 - 2.1.112 | - | 新增 PowerShell 安装脚本(install.ps1);适用于旧 npm cli.js 形态,CLI Patch 可用;Windows 上 session-start 二层兜底(launcher 暂不实现启动前自修复)。 | -| Windows / native .exe | experimental | 2.1.113 - 2.1.265 (不含未纳入本轮支持的 2.1.115, 2.1.125, 2.1.127, 2.1.130, 2.1.134, 2.1.135, 2.1.147, 2.1.149, 2.1.151, 2.1.154, 2.1.155, 2.1.157, 2.1.163, 2.1.164, 2.1.165, 2.1.166, 2.1.167, 2.1.168, 2.1.169, 2.1.170, 2.1.171, 2.1.172, 2.1.173, 2.1.174, 2.1.175, 2.1.176, 2.1.180, 2.1.181, 2.1.182, 2.1.184, 2.1.185, 2.1.186, 2.1.187, 2.1.188, 2.1.189, 2.1.191, 2.1.192, 2.1.193, 2.1.194, 2.1.195, 2.1.196, 2.1.197, 2.1.198, 2.1.199, 2.1.200, 2.1.201, 2.1.202, 2.1.203, 2.1.204, 2.1.206, 2.1.207, 2.1.208, 2.1.209, 2.1.210, 2.1.213, 2.1.234, 2.1.235, 2.1.236, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.158 PASS(native 1385, display 11/11) · 2.1.159 PASS(native 1392, display 11/11) · 2.1.160 PASS(native 1392, display 11/11) · 2.1.161 PASS(native 1387, display 11/11) · 2.1.162 PASS(native 1385, display 11/11) · 2.1.177 PASS(native 1381, display 11/11) · 2.1.178 PASS(native 1473, display 11/11) · 2.1.179 PASS(native 1463, display 11/11) · 2.1.183 PASS(native 1462, display 11/11) · 2.1.190 PASS(native 1452, display 11/11) · 2.1.205 PASS(native 1395, display 11/11) · 2.1.211 PASS(native 1390, display runtime 11/11, coverage PARTIAL 4) · 2.1.212 PASS(native 1386, display runtime 11/11, coverage PARTIAL 8) · 2.1.214 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.215 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.216 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.217 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.218 PASS(native 1383, display runtime 11/11, coverage PARTIAL 13) · 2.1.219 PASS(native 1382, display runtime 11/11, coverage PARTIAL 13) · 2.1.220 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.224 PARTIAL(native 1581, extract+patch PASS,新增 teamMemory/hook 结构规则生效,repack/display runtime 待用户手动验证) · 2.1.233 PASS(native 1610, display runtime 11/11, coverage PARTIAL 24) · 2.1.237 PASS(native 1720, display runtime 11/11, coverage PARTIAL 29) · 2.1.238 PASS(native 1717, display runtime 11/11, coverage PARTIAL 39) · 2.1.239 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.240 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.241 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.242 PASS(native 1555, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1145, display runtime 11/11, coverage PARTIAL 232) | Windows x64 native binary experimental;需要 node-lief;已验证 2.1.113 - 2.1.114、2.1.116 - 2.1.124、2.1.126、2.1.128 - 2.1.129、2.1.131 - 2.1.133、2.1.136 - 2.1.146、2.1.148、2.1.150、2.1.152 - 2.1.153、2.1.156、2.1.158 - 2.1.162、2.1.177 - 2.1.179、2.1.183、2.1.190、2.1.205、2.1.211 - 2.1.212、2.1.214 - 2.1.224、2.1.233、2.1.237 - 2.1.242、2.1.252、2.1.260、2.1.263、2.1.265 的原生汉化、--version 启动自检和 11 个稳定显示面的运行审计;2.1.265 的运行边界通过,但展示文案覆盖为 PARTIAL(232 个警告),不代表完整中文覆盖;2.1.115、2.1.125、2.1.127、2.1.130、2.1.134、2.1.135、2.1.147、2.1.149、2.1.151、2.1.154、2.1.155、2.1.157、2.1.163、2.1.164、2.1.165、2.1.166、2.1.167、2.1.168、2.1.169、2.1.170、2.1.171、2.1.172、2.1.173、2.1.174、2.1.175、2.1.176、2.1.180、2.1.181、2.1.182、2.1.184、2.1.185、2.1.186、2.1.187、2.1.188、2.1.189、2.1.191、2.1.192、2.1.193、2.1.194、2.1.195、2.1.196、2.1.197、2.1.198、2.1.199、2.1.200、2.1.201、2.1.202、2.1.203、2.1.204、2.1.206、2.1.207、2.1.208、2.1.209、2.1.210、2.1.213、2.1.234、2.1.235、2.1.236、2.1.243、2.1.244、2.1.245、2.1.246、2.1.247、2.1.248、2.1.249、2.1.250、2.1.251、2.1.253、2.1.254、2.1.255、2.1.256、2.1.257、2.1.258、2.1.259、2.1.261、2.1.262、2.1.264 未发布或未纳入支持;不代表 future latest 自动稳定。高于该窗口但仍在同一 minor 线的版本,install.ps1 可在安装时做本机自验证并以 provisional 方式启用;这不等于已发布支持窗口。 | +| Windows / native .exe | experimental | 2.1.113 - 2.1.265 (不含未纳入本轮支持的 2.1.115, 2.1.125, 2.1.127, 2.1.130, 2.1.134, 2.1.135, 2.1.147, 2.1.149, 2.1.151, 2.1.154, 2.1.155, 2.1.157, 2.1.163, 2.1.164, 2.1.165, 2.1.166, 2.1.167, 2.1.168, 2.1.169, 2.1.170, 2.1.171, 2.1.172, 2.1.173, 2.1.174, 2.1.175, 2.1.176, 2.1.180, 2.1.181, 2.1.182, 2.1.184, 2.1.185, 2.1.186, 2.1.187, 2.1.188, 2.1.189, 2.1.191, 2.1.192, 2.1.193, 2.1.194, 2.1.195, 2.1.196, 2.1.197, 2.1.198, 2.1.199, 2.1.200, 2.1.201, 2.1.202, 2.1.203, 2.1.204, 2.1.206, 2.1.207, 2.1.208, 2.1.209, 2.1.210, 2.1.213, 2.1.234, 2.1.235, 2.1.236, 2.1.243, 2.1.244, 2.1.245, 2.1.246, 2.1.247, 2.1.248, 2.1.249, 2.1.250, 2.1.251, 2.1.253, 2.1.254, 2.1.255, 2.1.256, 2.1.257, 2.1.258, 2.1.259, 2.1.261, 2.1.262, 2.1.264) | 2.1.158 PASS(native 1385, display 11/11) · 2.1.159 PASS(native 1392, display 11/11) · 2.1.160 PASS(native 1392, display 11/11) · 2.1.161 PASS(native 1387, display 11/11) · 2.1.162 PASS(native 1385, display 11/11) · 2.1.177 PASS(native 1381, display 11/11) · 2.1.178 PASS(native 1473, display 11/11) · 2.1.179 PASS(native 1463, display 11/11) · 2.1.183 PASS(native 1462, display 11/11) · 2.1.190 PASS(native 1452, display 11/11) · 2.1.205 PASS(native 1395, display 11/11) · 2.1.211 PASS(native 1390, display runtime 11/11, coverage PARTIAL 4) · 2.1.212 PASS(native 1386, display runtime 11/11, coverage PARTIAL 8) · 2.1.214 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.215 PASS(native 1385, display runtime 11/11, coverage PARTIAL 13) · 2.1.216 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.217 PASS(native 1384, display runtime 11/11, coverage PARTIAL 13) · 2.1.218 PASS(native 1383, display runtime 11/11, coverage PARTIAL 13) · 2.1.219 PASS(native 1382, display runtime 11/11, coverage PARTIAL 13) · 2.1.220 PASS(native 1492, display runtime 11/11, coverage PARTIAL 13) · 2.1.224 PARTIAL(native 1581, extract+patch PASS,新增 teamMemory/hook 结构规则生效,repack/display runtime 待用户手动验证) · 2.1.233 PASS(native 1610, display runtime 11/11, coverage PARTIAL 24) · 2.1.237 PASS(native 1720, display runtime 11/11, coverage PARTIAL 29) · 2.1.238 PASS(native 1717, display runtime 11/11, coverage PARTIAL 39) · 2.1.239 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.240 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.241 PASS(native 1715, display runtime 11/11, coverage PARTIAL 39) · 2.1.242 PASS(native 1555, display runtime 11/11, coverage PARTIAL 162) · 2.1.252 PASS(native 1145, display runtime 11/11, coverage PARTIAL 195) · 2.1.260 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.263 PASS(native 1133, display runtime 11/11, coverage PARTIAL 222) · 2.1.265 PASS(native 1145, display runtime 11/11, coverage PARTIAL 232) | 已验证版本见本矩阵;同平台未收录版本可在启动前按程序结构进行本机验证,不代表完整中文覆盖。 | ## Compatibility Matrix diff --git a/install.ps1 b/install.ps1 index de82dab4..535b3c36 100644 --- a/install.ps1 +++ b/install.ps1 @@ -595,6 +595,8 @@ function detect-launcher-install { return $null } + $nativeInfo = (& node "$PluginDst/bun-binary-io.js" detect $realPath 2>$null | Out-String).Trim() + if ($nativeInfo.StartsWith("native-bun:")) { return $nativeInfo } $claudeDir = Split-Path -Parent $realPath $candidates = @( (Join-Path $claudeDir "..\lib\node_modules\@anthropic-ai\claude-code\cli.js"), @@ -954,7 +956,7 @@ function install-launcher { $kind = ($installInfo -split ':', 2)[0] } - if ($kind -ne "npm") { + if ($kind -ne "npm" -and $kind -ne "native-bun") { remove-launcher-artifacts if (-not $SkipBanner) { Write-CN "当前安装方式不是 npm cli.js,已跳过 launcher PATH 注入" Yellow @@ -969,6 +971,7 @@ function install-launcher { return } New-Item -ItemType Directory -Force -Path $LauncherBinDir | Out-Null + Copy-Item "$PluginSrc\scripts\resolve-runtime.js" "$LauncherBinDir\resolve-runtime.js" -Force Copy-Item "$PluginSrc\bin\claude-launcher.ps1" "$LauncherBinDir\claude.ps1" -Force Copy-Item "$PluginSrc\bin\claude-launcher.cmd" "$LauncherBinDir\claude.cmd" -Force @@ -1158,159 +1161,14 @@ function get-native-version-from-execution { function patch-native-bun { param([string]$BinaryPath) - $helper = "$PluginDst\bun-binary-io.js" - if (-not (Test-Path $helper)) { - $helper = "$PluginSrc\bun-binary-io.js" - } - if (-not (Test-Path $helper)) { - Write-CN "原生二进制 patch helper 缺失,已跳过 CLI Patch" Yellow - write-support-window-link - $script:CliPatchStatusSummary = "已跳过(原生二进制 helper 缺失)" - return - } - - Write-Host "" - Write-CN "检测到 Windows 原生二进制安装" Blue - Write-Host " 二进制路径: $BinaryPath" - - $currentVersion = (node $helper version $BinaryPath 2>$null) - if ($currentVersion) { $currentVersion = $currentVersion.Trim() } - - $patchMode = "verified" - if (is-supported-windows-native-version $currentVersion) { - $patchMode = "verified" - } elseif (can-try-provisional-windows-native-version $currentVersion) { - $patchMode = "provisional" - } else { - $displayVersion = $currentVersion - if (-not $displayVersion) { $displayVersion = "unknown" } - Write-CN "当前 Windows 原生二进制版本 $displayVersion 暂不支持 CLI Patch,已跳过 CLI Patch(安全退出)" Yellow - write-support-window-link - write-updater-boundary-note - Write-CN " 下一步:如果是 Claude Code 自动升到未发布窗口,请等插件发布支持,或临时安装支持窗口内版本。" Yellow - $script:CliPatchStatusSummary = "已跳过(Windows 原生二进制版本 $displayVersion 暂不支持 CLI Patch)" - return - } - - if ($patchMode -eq "provisional") { - Write-Host " 版本: $currentVersion(未纳入已发布支持窗口,安装时本机自验证)" - write-support-window-link - write-unpublished-window-note - } else { - Write-Host " 版本: $currentVersion(已验证)" - } - - if (-not (test-binary-writable $BinaryPath)) { - $procs = find-claude-processes - $procList = ($procs | ForEach-Object { "PID=$($_.Id)" }) -join ", " - if (-not $procList) { $procList = "(未列出 claude 进程,可能由其他句柄占用)" } - Write-CN " 原生二进制无法独占写入($procList):$script:NativeWriteFailure" Red - Write-CN " 请手动退出所有 Claude Code 实例(关闭所有 CC 窗口,含当前会话),然后重新运行 install.ps1。" Yellow - Write-CN " Layer 1~3(settings / 插件目录 / hooks)已在本会话写入;CLI Patch 待所有实例退出后重跑才能完成。" Yellow - write-support-window-link - $script:CliPatchStatusSummary = "已中止(原生二进制被运行中的 Claude Code 占用,需手动退出所有 claude 实例后重跑)" - Write-CN "脚本停止。" Red - exit 1 - } - - $depStatus = (node $helper check-deps 2>$null) - if (-not $depStatus -or $depStatus.Trim() -ne "ok") { - Write-CN "需要安装 node-lief 来支持 Windows native patch" Yellow - Write-Host " 运行: npm install -g node-lief" - Write-Host " 然后重新运行 install.ps1" - write-support-window-link - $script:CliPatchStatusSummary = "已跳过(Windows native CLI Patch 需要 node-lief)" - return - } - - $containerLayout = ((& node $helper probe $BinaryPath 2>$null) | Out-String).Trim() - - $tmpJs = Join-Path $TmpDir "claude-zh-cn-extract-$PID.js" - $backupFile = "$BinaryPath.zh-cn-backup" - New-Item -Force -ItemType Directory -Path $TmpDir | Out-Null - - $backupVersion = "" - if (Test-Path $backupFile) { - $backupVersion = (node $helper version $backupFile 2>$null) - if ($backupVersion) { $backupVersion = $backupVersion.Trim() } - } - - if ((Test-Path $backupFile) -and $currentVersion -and $backupVersion -eq $currentVersion) { - Copy-Item $backupFile $BinaryPath -Force - Write-CN "已从备份恢复原始原生二进制(版本一致: $currentVersion)" Green + $result = & node "$PluginDst\scripts\native-repair.js" $BinaryPath $PluginDst + if ($LASTEXITCODE -eq 0) { + $script:CliPatchStatusOk = $true + $script:CliPatchStatusSummary = "本机自验证通过(未纳入已发布支持窗口的版本标记为 provisional)" } else { - Copy-Item $BinaryPath $backupFile -Force - Write-CN "已备份原生二进制(版本: $currentVersion)" Green - } - - $sourceHash = (node $helper hash $BinaryPath 2>$null) - if ($sourceHash) { $sourceHash = $sourceHash.Trim() } - if (-not $sourceHash) { $sourceHash = "unknown" } - - try { - $translationsFile = Join-Path $PluginDst "cli-translations.json" - if ($containerLayout -eq "bytecode") { - $patchCount = node "$PluginDst\scripts\patch-bytecode.js" patch $BinaryPath $translationsFile - if ($LASTEXITCODE -ne 0) { throw "bytecode patch failed" } - } else { - node $helper extract $BinaryPath $tmpJs | Out-Null - if ($LASTEXITCODE -ne 0) { throw "extract failed" } - $patchScript = Join-Path $PluginDst "patch-cli.js" - $patchCount = node $patchScript $tmpJs $translationsFile 2>$null - if ($LASTEXITCODE -ne 0) { throw "patch-cli failed" } - } - if (-not $patchCount) { $patchCount = "0" } - - if ([int]$patchCount -gt 0) { - if ($containerLayout -ne "bytecode") { - node $helper repack $BinaryPath $tmpJs | Out-Null - if ($LASTEXITCODE -ne 0) { throw "repack failed" } - } - Write-Host " 正在运行 --version 做启动自检..." - $verifiedVersion = get-native-version-from-execution $BinaryPath - if ($verifiedVersion -ne $currentVersion) { throw "self verification failed" } - if ($patchMode -eq "provisional") { - Write-CN "本机自验证通过,已 patch Windows 原生二进制(${patchCount} 处硬编码文字)" Green - $script:CliPatchStatusSummary = "Windows native 本机自验证中文化(${patchCount} 处硬编码文字,未纳入已发布支持窗口)" - } else { - Write-CN "已 patch Windows 原生二进制(${patchCount} 处硬编码文字)" Green - $script:CliPatchStatusSummary = "Windows native 中文化(${patchCount} 处硬编码文字)" - } - $script:CliPatchStatusOk = $true - } else { - Write-CN "Windows 原生二进制无新增改动(可能已是最新状态)" Yellow - if ($patchMode -eq "provisional") { - $script:CliPatchStatusSummary = "已跳过(Windows 原生二进制本机自验证未找到可 patch 内容)" - write-support-window-link - return - } else { - $script:CliPatchStatusSummary = "Windows native 无新增改动(可能已是最新状态)" - $script:CliPatchStatusOk = $true - } - } - } catch { - Write-CN "Windows 原生二进制 patch 失败,正在从备份恢复..." Red - if (Test-Path $backupFile) { - Copy-Item $backupFile $BinaryPath -Force -ErrorAction SilentlyContinue - } - write-support-window-link - $script:CliPatchStatusSummary = "已跳过(Windows 原生二进制 patch 失败)" - return - } finally { - Remove-Item $tmpJs -Force -ErrorAction SilentlyContinue - } - - $patchRevision = get-patch-revision $PluginDst - $finalHash = (node $helper hash $BinaryPath 2>$null) - if ($finalHash) { $finalHash = $finalHash.Trim() } - if ($patchRevision -and $currentVersion) { - if (-not $finalHash) { $finalHash = "unknown" } - if ($patchMode -eq "provisional") { - "native|${currentVersion}|${finalHash}|${patchRevision}|provisional|win32-x64|${sourceHash}" | Out-File -FilePath $MarkerFile -Encoding ascii -NoNewline - } else { - "native|${currentVersion}|${finalHash}|${patchRevision}" | Out-File -FilePath $MarkerFile -Encoding ascii -NoNewline - } + $script:CliPatchStatusSummary = "汉化未完成;请查看具体错误提示,未写入成功标记" } + Write-CN $script:CliPatchStatusSummary Yellow } function initial-patch { diff --git a/install.sh b/install.sh index 47296239..de05d7c4 100755 --- a/install.sh +++ b/install.sh @@ -42,43 +42,8 @@ print_updater_boundary_note() { echo -e " ${YELLOW}!${NC} Claude Code 本体自动升级 → DISABLE_AUTOUPDATER 不归本插件兜底;请以 claude doctor 的 Updates 段为准" } -# 检测 CC 是否已禁用自动更新;未禁用时警告,ZH_CN_DISABLE_CC_AUTOUPDATE=1 则主动写入 shell profile。 -# patch 在 CC 自动更新下载全新 binary 后会失效,禁用自动更新是保持汉化持久的前提。 -ensure_cc_autoupdate_disabled() { - if [ "${DISABLE_AUTOUPDATER:-}" = "1" ] || [ "${DISABLE_AUTOUPDATER:-}" = "true" ]; then - return 0 - fi - if command -v node >/dev/null 2>&1 && [ -f "$SETTINGS_FILE" ]; then - if node -e 'const e=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8")).env||{};process.exit((e.DISABLE_AUTOUPDATER==="1"||e.DISABLE_AUTOUPDATER==="true")?0:1)' "$SETTINGS_FILE" 2>/dev/null; then - return 0 - fi - fi - local profile - for profile in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile"; do - [ -f "$profile" ] || continue - if grep -qE '^[[:space:]]*(export[[:space:]]+)?DISABLE_AUTOUPDATER=(1|true|"1"|"true"|'"'"'1'"'"'|'"'"'true'"'"')([[:space:]]|$|#)' "$profile" 2>/dev/null; then - return 0 - fi - done - if [ "${ZH_CN_DISABLE_CC_AUTOUPDATE:-0}" = "1" ]; then - local target="$HOME/.zshrc" - [ -n "${SHELL##*zsh*}" ] && [ -f "$HOME/.bashrc" ] && target="$HOME/.bashrc" - if ! grep -qE '^\s*(export\s+)?DISABLE_AUTOUPDATER=' "$target" 2>/dev/null; then - { - echo "" - echo "# 禁用 Claude Code 自动更新(由 claude-code-zh-cn 安装脚本添加,防止 CC 升级后 patch 失效)" - echo "export DISABLE_AUTOUPDATER=1" - } >> "$target" - echo -e " ${GREEN}✓${NC} 已在 $target 添加 export DISABLE_AUTOUPDATER=1(重新打开终端后生效)" - fi - else - echo -e " ${YELLOW}!${NC} Claude Code 自动更新未禁用:CC 升级后 patch 会失效,需重新安装" - echo -e " 一键禁用:重跑安装时加 ZH_CN_DISABLE_CC_AUTOUPDATE=1,或手动在 shell profile 加 export DISABLE_AUTOUPDATER=1" - fi -} - print_unpublished_window_note() { - echo -e "${YELLOW} 提醒:本机自验证是临时 patch,不等于已发布支持;升到未发布窗口时请先看支持窗口,未收录就等插件 Release 或临时退回已验证版本。${NC}" + echo -e "${YELLOW} 提醒:本机自验证不等于已发布支持;普通更新会在下次启动前检查,未知文案继续显示英文。${NC}" } make_private_temp_dir() { @@ -129,9 +94,9 @@ print_completion() { echo -e " ${GREEN}✓${NC} 输出风格 → Chinese" echo -e " ${GREEN}✓${NC} 自动重 patch → Claude Code 更新后首次会话自动修复" if [ "$LAUNCHER_STATUS_OK" = true ]; then - echo -e " ${GREEN}✓${NC} npm 启动前自修复 → ${LAUNCHER_STATUS_SUMMARY}" + echo -e " ${GREEN}✓${NC} 启动前自修复 → ${LAUNCHER_STATUS_SUMMARY}" else - echo -e " ${YELLOW}!${NC} npm 启动前自修复 → ${LAUNCHER_STATUS_SUMMARY}" + echo -e " ${YELLOW}!${NC} 启动前自修复 → ${LAUNCHER_STATUS_SUMMARY}" fi case "$PLUGIN_RUNTIME_MODE" in standalone) @@ -157,7 +122,7 @@ print_completion() { if [[ "${install_info:-}" == native-bun:* ]]; then echo "" if [[ "$(native_platform)" == linux-* ]]; then - echo -e " ${YELLOW}!${NC} Linux native patch:仅对已验证支持窗口开放" + echo -e " ${YELLOW}!${NC} Linux x64 glibc 未收录版本会先本机验证;不支持 arm64、musl" else echo -e " ${YELLOW}!${NC} 官方安装器 native patch:已验证版本有公开证据;更高可识别版本会在安装时本机自验证" fi @@ -196,249 +161,15 @@ check_dependencies() { local install_info install_info="$(detect_installation)" if [[ "${install_info:-}" == native-bun:* ]]; then - local native_path native_version dep_status platform lief_requirement - native_path="${install_info#*:}" - native_version="$(native_binary_version "$native_path")" - platform="$(native_platform)" - lief_requirement="node-lief" - [[ "$platform" == linux-* ]] && lief_requirement="node-lief >= 1.3.0" - dep_status="$(node "$PLUGIN_SRC/bun-binary-io.js" check-deps 2>/dev/null || echo "missing")" - - if is_supported_native_version "$native_version"; then - if [ "$dep_status" != "ok" ]; then - echo -e "${YELLOW}检测到已验证原生二进制版本 ${native_version:-unknown},CLI Patch 需要 ${lief_requirement}${NC}" - if [[ "$platform" == linux-* ]]; then - echo -e " 运行: ${GREEN}npm install -g node-lief@^1.3.0${NC}" - else - echo -e " 运行: ${GREEN}npm install -g node-lief${NC}" - fi - else - echo -e "${YELLOW}检测到已验证原生二进制版本 ${native_version},将执行 native patch${NC}" - fi - elif can_try_provisional_native_version "$native_version"; then - if [ "$dep_status" != "ok" ]; then - echo -e "${YELLOW}检测到新原生二进制版本 ${native_version:-unknown},可在安装时本机自验证;需要 node-lief${NC}" - echo -e " 运行: ${GREEN}npm install -g node-lief${NC}" - else - echo -e "${YELLOW}检测到新原生二进制版本 ${native_version},将尝试本机自验证;通过才启用 CLI Patch${NC}" - fi - else - echo -e "${YELLOW}检测到原生二进制安装方式;当前版本 ${native_version:-unknown} 暂不支持 CLI Patch,已跳过 CLI Patch(安全退出)${NC}" - echo -e " 当前平台 native 已验证窗口:$(native_support_summary)" - echo -e " 如需稳定 CLI 中文化,请使用 npm 安装 Claude Code 2.1.112" + echo "原生程序会按实际结构进行本机自验证;版本清单只记录发布验证结果。" + if [ "$(node "$PLUGIN_SRC/bun-binary-io.js" check-deps 2>/dev/null)" != "ok" ]; then + echo "CLI Patch 需要 node-lief(Linux x64 glibc 需要 >= 1.3.0);请安装 npm install -g node-lief@1.3.2" fi fi } -native_binary_version() { - local binary_path="$1" - local version output temp_home - - version="$(node "$PLUGIN_SRC/bun-binary-io.js" version "$binary_path" 2>/dev/null || true)" - if [ -n "${version:-}" ]; then - printf '%s' "$version" - return - fi - - temp_home="$(mktemp -d "${TMPDIR:-/tmp}/cczh-version-home.XXXXXX" 2>/dev/null || true)" - if [ -n "${temp_home:-}" ]; then - output="$(HOME="$temp_home" XDG_CONFIG_HOME="$temp_home/.config" XDG_CACHE_HOME="$temp_home/.cache" XDG_DATA_HOME="$temp_home/.local/share" "$binary_path" --version 2>/dev/null || true)" - rm -rf "$temp_home" 2>/dev/null || true - else - output="$("$binary_path" --version 2>/dev/null || true)" - fi - - printf '%s' "$output" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true -} - -native_binary_version_from_execution() { - local binary_path="$1" - local output temp_home - - temp_home="$(mktemp -d "${TMPDIR:-/tmp}/cczh-version-home.XXXXXX" 2>/dev/null || true)" - if [ -n "${temp_home:-}" ]; then - output="$(HOME="$temp_home" XDG_CONFIG_HOME="$temp_home/.config" XDG_CACHE_HOME="$temp_home/.cache" XDG_DATA_HOME="$temp_home/.local/share" "$binary_path" --version 2>/dev/null || true)" - rm -rf "$temp_home" 2>/dev/null || true - else - output="$("$binary_path" --version 2>/dev/null || true)" - fi - - printf '%s' "$output" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true -} - native_platform() { - if [ -n "${ZH_CN_NATIVE_PLATFORM:-}" ]; then - printf '%s' "$ZH_CN_NATIVE_PLATFORM" - return - fi - - local system_arch linux_arch="" - system_arch="$(uname -s 2>/dev/null)-$(uname -m 2>/dev/null)" - case "$system_arch" in - Darwin-arm64|Darwin-aarch64) - printf 'darwin-arm64' - return - ;; - Linux-x86_64|Linux-amd64) - linux_arch="x64" - ;; - Linux-arm64|Linux-aarch64) - linux_arch="arm64" - ;; - *) - return - ;; - esac - - if node -e 'const r=process.report&&process.report.getReport?process.report.getReport():null;process.exit(r&&r.header&&r.header.glibcVersionRuntime?0:1)' >/dev/null 2>&1; then - printf 'linux-%s' "$linux_arch" - else - printf 'linux-%s-musl' "$linux_arch" - fi -} - -is_supported_native_version() { - local version="$1" - local platform="${2:-$(native_platform)}" - local support_file="$PLUGIN_SRC/support-window.json" - - if [ ! -f "$support_file" ]; then - [ "$platform" = "darwin-arm64" ] || return 1 - case "${version:-}" in - 2.1.110|2.1.111|2.1.112) - return 0 - ;; - *) - return 1 - ;; - esac - fi - -node - "$support_file" "$version" "$platform" <<'NODE' -const fs = require("fs"); -const file = process.argv[2]; -const version = process.argv[3]; -const platform = process.argv[4] || ""; -const data = JSON.parse(fs.readFileSync(file, "utf8")); -const versions = []; -for (const key of [ - "macosNativeOfficialInstallerExperimental", - "macosNativeExperimental", - "linuxNativeExperimental", -]) { - const entry = data[key]; - if (!entry) continue; - if (platform && entry.platform && entry.platform !== platform) continue; - versions.push(...(entry.versions || [])); -} -process.exit(versions.includes(version) ? 0 : 1); -NODE -} - -can_try_provisional_native_version() { - local version="$1" - local platform="${2:-$(native_platform)}" - local support_file="$PLUGIN_SRC/support-window.json" - - if [ -z "${version:-}" ] || [ -z "${platform:-}" ] || [ ! -f "$support_file" ]; then - return 1 - fi - - node - "$support_file" "$version" "$platform" <<'NODE' -const fs = require("fs"); -const file = process.argv[2]; -const version = process.argv[3]; -const platform = process.argv[4]; -const data = JSON.parse(fs.readFileSync(file, "utf8")); - -function parse(v) { - return String(v || "").split(".").map((part) => { - const n = Number.parseInt(part, 10); - return Number.isFinite(n) ? n : 0; - }); -} - -function compare(a, b) { - const left = parse(a); - const right = parse(b); - const max = Math.max(left.length, right.length); - for (let i = 0; i < max; i += 1) { - const l = left[i] || 0; - const r = right[i] || 0; - if (l > r) return 1; - if (l < r) return -1; - } - return 0; -} - -const keys = ["macosNativeExperimental"]; -for (const key of keys) { - const entry = data[key]; - if (!entry || entry.platform !== platform || !entry.floor) continue; - if (compare(version, entry.floor) >= 0) { - process.exit(0); - } -} -process.exit(1); -NODE -} - -native_support_summary() { - local support_file="$PLUGIN_SRC/support-window.json" - local platform="${1:-$(native_platform)}" - - if [ ! -f "$support_file" ]; then - if [ "$platform" = "darwin-arm64" ]; then - printf "2.1.110 - 2.1.112" - else - printf "无" - fi - return - fi - - node - "$support_file" "$platform" <<'NODE' -const fs = require("fs"); -const file = process.argv[2]; -const platform = process.argv[3] || ""; -const data = JSON.parse(fs.readFileSync(file, "utf8")); -const ranges = []; -for (const key of [ - "macosNativeOfficialInstallerExperimental", - "macosNativeExperimental", - "linuxNativeExperimental", -]) { - const entry = data[key]; - if (!entry || !entry.floor || !entry.ceiling) continue; - if (platform && entry.platform && entry.platform !== platform) continue; - let range = entry.floor === entry.ceiling ? entry.floor : `${entry.floor} - ${entry.ceiling}`; - if (Array.isArray(entry.excluded) && entry.excluded.length > 0) { - range += ` (不含 ${entry.excluded.join(", ")})`; - } - ranges.push(range); -} -process.stdout.write(ranges.join(";") || "无"); -NODE -} - -replace_native_binary_from_file() { - local source_path="$1" - local binary_path="$2" - local replacement_path - - replacement_path="$(mktemp "${binary_path}.zh-cn-restore.XXXXXX" 2>/dev/null || true)" - [ -n "${replacement_path:-}" ] || return 1 - - if cp -p "$source_path" "$replacement_path" 2>/dev/null && mv -f "$replacement_path" "$binary_path" 2>/dev/null; then - return 0 - fi - - rm -f "$replacement_path" 2>/dev/null || true - return 1 -} - -native_binary_hash() { - local binary_path="$1" - node "$PLUGIN_SRC/bun-binary-io.js" hash "$binary_path" 2>/dev/null || printf "unknown" + node -e 'process.stdout.write(require(process.argv[1]).platform())' "$PLUGIN_SRC/scripts/native-repair.js" } ensure_settings_file() { @@ -1439,6 +1170,9 @@ remove_launcher_artifacts() { } detect_launcher_installation() { + local native_info + native_info="$(node "$PLUGIN_DST/bun-binary-io.js" detect "$(find_real_claude_binary)" 2>/dev/null || true)" + case "$native_info" in native-bun:*) printf "%s" "$native_info"; return ;; esac local claude_bin claude_bin="$(find_real_claude_binary)" if [ -z "$claude_bin" ]; then @@ -1478,11 +1212,11 @@ install_launcher() { install_info="$(detect_launcher_installation)" install_kind="${install_info%%:*}" - if [ "$install_kind" != "npm" ]; then + if [ "$install_kind" != "npm" ] && [ "$install_kind" != "native-bun" ]; then remove_launcher_artifacts LAUNCHER_STATUS_SUMMARY="已跳过(当前 claude 命令不是 npm cli.js)" if [ "$SKIP_BANNER" != "1" ]; then - echo -e "${YELLOW}当前安装方式不需要 npm 启动前自修复,已跳过 launcher PATH 注入${NC}" + echo -e "${YELLOW}当前安装方式不需要 启动前自修复,已跳过 launcher PATH 注入${NC}" fi return fi @@ -1495,6 +1229,7 @@ install_launcher() { mkdir -p "$LAUNCHER_BIN_DIR" cp "$source_launcher" "$LAUNCHER_FILE" + cp "$PLUGIN_DST/scripts/resolve-runtime.js" "$LAUNCHER_BIN_DIR/resolve-runtime.js" chmod +x "$LAUNCHER_FILE" 2>/dev/null || true while IFS= read -r target; do @@ -1505,7 +1240,7 @@ install_launcher() { if [ "$SKIP_BANNER" != "1" ]; then echo -e "${GREEN}已安装 launcher → ${LAUNCHER_FILE}${NC}" fi - LAUNCHER_STATUS_SUMMARY="npm 更新后首次启动会先 patch" + LAUNCHER_STATUS_SUMMARY="CC 更新后首次启动会先验证并补汉化" LAUNCHER_STATUS_OK=true } @@ -1655,156 +1390,14 @@ patch_npm_cli() { } patch_native_binary() { - local binary_path="$1" - local tmp_dir tmp_js - tmp_dir="$(make_private_temp_dir "claude-zh-cn-extract.")" - tmp_js="$tmp_dir/extracted.js" - local backup_path="${binary_path}.zh-cn-backup" - local current_version backup_version patch_mode platform - - echo "" - echo -e "${BLUE}检测到官方安装器(原生二进制)${NC}" - echo -e " 二进制路径: ${binary_path}" - - current_version="$(native_binary_version "$binary_path")" - platform="$(native_platform)" - patch_mode="verified" - if is_supported_native_version "$current_version" "$platform"; then - patch_mode="verified" - elif can_try_provisional_native_version "$current_version" "$platform"; then - patch_mode="provisional" - else - echo -e "${YELLOW}当前原生二进制版本 ${current_version:-unknown} 暂不支持 CLI Patch,已跳过 CLI Patch(安全退出)${NC}" - echo -e " 当前平台 native 已验证窗口:$(native_support_summary "$platform")" - echo -e " 如需稳定 CLI 中文化,请使用 npm 安装 Claude Code 2.1.112" - print_updater_boundary_note - echo -e "${YELLOW} 下一步:如果是 Claude Code 自动升到未发布窗口,请等插件发布支持,或临时安装支持窗口内版本。${NC}" - CLI_PATCH_STATUS_SUMMARY="已跳过(原生二进制版本 ${current_version:-unknown} 暂不支持 CLI Patch)" - return - fi - - if [ "$patch_mode" = "provisional" ]; then - echo -e " 版本: ${current_version}(未纳入已发布支持窗口,安装时本机自验证)" - print_unpublished_window_note - else - echo -e " 版本: ${current_version}(已验证)" - fi - - local dep_status - dep_status="$(node "$PLUGIN_SRC/bun-binary-io.js" check-deps 2>/dev/null || echo "missing")" - if [ "$dep_status" != "ok" ]; then - if [[ "$platform" == linux-* ]]; then - echo -e "${YELLOW}Linux native patch 需要 node-lief >= 1.3.0${NC}" - echo -e " 运行: ${GREEN}npm install -g node-lief@^1.3.0${NC}" - else - echo -e "${YELLOW}需要安装 node-lief 来支持官方安装器 native patch${NC}" - echo -e " 运行: ${GREEN}npm install -g node-lief${NC}" - fi - echo -e " 然后重新运行 ./install.sh" - CLI_PATCH_STATUS_SUMMARY="已跳过(官方安装器 CLI Patch 需要 node-lief)" - return - fi - - backup_version="" - if [ -f "$backup_path" ]; then - backup_version="$(native_binary_version "$backup_path")" - fi - - # 备份逻辑:仅同版本恢复 backup;版本变化时刷新 backup 为当前版本 - if [ -f "$backup_path" ] && [ -n "${current_version:-}" ] && [ "${current_version:-}" = "${backup_version:-}" ]; then - echo -e " 从备份恢复原始二进制..." - replace_native_binary_from_file "$backup_path" "$binary_path" || { - echo -e "${RED}恢复备份失败${NC}" - return - } - else - echo -e " 备份原始二进制..." - cp "$binary_path" "$backup_path" || { - echo -e "${RED}创建备份失败${NC}" - return - } - fi - - local source_hash - source_hash="$(native_binary_hash "$binary_path")" - - local patch_count container_layout - container_layout="$(node "$PLUGIN_SRC/bun-binary-io.js" probe "$binary_path")" - if [ "$container_layout" = "bytecode" ]; then - patch_count="$(node "$PLUGIN_SRC/scripts/patch-bytecode.js" patch "$binary_path" "$PLUGIN_SRC/cli-translations.json")" || { - CLI_PATCH_STATUS_SUMMARY="失败(字节码汉化未通过验证)" - rm -rf "$tmp_dir" - return - } - else - node "$PLUGIN_SRC/bun-binary-io.js" extract "$binary_path" "$tmp_js" || { - echo -e "${RED}提取 JS 失败${NC}" - CLI_PATCH_STATUS_SUMMARY="已跳过(原生二进制提取失败)" - rm -rf "$tmp_dir" - return - } - patch_count=$("$PLUGIN_SRC/patch-cli.sh" "$tmp_js" 2>/dev/null || echo "0") - fi - - if [ "$patch_count" != "0" ]; then - { [ "$container_layout" = "bytecode" ] || node "$PLUGIN_SRC/bun-binary-io.js" repack "$binary_path" "$tmp_js"; } || { - echo -e "${RED}写回二进制失败,正在从备份恢复...${NC}" - if replace_native_binary_from_file "$backup_path" "$binary_path" 2>/dev/null; then - CLI_PATCH_STATUS_SUMMARY="已跳过(原生二进制写回失败,已恢复原文件)" - else - echo -e "${RED}自动恢复失败;原始备份仍保留在 ${backup_path}${NC}" - CLI_PATCH_STATUS_SUMMARY="失败(原生二进制写回及自动恢复均失败;请从 ${backup_path} 手动恢复)" - fi - rm -rf "$tmp_dir" - return - } - local verified_version - echo -e " 正在运行 --version 做本机启动自检..." - verified_version="$(native_binary_version_from_execution "$binary_path")" - if [ "${verified_version:-}" != "${current_version:-}" ]; then - echo -e "${RED}本机启动自检失败,正在从备份恢复...${NC}" - if replace_native_binary_from_file "$backup_path" "$binary_path" 2>/dev/null; then - CLI_PATCH_STATUS_SUMMARY="已跳过(原生二进制本机启动自检失败,已恢复原文件)" - else - echo -e "${RED}自动恢复失败;原始备份仍保留在 ${backup_path}${NC}" - CLI_PATCH_STATUS_SUMMARY="失败(原生二进制启动自检及自动恢复均失败;请从 ${backup_path} 手动恢复)" - fi - rm -rf "$tmp_dir" - return - fi - - if [ "$patch_mode" = "provisional" ]; then - echo -e "${GREEN}本机自验证通过,已 patch 原生二进制(${patch_count} 处硬编码文字)${NC}" - CLI_PATCH_STATUS_SUMMARY="官方安装器 native 本机自验证中文化(${patch_count} 处硬编码文字,未纳入已发布支持窗口)" - else - echo -e "${GREEN}本机启动自检通过,已 patch 原生二进制(${patch_count} 处硬编码文字)${NC}" - CLI_PATCH_STATUS_SUMMARY="官方安装器 native 启动自检中文化(${patch_count} 处硬编码文字)" - fi + local result + if result="$(node "$PLUGIN_DST/scripts/native-repair.js" "$1" "$PLUGIN_DST")"; then CLI_PATCH_STATUS_OK=true + CLI_PATCH_STATUS_SUMMARY="$(node -e 'const r=JSON.parse(process.argv[1]);process.stdout.write("Claude Code "+r.version+" 本机自验证通过("+r.patched+" 处译文;"+(r.mode==="provisional"?"未纳入已发布支持窗口":"已发布验证版本")+")")' "$result")" + echo "$CLI_PATCH_STATUS_SUMMARY" else - echo -e "${YELLOW}未找到需要 patch 的内容${NC}" - if [ "$patch_mode" = "provisional" ]; then - CLI_PATCH_STATUS_SUMMARY="已跳过(原生二进制本机自验证未找到可 patch 内容)" - rm -rf "$tmp_dir" - return - else - CLI_PATCH_STATUS_SUMMARY="原生二进制无新增改动(可能已是最新状态)" - CLI_PATCH_STATUS_OK=true - fi - fi - - rm -rf "$tmp_dir" - - local patch_revision final_hash - current_version="$(native_binary_version "$binary_path")" - final_hash="$(native_binary_hash "$binary_path")" - patch_revision=$(compute_patch_revision "$PLUGIN_DST" 2>/dev/null || true) - if [ -n "${patch_revision:-}" ] && [ -n "${current_version:-}" ]; then - if [ "$patch_mode" = "provisional" ]; then - echo "native|${current_version}|${final_hash:-unknown}|${patch_revision}|provisional|${platform:-unknown}|${source_hash:-unknown}" > "$MARKER_FILE" - else - echo "native|${current_version}|${final_hash:-unknown}|${patch_revision}" > "$MARKER_FILE" - fi + CLI_PATCH_STATUS_SUMMARY="未完成;请按上方具体错误处理,正常升级后再次启动即可重试" + echo "$CLI_PATCH_STATUS_SUMMARY" fi } @@ -1854,7 +1447,6 @@ main() { initial_patch_cli fi - ensure_cc_autoupdate_disabled print_completion } diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json index da7aef5e..d3e49e6b 100644 --- a/plugin/.claude-plugin/plugin.json +++ b/plugin/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "claude-code-zh-cn", "displayName": "Claude Code 中文本地化", - "version": "2.16.1", + "version": "2.17.0", "description": "为 Claude Code 提供简体中文界面、本地化提示和安全的渐进式 CLI patch", "author": { "name": "taekchef" diff --git a/plugin/bin/claude-launcher b/plugin/bin/claude-launcher index 611c2a49..64edc829 100644 --- a/plugin/bin/claude-launcher +++ b/plugin/bin/claude-launcher @@ -6,6 +6,17 @@ PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$HOME/.claude/plugins/claude-code-zh-cn}" MARKER_FILE="$PLUGIN_ROOT/.patched-version" LAUNCHER_BIN_DIR="$(cd "$(dirname "$0")" && pwd)" WARNING_PREFIX="[claude-code-zh-cn]" +RESOLVER="$LAUNCHER_BIN_DIR/resolve-runtime.js" +[ -f "$RESOLVER" ] || RESOLVER="$LAUNCHER_BIN_DIR/../scripts/resolve-runtime.js" +if [ -f "$RESOLVER" ]; then + PLUGIN_ROOT="$(node "$RESOLVER")" +fi +if [ -n "$PLUGIN_ROOT" ] && [ "${ZH_CN_LAUNCHER_DISPATCHED:-}" != "1" ] && [ -f "$PLUGIN_ROOT/bin/claude-launcher" ]; then + exec env ZH_CN_LAUNCHER_DISPATCHED=1 ZH_CN_LAUNCHER_BIN_DIR="$LAUNCHER_BIN_DIR" bash "$PLUGIN_ROOT/bin/claude-launcher" "$@" +fi +LAUNCHER_BIN_DIR="${ZH_CN_LAUNCHER_BIN_DIR:-$LAUNCHER_BIN_DIR}" +MARKER_FILE="$PLUGIN_ROOT/.patched-version" + log_warning() { printf "%s %s\n" "$WARNING_PREFIX" "$1" >&2 @@ -175,6 +186,10 @@ main() { install_kind="${install_info%%:*}" install_target="${install_info#*:}" + if [ "$install_kind" = "native-bun" ] && [ -n "$PLUGIN_ROOT" ]; then + node "$PLUGIN_ROOT/scripts/native-repair.js" "$install_target" "${CLAUDE_PLUGIN_DATA:-$HOME/.claude/plugins/claude-code-zh-cn}" >/dev/null || true + fi + if [ "$install_kind" = "npm" ] && [ -f "$install_target" ]; then current_marker="$(compute_current_marker "$install_target" 2>/dev/null || true)" if [ -n "$current_marker" ]; then diff --git a/plugin/bin/claude-launcher.ps1 b/plugin/bin/claude-launcher.ps1 index db06163a..4a738dd5 100644 --- a/plugin/bin/claude-launcher.ps1 +++ b/plugin/bin/claude-launcher.ps1 @@ -7,6 +7,16 @@ param( $ErrorActionPreference = "SilentlyContinue" $LauncherDir = Split-Path -Parent $PSCommandPath +$resolver = Join-Path $LauncherDir "resolve-runtime.js" +if (-not (Test-Path $resolver)) { $resolver = Join-Path $LauncherDir "../scripts/resolve-runtime.js" } +$pluginRoot = (& node $resolver | Out-String).Trim() +if ($pluginRoot -and $env:ZH_CN_LAUNCHER_DISPATCHED -ne "1" -and (Test-Path "$pluginRoot/bin/claude-launcher.ps1")) { + $env:ZH_CN_LAUNCHER_DISPATCHED = "1" + $env:ZH_CN_LAUNCHER_BIN_DIR = $LauncherDir + & "$pluginRoot/bin/claude-launcher.ps1" @PassThruArgs + exit $LASTEXITCODE +} +if ($env:ZH_CN_LAUNCHER_BIN_DIR) { $LauncherDir = $env:ZH_CN_LAUNCHER_BIN_DIR } $oldPath = $env:PATH try { $filtered = ($env:PATH -split ';' | Where-Object { $_ -ne $LauncherDir }) -join ';' @@ -21,6 +31,13 @@ if (-not $realClaude) { exit 127 } +if ($pluginRoot -and (Test-Path "$pluginRoot/bun-binary-io.js")) { + $detected = (& node "$pluginRoot/bun-binary-io.js" detect $realClaude | Out-String).Trim() + if ($detected.StartsWith("native-bun:")) { + $stateRoot = Join-Path $env:USERPROFILE ".claude/plugins/claude-code-zh-cn" + & node "$pluginRoot/scripts/native-repair.js" $detected.Substring(11) $stateRoot | Out-Null + } +} $env:ZH_CN_REAL_CLAUDE = $realClaude $env:PATH = $filtered & $realClaude @PassThruArgs diff --git a/plugin/compute-patch-revision.sh b/plugin/compute-patch-revision.sh index 4c93c463..41da28c9 100755 --- a/plugin/compute-patch-revision.sh +++ b/plugin/compute-patch-revision.sh @@ -17,6 +17,7 @@ const files = [ "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", + "scripts/native-repair.js", ]; const hash = crypto.createHash("sha256"); diff --git a/plugin/hooks/session-start b/plugin/hooks/session-start index ece51723..9c78574b 100755 --- a/plugin/hooks/session-start +++ b/plugin/hooks/session-start @@ -176,203 +176,6 @@ process.exit(compare(latest, current) > 0 ? 0 : 1); NODE } -native_binary_version() { - local binary_path="$1" - local version output temp_home - - version="$(node "$PLUGIN_ROOT/bun-binary-io.js" version "$binary_path" 2>/dev/null || true)" - if [ -n "${version:-}" ]; then - printf '%s' "$version" - return - fi - - temp_home="$(mktemp -d "${TMPDIR:-/tmp}/cczh-version-home.XXXXXX" 2>/dev/null || true)" - if [ -n "${temp_home:-}" ]; then - output="$(HOME="$temp_home" XDG_CONFIG_HOME="$temp_home/.config" XDG_CACHE_HOME="$temp_home/.cache" XDG_DATA_HOME="$temp_home/.local/share" "$binary_path" --version 2>/dev/null || true)" - rm -rf "$temp_home" 2>/dev/null || true - else - output="$("$binary_path" --version 2>/dev/null || true)" - fi - - printf '%s' "$output" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true -} - -native_binary_version_from_execution() { - local binary_path="$1" - local output temp_home - - temp_home="$(mktemp -d "${TMPDIR:-/tmp}/cczh-version-home.XXXXXX" 2>/dev/null || true)" - if [ -n "${temp_home:-}" ]; then - output="$(HOME="$temp_home" XDG_CONFIG_HOME="$temp_home/.config" XDG_CACHE_HOME="$temp_home/.cache" XDG_DATA_HOME="$temp_home/.local/share" "$binary_path" --version 2>/dev/null || true)" - rm -rf "$temp_home" 2>/dev/null || true - else - output="$("$binary_path" --version 2>/dev/null || true)" - fi - - printf '%s' "$output" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true -} - -native_platform() { - if [ -n "${ZH_CN_NATIVE_PLATFORM:-}" ]; then - printf '%s' "$ZH_CN_NATIVE_PLATFORM" - return - fi - - local system_arch linux_arch="" - system_arch="$(uname -s 2>/dev/null)-$(uname -m 2>/dev/null)" - case "$system_arch" in - Darwin-arm64|Darwin-aarch64) - printf 'darwin-arm64' - return - ;; - Linux-x86_64|Linux-amd64) - linux_arch="x64" - ;; - Linux-arm64|Linux-aarch64) - linux_arch="arm64" - ;; - MINGW64_NT-*|MSYS_NT-*|CYGWIN_NT-*) - printf 'win32-x64' - return - ;; - *) - return - ;; - esac - - if node -e 'const r=process.report&&process.report.getReport?process.report.getReport():null;process.exit(r&&r.header&&r.header.glibcVersionRuntime?0:1)' >/dev/null 2>&1; then - printf 'linux-%s' "$linux_arch" - else - printf 'linux-%s-musl' "$linux_arch" - fi -} - -is_supported_native_version() { - local version="$1" - local platform="${2:-$(native_platform)}" - local support_file="$PLUGIN_ROOT/support-window.json" - - if [ ! -f "$support_file" ]; then - [ "$platform" = "darwin-arm64" ] || return 1 - case "${version:-}" in - 2.1.110|2.1.111|2.1.112) - return 0 - ;; - *) - return 1 - ;; - esac - fi - - node - "$support_file" "$version" "$platform" <<'NODE' -const fs = require("fs"); -const file = process.argv[2]; -const version = process.argv[3]; -const platform = process.argv[4] || ""; -const data = JSON.parse(fs.readFileSync(file, "utf8")); -const versions = []; -for (const key of [ - "macosNativeOfficialInstallerExperimental", - "macosNativeExperimental", - "linuxNativeExperimental", - "windowsNativeExperimental", -]) { - const entry = data[key]; - if (!entry) continue; - if (platform && entry.platform && entry.platform !== platform) continue; - versions.push(...(entry.versions || [])); -} -process.exit(versions.includes(version) ? 0 : 1); -NODE -} - -can_try_provisional_native_version() { - local version="$1" - local platform="${2:-$(native_platform)}" - local support_file="$PLUGIN_ROOT/support-window.json" - - if [ -z "${version:-}" ] || [ -z "${platform:-}" ] || [ ! -f "$support_file" ]; then - return 1 - fi - - node - "$support_file" "$version" "$platform" <<'NODE' -const fs = require("fs"); -const file = process.argv[2]; -const version = process.argv[3]; -const platform = process.argv[4]; -const data = JSON.parse(fs.readFileSync(file, "utf8")); - -function parse(value) { - const match = String(value || "").match(/^(\d+)\.(\d+)\.(\d+)$/); - return match ? match.slice(1).map(Number) : null; -} - -function compare(left, right) { - for (let i = 0; i < 3; i += 1) { - if (left[i] > right[i]) return 1; - if (left[i] < right[i]) return -1; - } - return 0; -} - -const candidate = parse(version); -if (!candidate) process.exit(1); - -for (const key of [ - "macosNativeOfficialInstallerExperimental", - "macosNativeExperimental", - "windowsNativeExperimental", -]) { - const entry = data[key]; - if (!entry || entry.platform !== platform) continue; - const floor = parse(entry.floor); - if (!floor) continue; - if (compare(candidate, floor) >= 0) process.exit(0); -} - -process.exit(1); -NODE -} - -replace_native_binary_from_file() { - local source_path="$1" - local binary_path="$2" - local replacement_path - - replacement_path="$(mktemp "${binary_path}.zh-cn-restore.XXXXXX" 2>/dev/null || true)" - [ -n "${replacement_path:-}" ] || return 1 - - if cp -p "$source_path" "$replacement_path" 2>/dev/null && mv -f "$replacement_path" "$binary_path" 2>/dev/null; then - return 0 - fi - - rm -f "$replacement_path" 2>/dev/null || true - return 1 -} - -native_marker_is_current() { - local marker="$1" - local version="$2" - local hash="$3" - local revision="$4" - local mode="$5" - local platform="$6" - - if [ "$mode" = "provisional" ]; then - case "$marker" in - "native|${version}|${hash}|${revision}|provisional|${platform}|"*) return 0 ;; - *) return 1 ;; - esac - fi - - [ "$marker" = "native|${version}|${hash}|${revision}" ] -} - -native_binary_hash() { - local binary_path="$1" - node "$PLUGIN_ROOT/bun-binary-io.js" hash "$binary_path" 2>/dev/null || printf "unknown" -} - read_source_repo() { if [ ! -f "$SOURCE_REPO_FILE" ]; then return @@ -723,140 +526,11 @@ fi AUTO_PATCH_MSG="" if [ -n "$NATIVE_BINARY" ]; then - # support-window 只代表“发布前完整验证过”,不再充当 runtime 开关。 - # 高于已知下限的可识别新版本走 provisional:只替换仍能精确命中的文案, - # 未知文案保留英文;repack 或 --version 自检失败则恢复干净备份。 - CURRENT_VERSION=$(native_binary_version "$NATIVE_BINARY") - NATIVE_PLATFORM=$(native_platform) - PATCH_MODE="" - if is_supported_native_version "$CURRENT_VERSION" "$NATIVE_PLATFORM"; then - PATCH_MODE="verified" - elif can_try_provisional_native_version "$CURRENT_VERSION" "$NATIVE_PLATFORM"; then - PATCH_MODE="provisional" - fi - - if [ -n "$PATCH_MODE" ]; then - PATCH_REVISION=$(compute_patch_revision "$PLUGIN_ROOT" 2>/dev/null || true) - CURRENT_HASH=$(native_binary_hash "$NATIVE_BINARY") - PATCHED_VERSION="" - [ -f "$MARKER_FILE" ] && PATCHED_VERSION=$(cat "$MARKER_FILE") - - if ! native_marker_is_current \ - "$PATCHED_VERSION" \ - "$CURRENT_VERSION" \ - "${CURRENT_HASH:-unknown}" \ - "${PATCH_REVISION:-unknown}" \ - "$PATCH_MODE" \ - "$NATIVE_PLATFORM"; then - if acquire_patch_lock "$NATIVE_BINARY"; then - # 另一个 SessionStart 可能刚完成同一文件;拿到锁后必须重新读取现场。 - CURRENT_HASH=$(native_binary_hash "$NATIVE_BINARY") - PATCHED_VERSION="" - [ -f "$MARKER_FILE" ] && PATCHED_VERSION=$(cat "$MARKER_FILE") - - if ! native_marker_is_current \ - "$PATCHED_VERSION" \ - "$CURRENT_VERSION" \ - "${CURRENT_HASH:-unknown}" \ - "${PATCH_REVISION:-unknown}" \ - "$PATCH_MODE" \ - "$NATIVE_PLATFORM"; then - if node "$PLUGIN_ROOT/bun-binary-io.js" check-deps 2>/dev/null | grep -q "ok"; then - native_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/claude-zh-cn-repatch.XXXXXX" 2>/dev/null || true)" - [ -n "$native_tmp_dir" ] || native_tmp_dir="$STATE_ROOT" - tmp_js="$native_tmp_dir/extracted.js" - status_file="$native_tmp_dir/status" - backup_path="${NATIVE_BINARY}.zh-cn-backup" - backup_version="" - if [ -f "$backup_path" ]; then - backup_version=$(native_binary_version "$backup_path") - fi - - # 同版本时恢复 backup 保证干净基底;版本变化时刷新 backup 为当前版本 - BACKUP_READY=false - if [ -f "$backup_path" ] && [ -n "${CURRENT_VERSION:-}" ] && [ "${CURRENT_VERSION:-}" = "${backup_version:-}" ]; then - if replace_native_binary_from_file "$backup_path" "$NATIVE_BINARY" 2>/dev/null; then - BACKUP_READY=true - fi - else - if cp "$NATIVE_BINARY" "$backup_path" 2>/dev/null; then - BACKUP_READY=true - fi - fi - - if [ "$BACKUP_READY" = true ]; then - SOURCE_HASH=$(native_binary_hash "$NATIVE_BINARY") - CONTAINER_LAYOUT="$(node "$PLUGIN_ROOT/bun-binary-io.js" probe "$NATIVE_BINARY" 2>/dev/null)" - if [ "$CONTAINER_LAYOUT" = "bytecode" ] || node "$PLUGIN_ROOT/bun-binary-io.js" extract "$NATIVE_BINARY" "$tmp_js" 2>/dev/null; then - if [ "$CONTAINER_LAYOUT" = "bytecode" ]; then - PATCH_STATUS="ok" - PATCH_COUNT=$(node "$PLUGIN_ROOT/scripts/patch-bytecode.js" patch "$NATIVE_BINARY" "$PLUGIN_ROOT/cli-translations.json" 2>/dev/null) || PATCH_STATUS="failed" - else - PATCH_COUNT=$("$PLUGIN_ROOT/patch-cli.sh" "$tmp_js" \ - --status "$status_file" \ - --log "$STATE_ROOT/patch.log" 2>/dev/null || echo "0") - PATCH_STATUS="$(cat "$status_file" 2>/dev/null || true)" - fi - if [ -z "$PATCH_STATUS" ]; then - # 兼容旧版/自定义 patch helper:只有数字计数时仍按 best-effort 处理。 - if [ "$PATCH_COUNT" = "0" ]; then - PATCH_STATUS="noop" - else - PATCH_STATUS="ok" - fi - fi - - case "$PATCH_STATUS" in - ok|partial|noop) - NATIVE_PATCH_OK=true - if [ "$PATCH_COUNT" != "0" ]; then - if [ "$CONTAINER_LAYOUT" = "bytecode" ] || node "$PLUGIN_ROOT/bun-binary-io.js" repack "$NATIVE_BINARY" "$tmp_js" 2>/dev/null; then - VERIFIED_VERSION="$(native_binary_version_from_execution "$NATIVE_BINARY")" - if [ "${VERIFIED_VERSION:-}" != "${CURRENT_VERSION:-}" ]; then - NATIVE_PATCH_OK=false - fi - else - NATIVE_PATCH_OK=false - fi - fi - - if [ "$NATIVE_PATCH_OK" = true ]; then - FINAL_HASH=$(native_binary_hash "$NATIVE_BINARY") - FINAL_MARKER="native|${CURRENT_VERSION}|${FINAL_HASH:-unknown}|${PATCH_REVISION:-unknown}" - if [ "$PATCH_MODE" = "provisional" ]; then - FINAL_MARKER="${FINAL_MARKER}|provisional|${NATIVE_PLATFORM}|${SOURCE_HASH:-unknown}" - fi - echo "$FINAL_MARKER" > "$MARKER_FILE" - - if [ "$PATCH_MODE" = "provisional" ]; then - AUTO_PATCH_MSG="(新版本已本机自验证,自动 patch ${PATCH_COUNT} 处;未覆盖文案继续显示英文)" - elif [ "$PATCH_STATUS" = "partial" ]; then - AUTO_PATCH_MSG="(已自动 patch ${PATCH_COUNT} 处;未覆盖文案继续显示英文)" - elif [ "$PATCH_COUNT" != "0" ]; then - AUTO_PATCH_MSG="(已自动 patch ${PATCH_COUNT} 处硬编码文字,启动自检通过)" - fi - else - if ! replace_native_binary_from_file "$backup_path" "$NATIVE_BINARY" 2>/dev/null; then - AUTO_PATCH_MSG="(自动 patch 失败且无法恢复;原始备份已保留,请重新运行安装器)" - fi - fi - ;; - *) - # patch-cli 未写回二进制;保持干净英文版本可用。 - ;; - esac - fi - fi - if [ "$native_tmp_dir" = "$STATE_ROOT" ]; then - rm -f "$tmp_js" "$status_file" - else - rm -rf "$native_tmp_dir" - fi - fi - # node-lief 不可用 → 静默跳过,保持 hook JSON 合法 - fi - release_patch_lock - fi + if [ "${ZH_CN_PRELAUNCH:-}" != "1" ]; then + if node "$PLUGIN_ROOT/scripts/native-repair.js" "$NATIVE_BINARY" "$STATE_ROOT" > /dev/null 2> "$STATE_ROOT/.native-repair-error"; then + AUTO_PATCH_MSG="" + else + AUTO_PATCH_MSG="(汉化未完成:$(cat "$STATE_ROOT/.native-repair-error"))" fi fi elif [ -f "$CLI_FILE" ]; then diff --git a/plugin/hooks/session-start.ps1 b/plugin/hooks/session-start.ps1 index 5e6a3c87..17117db6 100644 --- a/plugin/hooks/session-start.ps1 +++ b/plugin/hooks/session-start.ps1 @@ -541,7 +541,7 @@ if ($InstallInfo) { reason = "running-executable-locked" recordedAt = [DateTimeOffset]::Now.ToUnixTimeSeconds() } | ConvertTo-Json -Compress | Out-File -FilePath $NativePatchPendingFile -Encoding utf8 - $AutoPatchMsg = "(Windows 不改写正在运行的 claude.exe;本次保持原版可用。关闭所有 Claude Code 窗口后,按 https://github.com/taekchef/claude-code-zh-cn#windows-原生安装 重跑 install.ps1,即可安全补上仍能匹配的中文文案)" + $AutoPatchMsg = "(Windows 不改写正在运行的 claude.exe;本次保持原版可用。关闭占用窗口后,通过已安装的 claude 启动器再次启动,会自动验证并补汉化,无需重装)" } } else { Remove-Item $NativePatchPendingFile -Force -ErrorAction SilentlyContinue diff --git a/plugin/manifest.json b/plugin/manifest.json index f61bfcc8..44ebc1c8 100644 --- a/plugin/manifest.json +++ b/plugin/manifest.json @@ -1,6 +1,6 @@ { "name": "claude-code-zh-cn", - "version": "2.16.1", + "version": "2.17.0", "description": "Claude Code 中文本地化插件 — 简体中文支持", "author": "taekchef", "license": "MIT", diff --git a/plugin/scripts/native-repair.js b/plugin/scripts/native-repair.js new file mode 100644 index 00000000..3aeed4cd --- /dev/null +++ b/plugin/scripts/native-repair.js @@ -0,0 +1,145 @@ +#!/usr/bin/env node +"use strict"; + +// 安装器、启动器和 Hook 共用一次事务:只在副本验证成功后替换现场。 +const fs = require("node:fs"); +const path = require("node:path"); +const os = require("node:os"); +const crypto = require("node:crypto"); +const { execFileSync } = require("node:child_process"); +const root = path.resolve(__dirname, ".."); +const revisionFiles = ["patch-cli.sh", "patch-cli.js", "cli-translations.json", "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", "scripts/native-repair.js"]; +function hash(file) { + const h = crypto.createHash("sha256"), fd = fs.openSync(file, "r"), buffer = Buffer.alloc(1024 * 1024); + try { let n; while ((n = fs.readSync(fd, buffer, 0, buffer.length, null))) h.update(buffer.subarray(0, n)); } + finally { fs.closeSync(fd); } + return h.digest("hex"); +} +function revision() { + const h = crypto.createHash("sha256"); + for (const file of revisionFiles) if (fs.existsSync(path.join(root, file))) h.update(file).update("\0").update(fs.readFileSync(path.join(root, file))).update("\0"); + return h.digest("hex").slice(0, 16); +} +function readJson(file) { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } } +function platform() { + if (process.env.ZH_CN_NATIVE_PLATFORM) return process.env.ZH_CN_NATIVE_PLATFORM; + const name = `${process.platform}-${process.arch}`; + return name === "linux-x64" && !process.report.getReport().header.glibcVersionRuntime ? `${name}-musl` : name; +} +function run(file, args, env = process.env) { + return execFileSync(file, args, { encoding: "utf8", timeout: 60000, maxBuffer: 8 * 1024 * 1024, windowsHide: true, env, stdio: ["ignore", "pipe", "pipe"] }).trim(); +} +function io(...args) { return run(process.execPath, [path.join(root, "bun-binary-io.js"), ...args]); } +function atomicJson(file, value) { + const tmp = `${file}.${process.pid}.tmp`; + try { fs.writeFileSync(tmp, JSON.stringify(value) + "\n", { mode: 0o600 }); fs.renameSync(tmp, file); } + finally { fs.rmSync(tmp, { force: true }); } +} +function repair(input, stateRoot) { + const target = fs.realpathSync(input), host = platform(); + if (!["darwin-arm64", "win32-x64", "linux-x64"].includes(host)) throw new Error(`当前平台 ${host} 暂不支持 CLI Patch`); + const receiptFile = `${target}.zh-cn-repair.json`, backup = `${target}.zh-cn-backup`; + const currentHash = hash(target), patchRevision = revision(); + const pendingFile = `${receiptFile}.pending`, pending = readJson(pendingFile); + const receipt = pending?.patchedHash === currentHash ? pending : readJson(receiptFile); + fs.mkdirSync(stateRoot, { recursive: true }); + function marker(result) { + const support = readJson(path.join(root, "support-window.json")); + const verified = Object.values(support || {}).some(e => e.platform === host && e.versions?.includes(result.version)); + const value = `native|${result.version}|${result.patchedHash}|${patchRevision}` + (verified ? "" : `|provisional|${host}|${result.sourceHash}`); + fs.writeFileSync(path.join(stateRoot, ".patched-version"), value + "\n"); + return { ...result, mode: verified ? "verified" : "provisional" }; + } + if (receipt?.patchedHash === currentHash && receipt.revision === patchRevision) return marker({ ...receipt, changed: false }); + const lock = `${target}.zh-cn-lock`; + const deadline = Date.now() + 30000; + for (;;) { + try { fs.mkdirSync(lock); break; } catch (error) { + if (error.code !== "EEXIST") throw error; + let owner = ""; + try { owner = fs.readFileSync(path.join(lock, "pid"), "utf8"); } catch {} + const pid = Number(owner.split(":")[0]); + let dead = false; + if (Number.isSafeInteger(pid) && pid > 0) { + try { process.kill(pid, 0); } catch (e) { dead = e.code === "ESRCH"; } + } + if (dead) { + // 清理者也互斥,且取得清理权后重新核对 owner,避免删掉新持有者的锁。 + const reaping = path.join(lock, "reaping"); + try { + fs.mkdirSync(reaping); + try { + if (fs.readFileSync(path.join(lock, "pid"), "utf8") === owner) fs.rmSync(lock, { recursive: true }); + } finally { fs.rmSync(reaping, { recursive: true, force: true }); } + } catch (e) { if (!["EEXIST", "ENOENT"].includes(e.code)) throw e; } + } + if (!fs.existsSync(lock)) return repair(input, stateRoot); + if (Date.now() >= deadline) throw new Error(`另一个汉化进程占用修复锁:${lock};本次未改动程序`); + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100); + } + } + let work; + try { + fs.writeFileSync(path.join(lock, "pid"), `${process.pid}:${crypto.randomUUID()}`); + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在检查期间已更新,请再次启动"); + // 只有文件指纹和备份指纹同时匹配,才从备份重新应用新规则。 + const source = receipt?.patchedHash === currentHash && fs.existsSync(backup) && receipt.sourceHash === hash(backup) ? backup : target; + const sourceHash = hash(source); + work = fs.mkdtempSync(path.join(path.dirname(target), ".zh-cn-prelaunch-")); + const candidate = path.join(work, host === "win32-x64" ? "claude.exe" : "claude"); + const pristine = path.join(work, "source"); + fs.copyFileSync(source, pristine); + if (hash(pristine) !== sourceHash) throw new Error("CC 在复制期间已更新,请再次启动"); + fs.copyFileSync(pristine, candidate); + fs.chmodSync(candidate, fs.statSync(target).mode); + const version = io("version", candidate); + if (!/^\d+\.\d+\.\d+$/.test(version)) throw new Error("无法识别 CC 版本,未改动程序"); + const layout = io("probe", candidate); + let patched; + if (layout === "bytecode") { + patched = Number(run(process.execPath, [path.join(root, "scripts/patch-bytecode.js"), "patch", candidate, path.join(root, "cli-translations.json")])); + } else if (layout === "source-js") { + const js = path.join(work, "cli.js"); + io("extract", candidate, js); + patched = Number(process.platform === "win32" + ? run(process.execPath, [path.join(root, "patch-cli.js"), js, path.join(root, "cli-translations.json")]) + : run("bash", [path.join(root, "patch-cli.sh"), js, "--status", path.join(work, "patch-status")])); + if (patched > 0) io("repack", candidate, js); + } else throw new Error("新版程序结构尚未适配,未改动程序"); + if (!Number.isFinite(patched) || patched <= 0) throw new Error("没有命中可验证的译文,未改动程序"); + const isolatedHome = path.join(work, "home"); + fs.mkdirSync(isolatedHome); + const env = { ...process.env, HOME: isolatedHome, USERPROFILE: isolatedHome, CLAUDE_CONFIG_DIR: path.join(isolatedHome, ".claude"), DISABLE_AUTOUPDATER: "1", CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", ZH_CN_PRELAUNCH: "1" }; + if (run(candidate, ["--version"], env).match(/\d+\.\d+\.\d+/)?.[0] !== version) throw new Error("汉化副本启动自检失败,未改动程序"); + if (!/[\u3400-\u9fff]/u.test(run(candidate, ["--help"], env))) throw new Error("汉化副本帮助界面未出现中文,未改动程序"); + const result = { version, sourceHash, patchedHash: hash(candidate), revision: patchRevision, patched, changed: true }; + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在验证期间已更新,请再次启动"); + if (source === target) { + const saved = path.join(work, "backup"); + fs.copyFileSync(pristine, saved); + fs.renameSync(saved, backup); + } + atomicJson(pendingFile, result); + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在提交前已更新,请再次启动"); + // Windows 的文件占用错误由系统返回;绝不 unlink 正在运行的程序。 + try { + if (process.platform === "win32") require("../bun-binary-io.js").withWindowsFileRetry(() => fs.renameSync(candidate, target)); + else fs.renameSync(candidate, target); + } catch (error) { + throw new Error(`无法替换 CC 程序(${error.code});请关闭占用它的窗口后再次启动,无需重装插件`); + } + atomicJson(receiptFile, result); + fs.rmSync(pendingFile, { force: true }); + return marker(result); + } finally { + if (work) fs.rmSync(work, { recursive: true, force: true }); + fs.rmSync(lock, { recursive: true, force: true }); + } +} +module.exports = { repair, hash, revision, platform }; +if (require.main === module) { + try { + const result = repair(process.argv[2], process.argv[3] || process.env.CLAUDE_PLUGIN_DATA || root); + process.stdout.write(JSON.stringify(result) + "\n"); + } catch (error) { process.stderr.write(`汉化未完成:${error.message}\n`); process.exitCode = 1; } +} diff --git a/plugin/scripts/patch-bytecode.js b/plugin/scripts/patch-bytecode.js index e5c60708..2cd2c5b8 100644 --- a/plugin/scripts/patch-bytecode.js +++ b/plugin/scripts/patch-bytecode.js @@ -200,6 +200,19 @@ function patchBinary(binaryPath, translations, { dryRun = false } = {}) { function restoreBinary(binaryPath) { binaryPath = fs.realpathSync(binaryPath); const backup = binaryPath + ".zh-cn-backup"; + const receiptPath = binaryPath + ".zh-cn-repair.json"; + if (fs.existsSync(receiptPath)) { + const receipt = JSON.parse(fs.readFileSync(receiptPath, "utf8")); + const { hash } = require("./native-repair.js"); + if (hash(backup) !== receipt.sourceHash) throw new Error("备份指纹不符,未还原文件;备份已保留"); + if (hash(binaryPath) !== receipt.patchedHash) { + // CC 已被上游重新安装或升级,不能用同版本旧备份覆盖它。 + fs.unlinkSync(backup); + fs.unlinkSync(receiptPath); + fs.rmSync(receiptPath + ".pending", { force: true }); + return { restored: false, reason: "current-file-changed", preservedCurrent: true }; + } + } const version = io.readExecutableVersion(binaryPath); if (!version || io.readExecutableVersion(backup) !== version) { throw new Error("备份与当前程序版本不一致或无法启动,未还原文件;备份已保留"); @@ -210,6 +223,8 @@ function restoreBinary(binaryPath) { fs.copyFileSync(backup, candidate); io.withWindowsFileRetry(() => fs.renameSync(candidate, binaryPath)); io.withWindowsFileRetry(() => fs.unlinkSync(backup)); + fs.rmSync(binaryPath + ".zh-cn-repair.json", { force: true }); + fs.rmSync(binaryPath + ".zh-cn-repair.json.pending", { force: true }); return { restored: true, version }; } finally { io.withWindowsFileRetry(() => fs.rmSync(tempDir, { recursive: true, force: true })); diff --git a/plugin/scripts/resolve-runtime.js b/plugin/scripts/resolve-runtime.js new file mode 100644 index 00000000..aa1e6f5d --- /dev/null +++ b/plugin/scripts/resolve-runtime.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; +const fs = require("node:fs"); +const path = require("node:path"); +const os = require("node:os"); +function read(file) { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; } } +function resolveRuntime() { + const config = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude"); + const id = "claude-code-zh-cn@claude-code-zh-cn"; + const legacy = path.join(config, "plugins/claude-code-zh-cn"); + if (read(path.join(config, "settings.json")).enabledPlugins?.[id] === false) { + return fs.existsSync(path.join(legacy, ".official-fallback-disabled")) ? (process.env.CLAUDE_PLUGIN_ROOT || legacy) : ""; + } + if (process.env.CLAUDE_PLUGIN_ROOT) return process.env.CLAUDE_PLUGIN_ROOT; + const entries = read(path.join(config, "plugins/installed_plugins.json")).plugins?.[id] || []; + const installed = entries.find(e => e.scope === "user"); + // 正式安装记录是唯一选择,不扫描缓存目录猜测“最新版本”。 + return installed ? installed.installPath : path.join(config, "plugins/claude-code-zh-cn"); +} +module.exports = { resolveRuntime }; +if (require.main === module) process.stdout.write(resolveRuntime() || ""); diff --git a/plugin/scripts/zh-cn-doctor.js b/plugin/scripts/zh-cn-doctor.js index e7554cec..6bd0ea7a 100644 --- a/plugin/scripts/zh-cn-doctor.js +++ b/plugin/scripts/zh-cn-doctor.js @@ -11,7 +11,7 @@ const STABLE_INSTALL_CMD = `npm install -g @anthropic-ai/claude-code@${STABLE_PI const CLAUDE_UPDATER_BOUNDARY = "DISABLE_AUTOUPDATER 归 Claude Code 本体;本插件只更新中文插件和重 patch,不能保证阻止本体升级。请以 claude doctor 的 Updates 段为准。"; const UNPUBLISHED_WINDOW_GUIDANCE = - "升到未发布支持窗口时,先看 docs/support-matrix.md;未收录就等插件 Release,或临时退回已验证版本"; + "发布验证见 docs/support-matrix.md;未收录版本通过启动器先做本机验证,不等于发布验证或完整中文覆盖"; const PATCH_REVISION_FILES = [ "patch-cli.sh", "patch-cli.js", @@ -19,6 +19,7 @@ const PATCH_REVISION_FILES = [ "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", + "scripts/native-repair.js", ]; const NPM_RESIDUE_PROBES = [ "Quick safety check", diff --git a/plugin/support-window.json b/plugin/support-window.json index 21136e96..4a9e299d 100644 --- a/plugin/support-window.json +++ b/plugin/support-window.json @@ -207,7 +207,7 @@ "requires": [ "node-lief >=1.3.0" ], - "allowProvisional": false + "allowProvisional": true }, "windowsNativeExperimental": { "floor": "2.1.113", diff --git a/scripts/check-payload-sources.js b/scripts/check-payload-sources.js index 5f334d1f..cda41ecd 100644 --- a/scripts/check-payload-sources.js +++ b/scripts/check-payload-sources.js @@ -20,6 +20,7 @@ const payloadPairs = [ { source: "doctor.ps1", mirror: "plugin/bin/doctor.ps1" }, { source: "scripts/zh-cn-doctor.js", mirror: "plugin/scripts/zh-cn-doctor.js" }, { source: "scripts/patch-bytecode.js", mirror: "plugin/scripts/patch-bytecode.js" }, + { source: "scripts/native-repair.js", mirror: "plugin/scripts/native-repair.js" }, ]; function fail(message) { diff --git a/scripts/check-support-boundary.js b/scripts/check-support-boundary.js index 998dfcc7..6423d9ab 100644 --- a/scripts/check-support-boundary.js +++ b/scripts/check-support-boundary.js @@ -183,9 +183,10 @@ function findLinuxNativeClaim(line) { } if (isAllowedLinuxNativeExperimentalLine(line)) return null; + if (/Linux x64 glibc 新版先本机自检/.test(line)) return null; if (!isSupportMatrixScopedLine(line)) { - return "Linux native 仅允许明确验证的 x64 glibc 版本,不能宣称 arm64 / musl / provisional latest"; + return "Linux native 必须限定 x64 glibc,不得宣称 arm64 / musl 或未来版本全量支持"; } return null; @@ -282,7 +283,7 @@ function addSupportEntryFindings(findings, node, relative, pathParts, boundary) [node.libc === "glibc", "linuxNativeExperimental libc 必须是 glibc"], [node.packageName === "@anthropic-ai/claude-code-linux-x64", "linuxNativeExperimental packageName 必须是 @anthropic-ai/claude-code-linux-x64"], [Array.isArray(node.requires) && node.requires.includes("node-lief >=1.3.0"), "linuxNativeExperimental requires 必须包含 node-lief >=1.3.0"], - [node.allowProvisional === false, "linuxNativeExperimental 必须禁用 provisional"], + [node.allowProvisional === true, "linuxNativeExperimental 必须允许同平台本机验证"], ]) { if (!valid) { findings.push({ file: relative, line: 1, message, text: `${entryPath}: ${JSON.stringify(node)}` }); @@ -368,7 +369,7 @@ function printOk(boundary) { console.log(`support-boundary-guard: OK`); console.log(`stable CLI Patch: ${boundary.stableRange}`); console.log(`native CLI Patch: only explicitly verified macOS / Windows / Linux experimental versions; no latest stable claim`); - console.log(`Linux native CLI Patch: verified x64 glibc versions only; no provisional latest`); + console.log(`Linux native CLI Patch: x64 glibc local validation; no arm64 or musl`); } function printFail(findings, boundary) { @@ -377,7 +378,7 @@ function printFail(findings, boundary) { console.log(`- stable CLI Patch: ${boundary.stableRange}`); console.log(`- ${boundary.nativeBoundary}+ / latest: 不能写成 stable;native 只能写已验证 experimental 窗口`); console.log("- Windows native 只能写成 explicit experimental,不能写成 stable"); - console.log("- Linux native 仅发布已验证的 x64 glibc 版本,不含 arm64 / musl / provisional latest"); + console.log("- Linux native 发布清单只记录已验证的 x64 glibc 版本;未收录版本须通过本机验证,不含 arm64 / musl"); console.log(""); for (const finding of findings) { diff --git a/scripts/generate-support-matrix.js b/scripts/generate-support-matrix.js index eb9d9c85..e61230f1 100644 --- a/scripts/generate-support-matrix.js +++ b/scripts/generate-support-matrix.js @@ -186,8 +186,8 @@ function buildMarkdown(config, compat) { "", "- 验证等级只表示公开证据和翻译覆盖程度,不是运行门禁。", "- `stable`:代表版本段已通过 compat matrix,且 npm 路径具备启动前自修复。", - "- `experimental`:已有 native 运行和显示面证据;更高可识别版本也会先本机自检,已知文案继续中文,未知文案保留英文(仅适用于 macOS / Windows)。", - "- Linux 仅启用已发布的 x64 glibc 版本,不尝试 provisional latest;arm64 与 musl 不在支持窗口内。", + "- `experimental`:已有 native 运行和显示面证据;更高可识别版本也会先本机自检,已知文案继续中文,未知文案保留英文(平台范围见下表)。", + "- Linux x64 glibc 未收录版本也可启动前本机验证(provisional);不支持 arm64、musl,不代表未来版本全量兼容。", "- `unsupported`:该平台或二进制格式不执行原生 Layer 4;正式插件的 Layer 1~3 继续可用。", "", "## Current Support", diff --git a/scripts/install-json-helper.js b/scripts/install-json-helper.js index fd4350c2..91c85365 100644 --- a/scripts/install-json-helper.js +++ b/scripts/install-json-helper.js @@ -15,6 +15,7 @@ const PATCH_REVISION_FILES = [ "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", + "scripts/native-repair.js", ]; function readJson(file) { diff --git a/scripts/native-repair.js b/scripts/native-repair.js new file mode 100644 index 00000000..3aeed4cd --- /dev/null +++ b/scripts/native-repair.js @@ -0,0 +1,145 @@ +#!/usr/bin/env node +"use strict"; + +// 安装器、启动器和 Hook 共用一次事务:只在副本验证成功后替换现场。 +const fs = require("node:fs"); +const path = require("node:path"); +const os = require("node:os"); +const crypto = require("node:crypto"); +const { execFileSync } = require("node:child_process"); +const root = path.resolve(__dirname, ".."); +const revisionFiles = ["patch-cli.sh", "patch-cli.js", "cli-translations.json", "bun-binary-io.js", "compute-patch-revision.sh", "scripts/patch-bytecode.js", "scripts/native-repair.js"]; +function hash(file) { + const h = crypto.createHash("sha256"), fd = fs.openSync(file, "r"), buffer = Buffer.alloc(1024 * 1024); + try { let n; while ((n = fs.readSync(fd, buffer, 0, buffer.length, null))) h.update(buffer.subarray(0, n)); } + finally { fs.closeSync(fd); } + return h.digest("hex"); +} +function revision() { + const h = crypto.createHash("sha256"); + for (const file of revisionFiles) if (fs.existsSync(path.join(root, file))) h.update(file).update("\0").update(fs.readFileSync(path.join(root, file))).update("\0"); + return h.digest("hex").slice(0, 16); +} +function readJson(file) { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } } +function platform() { + if (process.env.ZH_CN_NATIVE_PLATFORM) return process.env.ZH_CN_NATIVE_PLATFORM; + const name = `${process.platform}-${process.arch}`; + return name === "linux-x64" && !process.report.getReport().header.glibcVersionRuntime ? `${name}-musl` : name; +} +function run(file, args, env = process.env) { + return execFileSync(file, args, { encoding: "utf8", timeout: 60000, maxBuffer: 8 * 1024 * 1024, windowsHide: true, env, stdio: ["ignore", "pipe", "pipe"] }).trim(); +} +function io(...args) { return run(process.execPath, [path.join(root, "bun-binary-io.js"), ...args]); } +function atomicJson(file, value) { + const tmp = `${file}.${process.pid}.tmp`; + try { fs.writeFileSync(tmp, JSON.stringify(value) + "\n", { mode: 0o600 }); fs.renameSync(tmp, file); } + finally { fs.rmSync(tmp, { force: true }); } +} +function repair(input, stateRoot) { + const target = fs.realpathSync(input), host = platform(); + if (!["darwin-arm64", "win32-x64", "linux-x64"].includes(host)) throw new Error(`当前平台 ${host} 暂不支持 CLI Patch`); + const receiptFile = `${target}.zh-cn-repair.json`, backup = `${target}.zh-cn-backup`; + const currentHash = hash(target), patchRevision = revision(); + const pendingFile = `${receiptFile}.pending`, pending = readJson(pendingFile); + const receipt = pending?.patchedHash === currentHash ? pending : readJson(receiptFile); + fs.mkdirSync(stateRoot, { recursive: true }); + function marker(result) { + const support = readJson(path.join(root, "support-window.json")); + const verified = Object.values(support || {}).some(e => e.platform === host && e.versions?.includes(result.version)); + const value = `native|${result.version}|${result.patchedHash}|${patchRevision}` + (verified ? "" : `|provisional|${host}|${result.sourceHash}`); + fs.writeFileSync(path.join(stateRoot, ".patched-version"), value + "\n"); + return { ...result, mode: verified ? "verified" : "provisional" }; + } + if (receipt?.patchedHash === currentHash && receipt.revision === patchRevision) return marker({ ...receipt, changed: false }); + const lock = `${target}.zh-cn-lock`; + const deadline = Date.now() + 30000; + for (;;) { + try { fs.mkdirSync(lock); break; } catch (error) { + if (error.code !== "EEXIST") throw error; + let owner = ""; + try { owner = fs.readFileSync(path.join(lock, "pid"), "utf8"); } catch {} + const pid = Number(owner.split(":")[0]); + let dead = false; + if (Number.isSafeInteger(pid) && pid > 0) { + try { process.kill(pid, 0); } catch (e) { dead = e.code === "ESRCH"; } + } + if (dead) { + // 清理者也互斥,且取得清理权后重新核对 owner,避免删掉新持有者的锁。 + const reaping = path.join(lock, "reaping"); + try { + fs.mkdirSync(reaping); + try { + if (fs.readFileSync(path.join(lock, "pid"), "utf8") === owner) fs.rmSync(lock, { recursive: true }); + } finally { fs.rmSync(reaping, { recursive: true, force: true }); } + } catch (e) { if (!["EEXIST", "ENOENT"].includes(e.code)) throw e; } + } + if (!fs.existsSync(lock)) return repair(input, stateRoot); + if (Date.now() >= deadline) throw new Error(`另一个汉化进程占用修复锁:${lock};本次未改动程序`); + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100); + } + } + let work; + try { + fs.writeFileSync(path.join(lock, "pid"), `${process.pid}:${crypto.randomUUID()}`); + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在检查期间已更新,请再次启动"); + // 只有文件指纹和备份指纹同时匹配,才从备份重新应用新规则。 + const source = receipt?.patchedHash === currentHash && fs.existsSync(backup) && receipt.sourceHash === hash(backup) ? backup : target; + const sourceHash = hash(source); + work = fs.mkdtempSync(path.join(path.dirname(target), ".zh-cn-prelaunch-")); + const candidate = path.join(work, host === "win32-x64" ? "claude.exe" : "claude"); + const pristine = path.join(work, "source"); + fs.copyFileSync(source, pristine); + if (hash(pristine) !== sourceHash) throw new Error("CC 在复制期间已更新,请再次启动"); + fs.copyFileSync(pristine, candidate); + fs.chmodSync(candidate, fs.statSync(target).mode); + const version = io("version", candidate); + if (!/^\d+\.\d+\.\d+$/.test(version)) throw new Error("无法识别 CC 版本,未改动程序"); + const layout = io("probe", candidate); + let patched; + if (layout === "bytecode") { + patched = Number(run(process.execPath, [path.join(root, "scripts/patch-bytecode.js"), "patch", candidate, path.join(root, "cli-translations.json")])); + } else if (layout === "source-js") { + const js = path.join(work, "cli.js"); + io("extract", candidate, js); + patched = Number(process.platform === "win32" + ? run(process.execPath, [path.join(root, "patch-cli.js"), js, path.join(root, "cli-translations.json")]) + : run("bash", [path.join(root, "patch-cli.sh"), js, "--status", path.join(work, "patch-status")])); + if (patched > 0) io("repack", candidate, js); + } else throw new Error("新版程序结构尚未适配,未改动程序"); + if (!Number.isFinite(patched) || patched <= 0) throw new Error("没有命中可验证的译文,未改动程序"); + const isolatedHome = path.join(work, "home"); + fs.mkdirSync(isolatedHome); + const env = { ...process.env, HOME: isolatedHome, USERPROFILE: isolatedHome, CLAUDE_CONFIG_DIR: path.join(isolatedHome, ".claude"), DISABLE_AUTOUPDATER: "1", CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", ZH_CN_PRELAUNCH: "1" }; + if (run(candidate, ["--version"], env).match(/\d+\.\d+\.\d+/)?.[0] !== version) throw new Error("汉化副本启动自检失败,未改动程序"); + if (!/[\u3400-\u9fff]/u.test(run(candidate, ["--help"], env))) throw new Error("汉化副本帮助界面未出现中文,未改动程序"); + const result = { version, sourceHash, patchedHash: hash(candidate), revision: patchRevision, patched, changed: true }; + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在验证期间已更新,请再次启动"); + if (source === target) { + const saved = path.join(work, "backup"); + fs.copyFileSync(pristine, saved); + fs.renameSync(saved, backup); + } + atomicJson(pendingFile, result); + if (hash(target) !== currentHash || fs.realpathSync(input) !== target) throw new Error("CC 在提交前已更新,请再次启动"); + // Windows 的文件占用错误由系统返回;绝不 unlink 正在运行的程序。 + try { + if (process.platform === "win32") require("../bun-binary-io.js").withWindowsFileRetry(() => fs.renameSync(candidate, target)); + else fs.renameSync(candidate, target); + } catch (error) { + throw new Error(`无法替换 CC 程序(${error.code});请关闭占用它的窗口后再次启动,无需重装插件`); + } + atomicJson(receiptFile, result); + fs.rmSync(pendingFile, { force: true }); + return marker(result); + } finally { + if (work) fs.rmSync(work, { recursive: true, force: true }); + fs.rmSync(lock, { recursive: true, force: true }); + } +} +module.exports = { repair, hash, revision, platform }; +if (require.main === module) { + try { + const result = repair(process.argv[2], process.argv[3] || process.env.CLAUDE_PLUGIN_DATA || root); + process.stdout.write(JSON.stringify(result) + "\n"); + } catch (error) { process.stderr.write(`汉化未完成:${error.message}\n`); process.exitCode = 1; } +} diff --git a/scripts/patch-bytecode.js b/scripts/patch-bytecode.js index e5c60708..2cd2c5b8 100644 --- a/scripts/patch-bytecode.js +++ b/scripts/patch-bytecode.js @@ -200,6 +200,19 @@ function patchBinary(binaryPath, translations, { dryRun = false } = {}) { function restoreBinary(binaryPath) { binaryPath = fs.realpathSync(binaryPath); const backup = binaryPath + ".zh-cn-backup"; + const receiptPath = binaryPath + ".zh-cn-repair.json"; + if (fs.existsSync(receiptPath)) { + const receipt = JSON.parse(fs.readFileSync(receiptPath, "utf8")); + const { hash } = require("./native-repair.js"); + if (hash(backup) !== receipt.sourceHash) throw new Error("备份指纹不符,未还原文件;备份已保留"); + if (hash(binaryPath) !== receipt.patchedHash) { + // CC 已被上游重新安装或升级,不能用同版本旧备份覆盖它。 + fs.unlinkSync(backup); + fs.unlinkSync(receiptPath); + fs.rmSync(receiptPath + ".pending", { force: true }); + return { restored: false, reason: "current-file-changed", preservedCurrent: true }; + } + } const version = io.readExecutableVersion(binaryPath); if (!version || io.readExecutableVersion(backup) !== version) { throw new Error("备份与当前程序版本不一致或无法启动,未还原文件;备份已保留"); @@ -210,6 +223,8 @@ function restoreBinary(binaryPath) { fs.copyFileSync(backup, candidate); io.withWindowsFileRetry(() => fs.renameSync(candidate, binaryPath)); io.withWindowsFileRetry(() => fs.unlinkSync(backup)); + fs.rmSync(binaryPath + ".zh-cn-repair.json", { force: true }); + fs.rmSync(binaryPath + ".zh-cn-repair.json.pending", { force: true }); return { restored: true, version }; } finally { io.withWindowsFileRetry(() => fs.rmSync(tempDir, { recursive: true, force: true })); diff --git a/scripts/preflight.sh b/scripts/preflight.sh index 3b51fdf7..45e6e4f9 100755 --- a/scripts/preflight.sh +++ b/scripts/preflight.sh @@ -157,7 +157,7 @@ run node scripts/generate-plugin-support-window.js --write run git diff --exit-code plugin/support-window.json step "Run tests" -run node --test tests/*.test.js +run node --test --test-concurrency=4 tests/*.test.js if [ "$RUN_RELEASE_STATE" -eq 1 ]; then step "Verify release state" diff --git a/scripts/prepare-native-release-closeout.js b/scripts/prepare-native-release-closeout.js index af48d5e9..72f65317 100644 --- a/scripts/prepare-native-release-closeout.js +++ b/scripts/prepare-native-release-closeout.js @@ -42,7 +42,7 @@ function usage() { "Usage: node scripts/prepare-native-release-closeout.js --native-version