Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .crabcode-plugin/mirror-distribution.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"comment": "镜像分发裁剪清单(W-PLUGIN-MIRROR-SLIM, 2026-08-16)。列出的目录前缀不进 CN 镜像 zip(客户端运行时零消费的测试资产;git clone 回退路径仍是全仓)。发布器 acosmi/crabcode-source .github/workflows/sync-plugins-to-mirror.yml 与本仓审计器 .github/workflows/notify-mirror.yml 从同一 SHA 的 checkout 读本文件推导各自的文件集,两侧构造上不可能分叉。安全栅:前缀必须含 /tests/(两侧都强制),清单永远裁不掉运行时内容。改本文件无需改任何 workflow。",
"excludePrefixes": [
"plugins/crabcode-media-ops/tests/browser/__screenshots__/",
"plugins/crabcode-media-publisher/tests/browser/snapshots/"
]
}
36 changes: 34 additions & 2 deletions .github/workflows/notify-mirror.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,36 @@ jobs:
unzip -tq "${WORK}/${SHA}.zip" >/dev/null \
|| fail "mirror archive ${SHA}.zip failed CRC validation"
python3 - "${WORK}/${SHA}.zip" expected-source "${ARCHIVE_PREFIX}" <<'PY'
import pathlib, stat, sys, zipfile
import json, pathlib, stat, sys, zipfile

archive = pathlib.Path(sys.argv[1])
source = pathlib.Path(sys.argv[2])
prefix = sys.argv[3].rstrip("/") + "/"

# Mirror distribution trim manifest (W-PLUGIN-MIRROR-SLIM, 2026-08-16).
# The publisher (acosmi/crabcode-source sync-plugins-to-mirror.yml) reads
# the SAME file from the SAME commit it packs; we read it from the exact
# checkout of the served SHA, so both sides derive their file sets from
# one source and cannot diverge — the opposite of the one-sided contract
# upgrades that broke this audit three times before. Absent manifest =
# no exclusions = the legacy full-tree contract, byte for byte.
# Safety rail (mirrored on the publisher side): every exclude prefix must
# contain "/tests/", so the manifest can only ever carve out test assets,
# never runtime content (plugin manifests / dist / skills).
manifest_path = source / ".crabcode-plugin" / "mirror-distribution.json"
exclude_prefixes = []
if manifest_path.exists():
manifest = json.load(manifest_path.open(encoding="utf-8"))
exclude_prefixes = manifest.get("excludePrefixes", [])
if not isinstance(exclude_prefixes, list) or not all(
isinstance(p, str) for p in exclude_prefixes
):
raise SystemExit("mirror-distribution excludePrefixes must be a string array")
for p in exclude_prefixes:
if "/tests/" not in p:
raise SystemExit(f"mirror-distribution exclude prefix must stay under /tests/: {p}")
if p.startswith("/") or "\\" in p or ".." in p.split("/"):
raise SystemExit(f"mirror-distribution exclude prefix must be repo-relative POSIX: {p}")
with zipfile.ZipFile(archive) as bundle:
all_infos = bundle.infolist()
if not all_infos or len({item.filename for item in all_infos}) != len(all_infos):
Expand All @@ -246,16 +271,23 @@ jobs:
for info in all_infos if not info.is_dir()
}
local = {}
excluded_count = 0
for path in source.rglob("*"):
if path.is_symlink():
raise SystemExit(f"release source contains unsupported symlink: {path}")
if not path.is_file() or ".git" in path.parts:
continue
local[path.relative_to(source).as_posix()] = path
rel = path.relative_to(source).as_posix()
if any(rel.startswith(p) for p in exclude_prefixes):
excluded_count += 1
continue
local[rel] = path
if set(local) != set(archived):
missing = sorted(set(local) - set(archived))[:20]
extra = sorted(set(archived) - set(local))[:20]
raise SystemExit(f"archive file set mismatch; missing={missing} extra={extra}")
if exclude_prefixes:
print(f"mirror-distribution trim: {excluded_count} files excluded from the expected set")
for name, path in local.items():
info = archived[name]
if path.read_bytes() != bundle.read(info):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 2026-08-16 镜像瘦身清单 + 摘要校验闭环 —— 处置实况

