mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:40:22 +08:00
从 PR #188(Pluto-Yt,fix/drift-detection)手改取通用部分落地,不含
0.2.1-rc.3 专属适配:
- 补丁失配不再静默成 skipped:文件在、marker 没有、pattern 一条都不中
→ 新增 drifted 状态(filePatchOutcome / patchStatus / rollupPatchStatus)。
以前这种情况一律返回 skipped 并被当软成功,官方一升级插件就静默失效,
状态页却全绿
- isSelfMarkedPatch() 例外:pattern 本身就是本插件写进宿主的痕迹
(#80 撤销型),找不到 = 本来没加过或已撤过,仍算 skipped
- ALWAYS_SOFT_STATUSES 去掉 skipped;skipped 改为只有可选/自标记型才算
软成功;drifted 看得见(状态页标红、不计入 ready)但不阻断 Apply,
免得把 0.1.6 / 0.2.0 / 0.2.1-rc.2 上的用户堵死
- tallyDisplayPatches / rollupDisplayStatus:drifted 压过 applied
- ensureHostClean 不再过滤 pattern_not_found:没打上就是没打上
- 宿主版本门禁:读 package.json 的 dshTarget,比对宿主真实版本,
gatherState 暴露 host_version / host_target / host_supported,
CLI 与状态页在未适配时直接告警
- applyReplacementsToText 支持 { pattern, replace, expect } 命中数契约:
命中数与声明不符就整条拒写,并回传实际命中数
- #77 锚点容错:写死 5 行改成锚首尾 + 非贪婪窗口 (?:(?!isRewindMarker)[\s\S]){0,300}?,
官方在中间插一行也照样命中;窗口排除 isRewindMarker 保证二次应用不叠加
额外修的 bug:
- rollupPatchStatus 的 lastError 提到最前:以前只在 strictFiles 分支里返回,
一个文件写成功就把别的文件 EACCES / read_error 吞成绿
- applyReplacementsToText 的 expect 契约原先只对字面量 pattern 生效,正则
分支直接 continue 掉了 —— 官方把同一段代码复制成两份时正则会把两处一起
改掉,补上正则分支的命中数检查
- patchStatus 的 drifted 计数排在 strictFiles 的 unmatched 之前:严格补丁的
文件宿主一定在,原文对不上只可能是上游漂移,报 drifted 比 unmatched 更准
- 与 PR 的有意偏差:rollupPatchStatus 里 drifted 排在 already 之后。
多文件补丁(#50/#57/#62)有个别文件本来就不含这段代码(barrel 文件),
按 PR 的顺序会把这几条从 already/applied 翻成 drifted,制造假告警
未取(0.2.1-rc.3 专属、与通用逻辑无法拆开):新增 targetFiles 键
(harness-dev-context / skill-badge)、#32/#53 扩到 rc.3 新位置、
新增 #89 SANDBOX_POLICY_BRAND_NEUTRAL 与 #90 SKILL_BADGE_DESCRIPTION_NEUTRAL
新增 test/patch-status.test.js(29 条,全量 74 条全过)
Based-on: PR #188 by Pluto-Yt
Co-Authored-By: Claude <noreply@anthropic.com>
153 lines
6.8 KiB
JavaScript
Executable File
153 lines
6.8 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
|
||
import * as core from "../lib/core.js";
|
||
import * as rules from "../lib/rules.js";
|
||
|
||
const args = process.argv.slice(2);
|
||
|
||
function printStatus(state) {
|
||
const out = [];
|
||
out.push("dsh-purge 状态 / Status");
|
||
out.push(" 宿主 / surface " + (state.surface || "web"));
|
||
out.push(" DSH_HOME " + state.dsh_home);
|
||
if (state.desktop_install) out.push(" 桌面安装 " + state.desktop_install);
|
||
if (state.ai_base) {
|
||
out.push(" 插件根 / root " + state.ai_base);
|
||
for (const [key, fp] of Object.entries(state.files)) {
|
||
out.push(` ${key.padEnd(22)} ${fp}`);
|
||
}
|
||
} else {
|
||
out.push(" 插件根 / root " + core.missingAiBaseMessage());
|
||
}
|
||
out.push(` shim 目录 ${state.shim_dir || "未定位 / not found"}`);
|
||
out.push(` 备份 / backup ${state.has_backup ? "有 / yes" : "无 / no"}`);
|
||
out.push(` 注入文件 ${state.override_path}`);
|
||
out.push(` 注入状态 ${state.override_status}`);
|
||
out.push("");
|
||
out.push(` 补丁 ${state.patches_applied}/${core.ALL_PATCHES.length} 已清洗, ${state.patches_pending} 待清洗`
|
||
+ (state.patches_drifted ? `, ${state.patches_drifted} 漂移(官方原文已改,需更新 pattern)` : ""));
|
||
if (state.host_version) {
|
||
const compat = state.host_supported === false ? ` ← 未适配:插件声明 ${state.host_target || "?"}` : "";
|
||
out.push(` 宿主版本 / host ${state.host_version}${compat}`);
|
||
}
|
||
for (const p of core.ALL_PATCHES) {
|
||
const s = state.patch_status[p.id];
|
||
const mark = s === "applied" ? "✓" : s === "pending" ? "✗" : s === "drifted" ? "!" : "·";
|
||
out.push(` ${mark} [#${String(p.id).padEnd(2)}] ${p.name.padEnd(30)} ${p.layer}/${p.layer_en} — ${s}`);
|
||
}
|
||
out.push("");
|
||
out.push(` shim: dsh.cmd=${state.shim_cmd} dsh.ps1=${state.shim_ps1} dsh=${state.shim_bin}`);
|
||
return out.join("\n");
|
||
}
|
||
|
||
async function main() {
|
||
const mode = args.find((a) => ["--apply", "--revert", "--uninstall", "--status", "--edit", "--help"].includes(a));
|
||
|
||
if (mode === "--help" || args.includes("-h")) {
|
||
console.log(`dsh-purge 用法:
|
||
dsh-purge --status 显示状态
|
||
dsh-purge --apply 应用全部清洗(提示词+代码+shim+override)
|
||
dsh-purge --revert 回滚还原
|
||
dsh-purge --uninstall 卸载插件(已应用则先还原)
|
||
dsh-purge --edit 编辑注入文件 prompt-inject.md
|
||
dsh-purge --help 帮助`);
|
||
return;
|
||
}
|
||
|
||
const state = await core.gatherState();
|
||
console.log(printStatus(state));
|
||
console.log("");
|
||
|
||
if (mode === "--apply") {
|
||
if (!state.ai_base) {
|
||
console.log("[ERROR] " + core.missingAiBaseMessage());
|
||
console.log(" fix: 把 DSH_BASE 指到含 dsh-agent-instructions/lib 的 @deepseek-ai 目录");
|
||
process.exit(1);
|
||
}
|
||
const inject = rules.resolveInjectText(state.dsh_home);
|
||
if (!inject.text) {
|
||
console.log("[ERROR] 提示词和规则集都是空的,必须先添加提示词");
|
||
process.exit(1);
|
||
}
|
||
const { hosts, report } = await core.applyPatchesAllHosts(core.ALL_PATCHES, {
|
||
preserveClientBundles: false,
|
||
forApply: true,
|
||
});
|
||
for (const h of hosts) console.log(` 宿主 / host → ${h.aiBase}`);
|
||
for (const r of report) {
|
||
const m = r.status === "applied" ? "✓ 已清洗" : r.status === "already" ? "- 已是最新" : r.status === "missing_file" ? "⚠ 文件缺失" : `✗ ${r.status}`;
|
||
console.log(` ${m} patch #${String(r.patch_id).padEnd(2)} ${r.name}`);
|
||
}
|
||
const flashHost = hosts[0]?.aiBase || state.ai_base;
|
||
const flash = core.silenceCmdFlash(flashHost);
|
||
console.log(` cmd-flash=${flash.entry} phase-1=${flash.phase1} ok=${flash.ok}`);
|
||
if (hosts.length > 1) {
|
||
console.log(` 多宿主 / multi-host: ${hosts.length} 份 @deepseek-ai 已清洗`);
|
||
}
|
||
console.log(inject.source === "rule"
|
||
? " - 使用当前规则集,不改写提示词文件"
|
||
: " - 使用已有提示词文件");
|
||
if (state.shim_dir) {
|
||
const r = await core.patchShim(state.shim_dir);
|
||
for (const [fname, st] of Object.entries(r)) {
|
||
if (st === "patched") console.log(` ✓ shim ${fname} 注入 / injected`);
|
||
else if (st === "already_patched") console.log(` - shim ${fname} 已注入 / already injected`);
|
||
else if (st === "missing") console.log(` - shim ${fname} 不存在 / missing`);
|
||
else console.log(` ⚠ shim ${fname}: ${st}`);
|
||
}
|
||
} else {
|
||
console.log(" ⚠ 未定位 shim 目录,跳过注入");
|
||
}
|
||
console.log("");
|
||
console.log("全部完成 / All done。重启 dsh 生效 / Restart dsh to take effect.");
|
||
} else if (mode === "--revert") {
|
||
if (state.ai_base || core.enumeratePatchAiBases().length) {
|
||
const { reverted, errors } = await core.revertAllHosts();
|
||
for (const p of reverted) console.log(` ✓ 已还原 / Reverted ${p}`);
|
||
for (const [p, e] of errors) console.log(` ⚠ 还原失败 ${p}: ${e}`);
|
||
if (reverted.length === 0) console.log(" - 没有补丁备份可还原 / no patch backup");
|
||
}
|
||
if (state.shim_dir) {
|
||
const r = await core.revertShim(state.shim_dir);
|
||
for (const [fname, st] of Object.entries(r)) {
|
||
if (st === "reverted" || st === "stripped_inject") console.log(` ✓ shim ${fname}: ${st}`);
|
||
else console.log(` - shim ${fname}: ${st}`);
|
||
}
|
||
}
|
||
console.log("");
|
||
console.log("回滚完成 / Revert done。重启 dsh 后恢复 / restart to restore.");
|
||
console.log("注: prompt-inject.md 保留(用户文件)/ prompt-inject.md kept (user file)");
|
||
} else if (mode === "--uninstall") {
|
||
const { uninstallPurge } = await import("../lib/uninstall.js");
|
||
const result = await uninstallPurge();
|
||
if (result.applied) console.log(" 已检测到补丁,已还原回原版。");
|
||
else console.log(" 未检测到已应用补丁,仍会清除插件文件。");
|
||
if (result.override?.removed) console.log(` 已删除 ${result.override.path}`);
|
||
if (result.stripped?.length) console.log(` 已从 profile 移除: ${result.stripped.join(", ")}`);
|
||
for (const e of result.errors || []) console.log(` ⚠ ${e}`);
|
||
if (!result.ok) {
|
||
console.log(" 卸载中止:还原失败,插件文件未删除。");
|
||
process.exit(1);
|
||
}
|
||
console.log("");
|
||
console.log("卸载完成。请重启 dsh。");
|
||
} else if (mode === "--edit") {
|
||
const r = core.editOverride(state.dsh_home);
|
||
if (!r.ok && r.needCreate) {
|
||
await core.installOverride(state.dsh_home, true);
|
||
console.log(` ✓ 已创建默认注入文件:${state.override_path}`);
|
||
core.editOverride(state.dsh_home);
|
||
} else {
|
||
console.log(` → 已打开 ${r.editor} 编辑注入文件:${r.path}`);
|
||
}
|
||
console.log(" 编辑完成后重启 dsh 生效。");
|
||
} else {
|
||
console.log("默认不清洗,使用 --apply 应用清洗。");
|
||
}
|
||
}
|
||
|
||
main().catch((e) => {
|
||
console.error("[dsh-purge] error:", e);
|
||
process.exit(1);
|
||
});
|