mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-27 19:10:22 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38e3bdce70 | ||
|
|
3d49ccb501 |
No files matched your search
+98
-54
@@ -3257,6 +3257,100 @@ function flattenTargetPaths(files) {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 用户配置层:官方允许在 profiles/<name>/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/<name>/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;
|
||||
|
||||
+3
-2
@@ -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": [
|
||||
|
||||
@@ -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
|
||||
|
||||
## 中文
|
||||
|
||||
@@ -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)");
|
||||
Reference in new issue
Block a user