> 接同日《CN 镜像三缺陷修复与插件安装预算倒挂-处置实况》(PR #12 分支)。那轮把镜像修
> 新鲜后 zip 从 10.3MB 涨到 15.1MB(+46.5%),本轮把「涨回去的体积」与「发了没人验的
> 校验和」两条尾巴一并关掉。

## 一、镜像 zip 瘦身:排除清单三方同源(W-PLUGIN-MIRROR-SLIM)

### 实测(2026-08-16,本仓 main=988b228 的字节)

| 项 | 值 |
|---|---|
| 仓库全部 blob(未压缩) | 30.12 MB |
| 浏览器视觉回归 PNG 快照 | **40 个文件 / 6.76 MB**(PNG 不可再压,≈ 压缩包同量) |
| 其中 media-ops `tests/browser/__screenshots__/` | 10 个 |
| 其中 media-publisher `tests/browser/snapshots/` | 30 个 |
| 运行时 icon.png(**保留**) | 17 个 / 0.03 MB |
| 快照目录内非 PNG 文件 | 0(目录前缀排除 = 精确命中) |

客户端对这 40 个文件**运行时零消费**(安装即解压到 marketplace 缓存,manifest/dist/
skills 之外的 tests 目录无人读)。裁掉后镜像 zip 约 15.1MB → **约 8.3MB**,对老版本
客户端(30s 外墙未修)是立即生效的缓解 —— 不需要发版。

### 机制:清单只写一份,两侧从同一个 SHA 读

- 清单 = 本仓 `.crabcode-plugin/mirror-distribution.json` 的 `excludePrefixes`
- **发布器**(acosmi/crabcode-source `sync-plugins-to-mirror.yml`)在 staging 后按
清单删除命中文件再打 zip;**清单不存在 = 不裁剪**,与旧行为逐字节一致
- **审计器**(本仓 `notify-mirror.yml`)从**被审 SHA 的 checkout**读同一份清单,
过滤期望文件集后做全集相等比对

为什么是这个形状:上一轮的三个缺陷全是「审计器判据加严、发布器没跟」的契约单边升级
(同形三连)。把排除表写两份 = 给下一次单边升级留门;写一份、两侧同 SHA 读取,分叉在
构造上不可能。审计器 checkout 的就是 latest 指针指向的那个 SHA,所以「发布器打包时看
到的清单」与「审计器核对时看到的清单」永远是同一个字节。

### 安全栅(两侧同款,防清单变成足枪)

每个排除前缀必须含 `/tests/`,且必须是仓内相对 POSIX 路径 —— 清单**构造上**裁不掉
运行时内容(插件 manifest / dist / skills / commands)。违规两侧都当场 fail。

### 上线顺序为什么安全(可乱序合并)

| 状态 | 发布器 | 审计器 | 结果 |
|---|---|---|---|
| 现在(清单未合) | 新(读清单,不存在→no-op) | 旧或新 | 全集比对,绿 |
| 本 PR 合并、未再 sync | 新 | 新(从**旧指针 SHA** checkout,无清单→不过滤) | 镜像还是旧全量包,绿 |
| 合并后 sync | 新(裁剪) | 新(同 SHA 清单过滤) | 绿,包瘦身生效 |

唯一会红的组合「旧审计器 × 裁剪包」在时间线上不可达:清单与审计器改动在同一个
commit 落 main,任何含清单的指针 SHA 必然伴随新审计器(schedule/dispatch 都从
default branch 取 workflow 定义)。

### 本地验证矩阵(六箭头全过,harness 复刻两侧 heredoc 逻辑)

```
✓ A 新发布器+新审计器(有清单) → PASS [恰好 trim 40 files / 6.76 MB]
✓ B 新发布器+旧审计器 → 必须红,实测红(missing=40 个快照) ⇒ 审计器改动承重
✓ C 无清单+旧审计器 → PASS(trim no-op,旧行为)
✓ C' 无清单+新审计器 → PASS
✓ D 发布器安全栅(非 /tests/ 前缀) → 当场拒
✓ D' 审计器安全栅(同前缀) → 当场拒
```

负向箭头 B 是关键:没有它,「审计器改动是否真的在承重」只是推断(CLAUDE.md §0:
期望通过类断言必须配能证明判据有区分力的反例)。

### 负结果存档(上一轮已测,防下一个人重走)

workflow 注释曾设想「裁剪 src/tests 减小体积」:**数据上不成立**,`plugins/` 占压缩后
95%,裁 src/tests 只省约 4%。真正的大头就是本轮这 40 个快照。

## 二、摘要校验闭环(客户端半边,主仓 acosmi/crabcode-source)

上一轮发布侧补齐了 `<sha>.zip.sha256` / `marketplace.json.sha256`(审计器 fail-closed
强制),但客户端一个字节都没验 —— 校验和「发布是主张,被验证才是履行」。本轮客户端
(`src/utils/plugins/officialMarketplaceGcs.ts`)下载 zip 后取 sidecar 对比:

- **合规但摘要不等 → fail-closed**(丢弃下载,回退 git;遥测桶 `digest_mismatch`)
- sidecar 取不到 / 形态非法 → **fail-open 继续用**(遥测 `unavailable`/`malformed`)——
sidecar 是新文件,CDN 负缓存完全可能只打掉它而 zip 无恙(参见 nginx 404-immutable
事故);唯一携带「内容被换过」强信号的是合规但不等
- 记录形态三方共同契约:发布器 `printf '%s %s\n'` 产出、审计器正则强制、客户端用
同一正则解析(`<sha256>␠␠<basename>`,两空格)
- 新增的 10s sidecar 预算已并入跨语言预算闸门
`tests/unit/marketplace-install-budget-inversion.test.ts`(内层三笔求和 + 余量 <
Rust 外墙 180s;并钉死 timeout 只许引用具名常量,防有人写回内联字面量绕过求和)

同 PR 顺手修掉:遥测 host 硬写 `'downloads.acosmi.com'`(实际镜像是
`updates.acosmi.com`,按 host 过滤查 CN 镜像流量恒零命中)→ 改为从真实请求基址派生,
自配镜像折叠为 `custom-mirror` 不外送主机名。

主仓闸门实况:`bun run typecheck` = 0;root 全量套件 **7168 pass / 0 fail**(含新增
摘要判定正反 11 例与预算闸门重写)。

## 三、本机 Windows 上 `tests/crabcode-security/` 12 条红的根因定案(免下一个人重诊)

前轮只记了「12 条 Windows 平台债,CI(ubuntu) 绿」。本轮把根因跑实,**结论:不加平台
守卫、不改测试** —— 其中最大一族是被测工具**自己**声明平台边界并 fail-closed,测试
红是它如实工作的表现;且这些文件受 source-port 锁契约管辖,为本机观感改安全套件得不
偿失。分解(2026-08-16 实测):

| 族 | 条数 | 根因 | 证据等级 |
|---|---|---|---|
| `write_scan_meta.py` 全族 | 7 | 工具显式拒绝:`atomic run capture requires POSIX file locking`(fcntl,Windows 无)—— **设计如此,fail-closed,非缺陷** | 实测错误文本 |
| 可执行位移植完整性 ×2 | 2 | POSIX exec bit 在 NTFS 上不存在 | 测试名自证 |
| `patch_artifacts.py` 符号链接拒收 | 1 | Windows 建符号链接需特权;另含 RESULTS.md 逐字节比对(疑 CRLF) | 实测(比对差异),CRLF 为推断 |
| python3 缺席模拟 + `render_report.py` | 2 | PATH 形态/绝对路径模拟在 Windows 不成立(疑),RESULTS.md 比对同上 | 推断,未逐条深挖 |

执法面 = CI(ubuntu×2 + macos×2,双 Python 版本),全绿。本机跑该套件之前先确认
`python3` 可被 Bun `spawnSync` 找到(WindowsApps 的应用执行别名不行,需真实
`python3.exe`,如 `cp D:\Python311\python.exe D:\Python311\python3.exe`),否则是
44 红不是 12 红。

## 四、给管理员的合并与发布顺序

1. 合并 PR #12(文档归真)与本 PR(顺序无关,均安全,见上表)
2. **合并后必须手动跑一次**:
`gh workflow run sync-plugins-to-mirror.yml -R acosmi/crabcode-source`
(schedule 已于 07-27 因额度移除、dispatch token 因公仓→私仓提权红线故意不配,
同步永远是发版最后一步;不跑则镜像 latest 停在旧 SHA)
3. sync 后约 1 小时内公仓审计器按 cron 自动复核;也可手动
`gh workflow run notify-mirror.yml -R acosmi/CrabCode-Plugin` 立即验证