Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): skill 打包 zip 条目时间戳固定,消除 sha256 时间漂移 (#1121)

Chris Zhang87bbdc29d2d3
diff --git a/apps/desktop/src/main/skillhub/__tests__/zipPacker.test.ts b/apps/desktop/src/main/skillhub/__tests__/zipPacker.test.ts
index dad905c0a1..e29daa578e 100644
--- a/apps/desktop/src/main/skillhub/__tests__/zipPacker.test.ts
+++ b/apps/desktop/src/main/skillhub/__tests__/zipPacker.test.ts
@@ -9,7 +9,7 @@
* - zip 根直接是文件(无顶层目录包裹)
*/
-import { describe, it, expect, beforeAll, afterAll } from 'vitest';
+import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
@@ -252,6 +252,55 @@ describe('skillhub/zipPacker.pack', () => {
expect(r1.sha256).toBe(r2.sha256);
});
+ it('打包时刻不进 zip 字节:跨秒界两次 pack 仍同 sha256(条目时间戳已固定)', async () => {
+ // 回归背景:JSZip 缺省给条目盖打包瞬间墙钟(DOS 2 秒精度),两次 pack 跨过
+ // 秒界即 sha 漂移,上面的 determinism 用例在 CI 上随机红。用假时钟制造
+ // 远大于精度的墙钟差,锁住"时间不参与包字节";带子目录层级,覆盖 walk 递归路径。
+ const dir1 = path.join(tmpDir, 'clock1');
+ const dir2 = path.join(tmpDir, 'clock2');
+ for (const dir of [dir1, dir2]) {
+ writeFile(dir, 'SKILL.md', Buffer.from('clock-free', 'utf8'));
+ writeFile(dir, 'scripts/run.sh', Buffer.from('#!/bin/sh\necho ok\n', 'utf8'));
+ }
+
+ vi.useFakeTimers({ toFake: ['Date'] });
+ try {
+ vi.setSystemTime(new Date('2026-07-30T20:42:59.900Z'));
+ const r1 = await pack(dir1);
+ vi.setSystemTime(new Date('2026-07-30T20:43:07.100Z'));
+ const r2 = await pack(dir2);
+ expect(r1.sha256).toBe(r2.sha256);
+ expect(r1.manifest.files.map((f) => f.relPath)).toContain('scripts/run.sh');
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('时区不进 zip 字节:不同 TZ 下两次 pack 仍同 sha256(本地分量构造的条目时间戳)', async () => {
+ // zip 的 DOS 时间字段无时区,JSZip 用本地 getters 写分量——固定 UTC 瞬间在
+ // 不同时区会写出不同分量。锁住"本地分量构造"后任何时区字节一致。
+ // (POSIX Node 运行中改 process.env.TZ 即生效;不生效的平台上两次 pack
+ // 同时区,断言退化为原确定性语义,仍成立。)
+ const dir1 = path.join(tmpDir, 'tz1');
+ const dir2 = path.join(tmpDir, 'tz2');
+ for (const dir of [dir1, dir2]) {
+ writeFile(dir, 'SKILL.md', Buffer.from('tz-free', 'utf8'));
+ writeFile(dir, 'scripts/run.sh', Buffer.from('#!/bin/sh\necho ok\n', 'utf8'));
+ }
+
+ const prevTz = process.env.TZ;
+ try {
+ process.env.TZ = 'Asia/Shanghai';
+ const r1 = await pack(dir1);
+ process.env.TZ = 'America/New_York';
+ const r2 = await pack(dir2);
+ expect(r1.sha256).toBe(r2.sha256);
+ } finally {
+ if (prevTz === undefined) delete process.env.TZ;
+ else process.env.TZ = prevTz;
+ }
+ });
+
it('aborts before reading files when the signal is already cancelled', async () => {
const dir = path.join(tmpDir, 'abort-before-start');
writeFile(dir, 'SKILL.md', Buffer.from('skill', 'utf8'));
diff --git a/apps/desktop/src/main/skillhub/zipPacker.ts b/apps/desktop/src/main/skillhub/zipPacker.ts
index 01a7559af1..ade4c21fee 100644
--- a/apps/desktop/src/main/skillhub/zipPacker.ts
+++ b/apps/desktop/src/main/skillhub/zipPacker.ts
@@ -18,6 +18,20 @@ import { isIgnoredSkillPackagePath } from './packageIgnore';
const log = createLogger('skillhub:zipPacker');
+/**
+ * zip 条目时间戳固定为常量 UTC 瞬间:JSZip 缺省给每个条目盖打包瞬间的墙钟
+ * (DOS 时间 2 秒精度),同一内容两次打包跨过秒界 sha256 即漂移——"内容相同 ⇒
+ * 包字节相同"的确定性契约破产(publish 重试缓存与 server 端校验都以包 sha
+ * 为锚,CI 上 determinism 用例也会随机红)。
+ *
+ * 时区无关性:本仓 JSZip 3.10 写 DOS 字段用的是 **UTC getters**
+ * (generate/ZipFileWorker.js: getUTCHours / getUTCFullYear),固定 UTC 瞬间
+ * 在任何进程时区下写出的字节都相同;回归用例用 TZ 翻转锁住这一点,若未来
+ * 升级 JSZip 改回本地 getters,该用例会红。(zip DOS 时间下限 1980,不能用
+ * Unix epoch;条目真实 mtime 本就不进 manifest,无信息损失。)
+ */
+const ZIP_ENTRY_DATE = new Date(Date.UTC(2020, 0, 1));
+
// ── Types ────────────────────────────────────────────────────────────────────
export interface PackResult {
@@ -108,7 +122,10 @@ async function walk(
const content = await fs.promises.readFile(fullPath);
checkPackState();
- zip.file(relPath, content);
+ // createFolders: false —— JSZip 缺省会为子目录隐式生成文件夹条目,且该条目
+ // 盖打包瞬间墙钟(date 选项只作用于本文件),确定性再度破口;省略文件夹条目
+ // 是可复现打包的标准做法,解压工具一律按文件路径自建目录,语义无损。
+ zip.file(relPath, content, { date: ZIP_ENTRY_DATE, createFolders: false });
const fileSha256 = await streamSha256(fullPath);
checkPackState();