From 3d49ccb501f187355af45d555ce36e920c4ddeb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= <188421097+HunYiSage@users.noreply.github.com> Date: Mon, 28 Sep 2026 00:46:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8F=91=E5=B8=83=201.1.25=EF=BC=8C?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E7=94=A8=E6=88=B7=20profile=20cordis.patch.y?= =?UTF-8?q?ml=20=E4=B8=8D=E8=A2=AB=20bak=20=E5=9B=9E=E6=BB=9A=EF=BC=88#43?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 从 targetFiles/backup/revert 移除用户 patch 层,并清理历史误建 bak,避免升级自愈覆盖用户 insert。 Co-authored-by: Cursor --- lib/core.js | 152 ++++++++++++++++++---------- package.json | 5 +- release-notes.md | 13 +++ scripts/test-user-profile-patch.mjs | 52 ++++++++++ 4 files changed, 166 insertions(+), 56 deletions(-) create mode 100644 scripts/test-user-profile-patch.mjs diff --git a/lib/core.js b/lib/core.js index 7a45ae9..4f017da 100644 --- a/lib/core.js +++ b/lib/core.js @@ -3257,6 +3257,100 @@ function flattenTargetPaths(files) { return out; } +/** 用户配置层:官方允许在 profiles/*/cordis.patch.yml 手写 insert,绝不能进 bak/回滚(#43)。 */ +export function isUserProfilePatchFile(fp) { + const n = String(fp || "").replace(/\\/g, "/").toLowerCase(); + return /\/profiles\/[^/]+\/cordis\.patch\.yml$/i.test(n); +} + +/** 丢掉历史误建的 profile cordis.bak,不还原(保留用户当前文件)。 */ +export async function dropStaleUserProfilePatchBackups(dshHome = findDshHome()) { + const dropped = []; + const profilesRoot = path.join(dshHome, "profiles"); + if (!isDir(profilesRoot)) return dropped; + let names = []; + try { + names = fs.readdirSync(profilesRoot, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name); + } catch { + return dropped; + } + for (const name of names) { + const fp = path.join(profilesRoot, name, "cordis.patch.yml"); + const bak = `${fp}.dshpurge.bak`; + if (!fs.existsSync(bak)) continue; + try { + await fsp.unlink(bak); + dropped.push(bak); + } catch { + /* 下次再试 */ + } + } + return dropped; +} + +export async function backupAll(aiBase) { + const files = targetFiles(aiBase); + const made = []; + const errors = []; + const skippedUser = []; + for (const fp of flattenTargetPaths(files)) { + if (!fs.existsSync(fp)) continue; + // 用户 patch 层永不建 bak(#43)。 + if (isUserProfilePatchFile(fp)) { + skippedUser.push(fp); + continue; + } + const bak = fp + ".dshpurge.bak"; + if (!fs.existsSync(bak)) { + try { + await fsp.copyFile(fp, bak); + made.push(bak); + } catch (e) { + errors.push([fp, String(e)]); + } + } + } + const dropped = await dropStaleUserProfilePatchBackups(); + return { made, errors, skippedUser, dropped }; +} + +export async function revertAll(aiBase) { + const files = targetFiles(aiBase); + const reverted = []; + const skipped = []; + const preservedUser = []; + const errors = []; + for (const fp of flattenTargetPaths(files)) { + const bak = fp + ".dshpurge.bak"; + // 用户 patch 层:有 bak 也只丢掉 bak,绝不 copy 回去(#43)。 + if (isUserProfilePatchFile(fp)) { + if (fs.existsSync(bak)) { + try { + await fsp.unlink(bak); + preservedUser.push(fp); + } catch (e) { + errors.push([fp, String(e)]); + } + } + continue; + } + if (fs.existsSync(bak)) { + try { + await fsp.copyFile(bak, fp); + await fsp.unlink(bak); + reverted.push(fp); + } catch (e) { + errors.push([fp, String(e)]); + } + continue; + } + // 没有 bak 时绝不删除目标。源码部署的构建产物缺备份时,删了无法自愈(#41)。 + if (fs.existsSync(fp)) skipped.push(fp); + } + const dropped = await dropStaleUserProfilePatchBackups(); + return { reverted, skipped, preservedUser, dropped, errors }; +} + function nestedPkg(aiBase, pkg, ...rel) { return [ path.join(aiBase, pkg, ...rel), @@ -3292,7 +3386,6 @@ function filterTargetFiles(files) { export function targetFiles(aiBase, dshHome = findDshHome()) { const root = packageRootFromAiBase(aiBase); - const desktopHost = adapterFor(detectSurface()) === "desktop"; const files = { "agent-instructions": firstExisting([ path.join(aiBase, "dsh-agent-instructions", "lib", "index.js"), @@ -3313,17 +3406,9 @@ export function targetFiles(aiBase, dshHome = findDshHome()) { ...(root ? [path.join(root, "packages", "bundle", "headless", "cordis.patch.yml")] : []), ]), "desktop-shell": officialShellMain(), - "profile-web": firstExisting(desktopHost - ? [ - path.join(dshHome, "profiles", "desktop", "cordis.patch.yml"), - path.join(aiBase, "dsh-web-app", "cordis.patch.yml"), - ...(root ? [path.join(root, "packages", "bundle", "web-app", "cordis.patch.yml")] : []), - ] - : [ - path.join(dshHome, "profiles", "web", "cordis.patch.yml"), - path.join(aiBase, "dsh-web-app", "cordis.patch.yml"), - ...(root ? [path.join(root, "packages", "bundle", "web-app", "cordis.patch.yml")] : []), - ]), + // 注意:不要把 profiles//cordis.patch.yml 放进 targetFiles。 + // 那是官方文档支持的「用户 patch 层」,本插件没有任何补丁写入它; + // 一旦进 backupAll/revertAll,版本升级自愈会把用户后来追加的 insert 盖掉(#43)。 "agent-preset": firstExisting([ ...userPresetYml(dshHome, "unrestricted"), ...shippedPresetYml(root, "standard", aiBase), @@ -3999,51 +4084,10 @@ export async function patchStatus(aiBase, patches = ALL_PATCHES) { return status; } -export async function backupAll(aiBase) { - const files = targetFiles(aiBase); - const made = []; - const errors = []; - for (const fp of flattenTargetPaths(files)) { - if (!fs.existsSync(fp)) continue; - const bak = fp + ".dshpurge.bak"; - if (!fs.existsSync(bak)) { - try { - await fsp.copyFile(fp, bak); - made.push(bak); - } catch (e) { - errors.push([fp, String(e)]); - } - } - } - return { made, errors }; -} - -export async function revertAll(aiBase) { - const files = targetFiles(aiBase); - const reverted = []; - const skipped = []; - const errors = []; - for (const fp of flattenTargetPaths(files)) { - const bak = fp + ".dshpurge.bak"; - if (fs.existsSync(bak)) { - try { - await fsp.copyFile(bak, fp); - await fsp.unlink(bak); - reverted.push(fp); - } catch (e) { - errors.push([fp, String(e)]); - } - continue; - } - // 没有 bak 时绝不删除目标。源码部署的构建产物缺备份时,删了无法自愈(#41)。 - if (fs.existsSync(fp)) skipped.push(fp); - } - return { reverted, skipped, errors }; -} - export async function hasBackup(aiBase) { const files = targetFiles(aiBase); for (const fp of flattenTargetPaths(files)) { + if (isUserProfilePatchFile(fp)) continue; if (fs.existsSync(fp + ".dshpurge.bak")) return true; } return false; diff --git a/package.json b/package.json index 72b01ef..42ffe33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-purge", - "version": "1.1.24", + "version": "1.1.25", "description": "DeepSeek Harness 网络安全红队工具:本机官方 Harness 受控评测与清洗,不同模型可换不同提示词;默认提示词面向国模「小码酱」。求 Star ⭐", "type": "module", "main": "lib/index.js", @@ -33,7 +33,8 @@ "test:update": "node scripts/test-update-guards.mjs", "test:rewind": "node scripts/test-rewind-plan.mjs", "test:idempotence": "node scripts/test-patch-idempotence.mjs", - "test:surface": "node scripts/test-surface-desktop.mjs" + "test:surface": "node scripts/test-surface-desktop.mjs", + "test:profile-patch": "node scripts/test-user-profile-patch.mjs" }, "license": "MIT", "keywords": [ diff --git a/release-notes.md b/release-notes.md index 00b46bd..1314850 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,3 +1,16 @@ +# 1.1.25 + +## 中文 + +- **修复 [#43](https://github.com/YuJunZhiXue/dsh-purge/issues/43)**:不再把 `profiles/*/cordis.patch.yml`(用户 patch 层)纳入 `backupAll`/`revertAll`;升级自愈时不会用旧 bak 覆盖用户后来追加的 `insert`。 +- 启动/还原时主动丢掉历史上误建的 `cordis.patch.yml.dshpurge.bak`,保留当前用户文件。 +- 新增 `npm run test:profile-patch`。 + +## English + +- **Fix [#43](https://github.com/YuJunZhiXue/dsh-purge/issues/43)**: stop including `profiles/*/cordis.patch.yml` (user patch layer) in `backupAll`/`revertAll`, so upgrade reapply no longer restores an old bak over later user `insert`s. +- Drop stale `cordis.patch.yml.dshpurge.bak` without restoring; keep the live user file. +- Add `npm run test:profile-patch`. # 1.1.24 ## 中文 diff --git a/scripts/test-user-profile-patch.mjs b/scripts/test-user-profile-patch.mjs new file mode 100644 index 0000000..c0d9a17 --- /dev/null +++ b/scripts/test-user-profile-patch.mjs @@ -0,0 +1,52 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { + isUserProfilePatchFile, + dropStaleUserProfilePatchBackups, + targetFiles, +} from "../lib/core.js"; + +assert.equal( + isUserProfilePatchFile("D:/DeepSeek Harness/.dsh/profiles/web/cordis.patch.yml"), + true, +); +assert.equal( + isUserProfilePatchFile("C:/Users/x/.dsh/profiles/desktop/cordis.patch.yml"), + true, +); +assert.equal( + isUserProfilePatchFile("/home/me/.dsh/profiles/web/node_modules/dsh-purge/cordis.patch.yml"), + false, + "插件自己的 cordis.patch.yml 不是用户 profile 层", +); +assert.equal( + isUserProfilePatchFile("/x/node_modules/@deepseek-ai/dsh-web-app/cordis.patch.yml"), + false, +); + +// targetFiles 不得再收录 profiles/*/cordis.patch.yml(#43) +const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-43-")); +const profilePatch = path.join(tmpHome, "profiles", "web", "cordis.patch.yml"); +fs.mkdirSync(path.dirname(profilePatch), { recursive: true }); +fs.writeFileSync(profilePatch, "patches:\n - insert: user-mcp\n", "utf8"); +const bak = `${profilePatch}.dshpurge.bak`; +fs.writeFileSync(bak, "patches: []\n", "utf8"); + +const files = targetFiles(path.join(tmpHome, "missing-ai-base"), tmpHome); +const flat = Object.values(files).flatMap((v) => (Array.isArray(v) ? v : [v])).filter(Boolean); +assert.equal( + flat.some((fp) => isUserProfilePatchFile(fp)), + false, + "targetFiles 不应包含用户 profile cordis.patch.yml", +); + +const before = fs.readFileSync(profilePatch, "utf8"); +const dropped = await dropStaleUserProfilePatchBackups(tmpHome); +assert.equal(dropped.length, 1, "应丢掉误建 bak"); +assert.equal(fs.existsSync(bak), false, "bak 应已删除"); +assert.equal(fs.readFileSync(profilePatch, "utf8"), before, "用户当前内容必须保留"); + +fs.rmSync(tmpHome, { recursive: true, force: true }); +console.log("ok: user profile cordis.patch.yml not reverted (#43)");