mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:10:22 +08:00
fix: 发布 1.1.66,Hooks 开关与会话来源
清洗页可关掉 hooks deny 旁路。继续/拒答续跑改用官方允许的来源 kind,限流和传输失败不再自动续一轮。 Closes #80. Closes #81. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
76e64d65e0
commit
09bac57f2c
13 files changed
+789
-59
No files matched your search
+255
-6
@@ -28,6 +28,7 @@ import {
|
||||
import { adapterFor, detectSurface } from "./surface.js";
|
||||
import { hostPromptKeepsInject } from "./host-clean.js";
|
||||
import { openSlot } from "./table-read.js";
|
||||
import * as hooksDeny from "./hooks-deny.js";
|
||||
|
||||
// 无归档时用 node:fs;app.asar 还在时改写真实的 resources/app,避免新建文件 ENOENT。
|
||||
const fs = hostFs;
|
||||
@@ -1775,6 +1776,11 @@ async function resolveAiBaseForApply(aiBase, options = {}) {
|
||||
}
|
||||
|
||||
export async function applyPatchesAllHosts(patches = ALL_PATCHES, options = {}) {
|
||||
const settings = options.hooksDeny?.settings || getHooksDenySettings();
|
||||
const pick = options.hooksDeny?.selectPatches || hooksDeny.selectPatches;
|
||||
const selected = pick(patches, settings);
|
||||
const hookDenyRestoreFailed = [];
|
||||
const hookDenyRestoreChanged = [];
|
||||
const hosts = [];
|
||||
const seen = new Set();
|
||||
for (const raw of enumeratePatchAiBases()) {
|
||||
@@ -1784,7 +1790,12 @@ export async function applyPatchesAllHosts(patches = ALL_PATCHES, options = {})
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
await backupAll(aiBase);
|
||||
const report = await applyPatches(aiBase, patches, options);
|
||||
if (settings.hooksDenyBypass === false) {
|
||||
const restored = await restoreHookDenyForBases([aiBase]);
|
||||
hookDenyRestoreFailed.push(...restored.failed);
|
||||
hookDenyRestoreChanged.push(...restored.changed);
|
||||
}
|
||||
const report = await applyPatches(aiBase, selected, options);
|
||||
hosts.push({
|
||||
aiBase,
|
||||
report,
|
||||
@@ -1793,7 +1804,12 @@ export async function applyPatchesAllHosts(patches = ALL_PATCHES, options = {})
|
||||
flash: silenceCmdFlash(aiBase),
|
||||
});
|
||||
}
|
||||
return { hosts, report: mergeApplyReports(hosts.map((h) => h.report)) };
|
||||
return {
|
||||
hosts,
|
||||
report: mergeApplyReports(hosts.map((h) => h.report)),
|
||||
hookDenyRestoreFailed,
|
||||
hookDenyRestoreChanged,
|
||||
};
|
||||
}
|
||||
|
||||
export async function revertAllHosts(options = {}) {
|
||||
@@ -4267,6 +4283,47 @@ const NEW_TOOL_PATCHES = [
|
||||
],
|
||||
markers: ["[dsh-purge] prepare assemble before this step"],
|
||||
},
|
||||
{
|
||||
id: 81,
|
||||
name: "HUNG_PROMPT_EVERY_STEP",
|
||||
layer: "代码",
|
||||
layer_en: "Code",
|
||||
desc: "每一步用同一份 prompt-inject 全文,第二轮不再把注入换掉 / keep the hung prompt-inject on every step",
|
||||
file: ["agent-loop"],
|
||||
skipIfMarked: true,
|
||||
replacements: [
|
||||
{
|
||||
pattern:
|
||||
"\t\tconst dshPurgePrepare = globalThis.__dshPurgePrepareAssemble;\n" +
|
||||
"\t\tif (typeof dshPurgePrepare === \"function\") {\n" +
|
||||
"\t\t\ttry { dshPurgePrepare(this.loopCtx); } catch { /* [dsh-purge] prepare assemble before this step */ }\n" +
|
||||
"\t\t}\n" +
|
||||
"\t\tconst assembly = await this.loopCtx.systemPrompt.assemble(assembleContextFor(this, signal));",
|
||||
replace:
|
||||
"\t\tconst dshPurgePrepare = globalThis.__dshPurgePrepareAssemble;\n" +
|
||||
"\t\tlet dshPurgePrompt = null;\n" +
|
||||
"\t\tif (typeof dshPurgePrepare === \"function\") {\n" +
|
||||
"\t\t\ttry { dshPurgePrompt = dshPurgePrepare(this.loopCtx); } catch { /* [dsh-purge] prepare assemble before this step */ }\n" +
|
||||
"\t\t}\n" +
|
||||
"\t\tconst dshPurgeAssemble = dshPurgePrompt && typeof dshPurgePrompt.assemble === \"function\" ? dshPurgePrompt : this.loopCtx.systemPrompt;\n" +
|
||||
"\t\tconst assembly = await dshPurgeAssemble.assemble(assembleContextFor(this, signal));",
|
||||
},
|
||||
{
|
||||
pattern:
|
||||
"\t\tconst { assembly } = decision;\n" +
|
||||
"\t\tconst renderedPrompt = renderPrompt(assembly);",
|
||||
replace:
|
||||
"\t\tconst { assembly } = decision;\n" +
|
||||
"\t\tlet renderedPrompt = renderPrompt(assembly);\n" +
|
||||
"\t\tconst dshPurgeHung = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? globalThis.__dshPurgeHungPrompt() : \"\";\n" +
|
||||
"\t\tconst dshPurgeNeedle = String(dshPurgeHung || \"\").trim().slice(0, 96);\n" +
|
||||
"\t\tif (dshPurgeNeedle && !String(renderedPrompt || \"\").includes(dshPurgeNeedle)) {\n" +
|
||||
"\t\t\trenderedPrompt = String(dshPurgeHung).trim() + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject every step\n" +
|
||||
"\t\t}",
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] hang full prompt-inject every step"],
|
||||
},
|
||||
{
|
||||
id: 66,
|
||||
name: "SYSTEM_PROMPT_RENDER_GUARD",
|
||||
@@ -5026,6 +5083,16 @@ const NEW_TOOL_PATCHES = [
|
||||
'\t\t\tconst text = merged.reason ?? "continue: blocked by Stop hook";\n' +
|
||||
'\t\t\tagent.steer(createUserMessage({',
|
||||
},
|
||||
{
|
||||
pattern:
|
||||
'\t\tif (merged.decision === "deny") {\n' +
|
||||
'\t\t\tconst text = merged.reason ?? "continue: blocked by Stop hook";\n' +
|
||||
'\t\t\tagent.steer(createUserMessage({',
|
||||
replace:
|
||||
'\t\tif (merged.decision === "deny") {\n' +
|
||||
'\t\t\tconst text = merged.reason ?? "continue: blocked by Stop hook";\n' +
|
||||
'\t\t\tagent.steer(createUserMessage({',
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] Stop hook steer disabled"],
|
||||
},
|
||||
@@ -5046,9 +5113,53 @@ const NEW_TOOL_PATCHES = [
|
||||
'\t\tif (merged.decision === "deny") { /* [dsh-purge] UserPromptSubmit deny→allow */ }\n' +
|
||||
'\t\telse if (merged.decision === "__dsh_purge_deny_disabled__") { /* [dsh-purge] pre-step reject disabled */ }',
|
||||
},
|
||||
{
|
||||
pattern: '\t\tif (merged.decision === "deny") return { kind: "reject" };',
|
||||
replace: '\t\tif (merged.decision === "deny") return { kind: "reject" };',
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] UserPromptSubmit deny→allow"],
|
||||
},
|
||||
{
|
||||
id: 78,
|
||||
name: "OVERLAY_RESTORE_FROM_BAK",
|
||||
layer: "代码",
|
||||
layer_en: "Code",
|
||||
desc: "声明过的补丁文件不见时,从 .dshpurge.bak 拷回再加载 / restore a missing declared overlay from its bak",
|
||||
file: ["app-boot"],
|
||||
optional: true,
|
||||
skipIfMarked: true,
|
||||
replacements: [
|
||||
{
|
||||
pattern:
|
||||
"function loadOverlayPatches(binName, file) {\n" +
|
||||
"\tlet content;\n" +
|
||||
"\ttry {\n" +
|
||||
"\t\tcontent = readFileSync(file, \"utf8\");\n" +
|
||||
"\t} catch (error) {\n" +
|
||||
"\t\tthrow new Error(`${binName}: failed to read overlay ${file}: ${String(error)}`);\n" +
|
||||
"\t}\n" +
|
||||
"\treturn parsePatchList(binName, file, content, \"overlay\");\n" +
|
||||
"}",
|
||||
replace:
|
||||
"function loadOverlayPatches(binName, file) {\n" +
|
||||
"\tlet content;\n" +
|
||||
"\ttry {\n" +
|
||||
"\t\tcontent = readFileSync(file, \"utf8\");\n" +
|
||||
"\t} catch (error) {\n" +
|
||||
"\t\tconst bak = file + \".dshpurge.bak\"; // [dsh-purge] restore missing overlay from bak\n" +
|
||||
"\t\tif (error && error.code === \"ENOENT\" && fs.existsSync(bak)) {\n" +
|
||||
"\t\t\ttry { fs.copyFileSync(bak, file); content = readFileSync(file, \"utf8\"); }\n" +
|
||||
"\t\t\tcatch { /* bak restore failed */ }\n" +
|
||||
"\t\t}\n" +
|
||||
"\t\tif (content === void 0) throw new Error(`${binName}: failed to read overlay ${file}: ${String(error)}`);\n" +
|
||||
"\t}\n" +
|
||||
"\treturn parsePatchList(binName, file, content, \"overlay\");\n" +
|
||||
"}",
|
||||
},
|
||||
],
|
||||
markers: ["[dsh-purge] restore missing overlay from bak"],
|
||||
},
|
||||
];
|
||||
|
||||
export const ALL_PATCHES = [...PATCHES, ...CODE_PATCHES, ...ENGINE_PATCHES, ...NEW_TOOL_PATCHES];
|
||||
@@ -5181,6 +5292,79 @@ export async function dropStaleUserProfilePatchBackups(dshHome = findDshHome())
|
||||
return dropped;
|
||||
}
|
||||
|
||||
const BAK_SUFFIX = ".dshpurge.bak";
|
||||
|
||||
function shouldRestoreOriginal(original, bak) {
|
||||
if (isUserProfilePatchFile(original)) return false;
|
||||
let bakStat;
|
||||
try {
|
||||
bakStat = fs.statSync(bak);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (!bakStat.isFile() || bakStat.size <= 0) return false;
|
||||
try {
|
||||
const st = fs.statSync(original);
|
||||
if (!st.isFile() || st.size > 0) return false;
|
||||
} catch (e) {
|
||||
if (!e || e.code !== "ENOENT") return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 正式文件不见、或被写成空文件时,从旁边的 .dshpurge.bak 拷回去。
|
||||
* 不删 bak。targetFiles 只认还在的文件,所以丢了以后还原/应用都看不见这份备份。
|
||||
* @returns 已拷回的正式路径
|
||||
*/
|
||||
export function restoreMissingOriginalsFromBak(aiBase) {
|
||||
const restored = [];
|
||||
if (!aiBase || !isDir(aiBase)) return restored;
|
||||
const roots = [aiBase];
|
||||
const nested = path.join(aiBase, "dsh", "node_modules", "@deepseek-ai");
|
||||
if (isDir(nested)) roots.push(nested);
|
||||
const visit = (dir, depth) => {
|
||||
if (depth > 5) return;
|
||||
let ents = [];
|
||||
try {
|
||||
ents = fs.readdirSync(dir, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const ent of ents) {
|
||||
if (ent.name === "node_modules" || ent.name === ".git") continue;
|
||||
const fp = path.join(dir, ent.name);
|
||||
if (ent.isDirectory()) {
|
||||
visit(fp, depth + 1);
|
||||
continue;
|
||||
}
|
||||
if (!ent.isFile() || !ent.name.endsWith(BAK_SUFFIX)) continue;
|
||||
const original = fp.slice(0, -BAK_SUFFIX.length);
|
||||
if (!shouldRestoreOriginal(original, fp)) continue;
|
||||
try {
|
||||
fs.copyFileSync(fp, original);
|
||||
restored.push(original);
|
||||
} catch {
|
||||
/* 文件被占用时下次启动再试 */
|
||||
}
|
||||
}
|
||||
};
|
||||
for (const root of roots) visit(root, 0);
|
||||
return restored;
|
||||
}
|
||||
|
||||
export function restoreMissingOriginalsAllHosts() {
|
||||
const restored = [];
|
||||
for (const aiBase of enumeratePatchAiBases()) {
|
||||
try {
|
||||
restored.push(...restoreMissingOriginalsFromBak(aiBase));
|
||||
} catch {
|
||||
/* 一个宿主失败不挡住其余 */
|
||||
}
|
||||
}
|
||||
return restored;
|
||||
}
|
||||
|
||||
export async function backupAll(aiBase) {
|
||||
const files = targetFiles(aiBase);
|
||||
const made = [];
|
||||
@@ -5217,6 +5401,7 @@ async function sameFileBytes(left, right) {
|
||||
}
|
||||
|
||||
export async function revertAll(aiBase, options = {}) {
|
||||
restoreMissingOriginalsFromBak(aiBase);
|
||||
const files = targetFiles(aiBase);
|
||||
const reverted = [];
|
||||
const skipped = [];
|
||||
@@ -5518,6 +5703,9 @@ export function targetFiles(aiBase, dshHome = findDshHome()) {
|
||||
"agent-loop": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-agent-loop", "lib", "index.js"),
|
||||
]),
|
||||
"app-boot": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-app-boot", "lib", "index.js"),
|
||||
]),
|
||||
"conversation-client": allExisting([
|
||||
...nestedPkg(aiBase, "dsh-client-ui-conversation", "lib", "client.js"),
|
||||
]),
|
||||
@@ -5850,6 +6038,20 @@ function fileKeysOf(p) {
|
||||
return Array.isArray(p.file) ? p.file : [p.file];
|
||||
}
|
||||
|
||||
export function hookDenyPaths(aiBase) {
|
||||
const files = targetFiles(aiBase);
|
||||
const out = [];
|
||||
for (const p of ALL_PATCHES) {
|
||||
if (!hooksDeny.HOOK_DENY_PATCH_IDS.includes(p.id)) continue;
|
||||
for (const key of fileKeysOf(p)) {
|
||||
for (const fp of pathsOf(files, key)) {
|
||||
if (fp && !out.includes(fp)) out.push(fp);
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function patchReplacements(p) {
|
||||
if (Array.isArray(p.replacements)) return p.replacements;
|
||||
return (p.patterns ?? []).map((pat) => ({ pattern: pat, replace: p.replace }));
|
||||
@@ -5937,7 +6139,9 @@ export function applyReplacementsToText(text, patch, fp = "") {
|
||||
}
|
||||
const needle = asLf(pattern);
|
||||
if (!out.includes(needle)) continue;
|
||||
out = out.split(needle).join(written);
|
||||
const next = out.split(needle).join(written);
|
||||
if (next === out) continue;
|
||||
out = next;
|
||||
changed = true;
|
||||
}
|
||||
if (crlf) out = out.replace(/\n/g, "\r\n");
|
||||
@@ -5956,7 +6160,7 @@ function rollupPatchStatus({ applied, already, missing, notFound, lastError, key
|
||||
return "pattern_not_found";
|
||||
}
|
||||
|
||||
const ALWAYS_SOFT_STATUSES = new Set(["applied", "already", "missing_file", "skipped", "na"]);
|
||||
const ALWAYS_SOFT_STATUSES = new Set(["applied", "already", "missing_file", "skipped", "na", "off"]);
|
||||
|
||||
export function patchAppliesHere(patch, surface = detectSurface()) {
|
||||
const want = patch?.surfaces;
|
||||
@@ -6009,6 +6213,48 @@ async function saveText(p, text) {
|
||||
return true;
|
||||
}
|
||||
|
||||
export const hookDenyIo = {
|
||||
hookDenyPaths,
|
||||
loadText,
|
||||
saveText,
|
||||
};
|
||||
|
||||
let hooksDenySettings = null;
|
||||
let hooksDenyConfigValue;
|
||||
|
||||
export function setHooksDenyConfigValue(value) {
|
||||
hooksDenyConfigValue = value;
|
||||
}
|
||||
|
||||
export function refreshHooksDenySettings() {
|
||||
hooksDenySettings = Object.freeze(
|
||||
hooksDeny.loadHookDenySettings(findDshHome(), hooksDenyConfigValue),
|
||||
);
|
||||
return hooksDenySettings;
|
||||
}
|
||||
|
||||
export function getHooksDenySettings() {
|
||||
if (!hooksDenySettings) return refreshHooksDenySettings();
|
||||
return hooksDenySettings;
|
||||
}
|
||||
|
||||
export function setHooksDenySettings(patch) {
|
||||
const saved = hooksDeny.saveHookDenySettings(findDshHome(), patch);
|
||||
hooksDenySettings = Object.freeze({ hooksDenyBypass: saved.hooksDenyBypass });
|
||||
return saved;
|
||||
}
|
||||
|
||||
export async function restoreHookDenyForBases(bases) {
|
||||
const failed = [];
|
||||
const changed = [];
|
||||
for (const aiBase of bases || []) {
|
||||
const r = await hooksDeny.restoreHookDenyOfficial(aiBase, hookDenyIo);
|
||||
changed.push(...r.changed);
|
||||
failed.push(...r.sentinelLeft, ...r.misses);
|
||||
}
|
||||
return { changed, failed };
|
||||
}
|
||||
|
||||
async function repairPluginCordisYaml() {
|
||||
const fp = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "cordis.patch.yml");
|
||||
if (!isFile(fp)) return;
|
||||
@@ -6018,6 +6264,7 @@ async function repairPluginCordisYaml() {
|
||||
}
|
||||
|
||||
export async function applyPatches(aiBase, patches = ALL_PATCHES, options = {}) {
|
||||
restoreMissingOriginalsFromBak(aiBase);
|
||||
const files = targetFiles(aiBase);
|
||||
const report = [];
|
||||
const preserveClientBundles = options.preserveClientBundles === true;
|
||||
@@ -6180,6 +6427,7 @@ export async function patchStatus(aiBase, patches = ALL_PATCHES) {
|
||||
else if (skipped > 0) status[p.id] = "skipped";
|
||||
else status[p.id] = p.optional ? "na" : "missing_file";
|
||||
}
|
||||
await hooksDeny.annotateHookDenyStatus(status, getHooksDenySettings(), aiBase, hookDenyIo);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -6790,7 +7038,7 @@ export function editOverride(dshHome) {
|
||||
|
||||
/** 列表上的“已就绪”:已写入、或当前版本不需要 / 组件未装(与 apply 的 soft-ok 对齐)。 */
|
||||
export function patchStatusReady(status, patch = null) {
|
||||
if (status === "applied" || status === "already") return true;
|
||||
if (status === "applied" || status === "already" || status === "off") return true;
|
||||
if (status === "skipped" || status === "missing_file") return true;
|
||||
if (status === "na") return false;
|
||||
if (status === "pattern_not_found" && patch?.strictFiles) return false;
|
||||
@@ -6825,7 +7073,7 @@ export async function gatherState() {
|
||||
patches_total += 1;
|
||||
if (s === "applied") patches_applied += 1;
|
||||
if (s === "pending") patches_pending += 1;
|
||||
if (s === "skipped" || s === "missing_file") patches_skipped += 1;
|
||||
if (s === "skipped" || s === "missing_file" || s === "off") patches_skipped += 1;
|
||||
if (patchStatusReady(s, p)) patches_ready += 1;
|
||||
}
|
||||
let shim_cmd = "missing";
|
||||
@@ -6868,5 +7116,6 @@ export async function gatherState() {
|
||||
override_path: findOverrideFile(dshHome),
|
||||
override_status: await overrideStatus(dshHome),
|
||||
asar_still_sealed: desktopAsarStillSealed(),
|
||||
hooks_deny_bypass: getHooksDenySettings().hooksDenyBypass,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user