release: 1.1.70

Hub/残装不再空等 pluginPackages;清洗页合并升级链展示,Apply 仍全打。合并 #97/#98/#99。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-10-11 18:01:05 +08:00
1 parent 1863a4b1e4
commit a5dcbb02d8
17 files changed
+774 -471

No files matched your search

+334 -147
View File
@@ -28,6 +28,7 @@ import {
} from "./extract-asar.js";
import { adapterFor, detectSurface } from "./surface.js";
import { hostPromptKeepsInject } from "./host-clean.js";
import { PHASE1_KEEP_EXPR, widenPhase1KeepFilter } from "./phase1-keep.js";
import { openSlot } from "./table-read.js";
import * as hooksDeny from "./hooks-deny.js";
@@ -141,6 +142,7 @@ function pathEnvDirs() {
const memo = {
prefix: { ready: false, value: null },
shim: { ready: false, value: null },
desktopShim: { ready: false, value: null },
ai: { ready: false, value: null },
};
@@ -369,6 +371,7 @@ export function silenceCmdFlash(aiBase = findAiBase()) {
const subprocess = silenceSubprocessLocalFlash();
const customBash = silenceLiangshenCustomBashFlash();
const phase1 = silenceLiangshenPhase1Strip();
const officialPhase1 = silenceOfficialPhase1Strip();
const liangshenPrompt = keepLiangshenOperatorPrompt();
const mnemon = silenceMnemonGitFlash();
// bin.js 写失败(missing_bin / ACL)不阻断 Apply。源码 cli 跳过注入也算成功(#41)。
@@ -394,6 +397,7 @@ export function silenceCmdFlash(aiBase = findAiBase()) {
subprocess,
customBash,
phase1,
officialPhase1,
liangshenPrompt,
mnemon,
ok: hideOk && liangshenOk,
@@ -473,15 +477,10 @@ export function revertCmdFlash(aiBase = findAiBase(), dshHome = findDshHome()) {
/const PERSONA_SECTION_NAMES = new Set\(\[[^\]]*dsh-purge[^\]]*\]\)/,
origSet,
);
const officialFilter =
" const sections = Array.isArray(assembled.sections)\n" +
" ? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name))\n" +
" : undefined";
const wideFilter =
" const sections = Array.isArray(assembled.sections)\n" +
' ? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name) || String(section?.name || "").startsWith("dsh-purge"))\n' +
" : undefined";
next = next.replace(wideFilter, officialFilter);
next = next.replace(
/assembled\.sections\.filter\(section => PERSONA_SECTION_NAMES\.has\(section\?\.name\)(?: \|\| String\(section\?\.name \|\| ""\)\.startsWith\("dsh-purge"\))?(?: \|\| String\(section\?\.name \|\| ""\)\.startsWith\("dshpg-"\))?\)/g,
"assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name))",
);
next = next.replace(/\n \/\/ \[dsh-purge\] phase-1 keep persona\+inject sections/g, "");
next = next.replace(/\n \/\/ \[dsh-purge\] phase-1 keep full system-prompt sections/g, "");
return next;
@@ -720,6 +719,10 @@ export function silenceLiangshenPhase1Strip(dshHome = findDshHome()) {
" ? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name))\n" +
" : undefined";
const needleWide =
" const sections = Array.isArray(assembled.sections)\n" +
" ? assembled.sections.filter(section => " + PHASE1_KEEP_EXPR + ")\n" +
" : undefined";
const needleWideOld =
" const sections = Array.isArray(assembled.sections)\n" +
' ? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name) || String(section?.name || "").startsWith("dsh-purge"))\n' +
" : undefined";
@@ -767,8 +770,10 @@ export function silenceLiangshenPhase1Strip(dshHome = findDshHome()) {
if (next.includes(keepAllStacked)) next = next.replace(keepAllStacked, replacement);
else if (next.includes(keepAll)) next = next.replace(keepAll, replacement);
else if (next.includes(unfiltered)) next = next.replace(unfiltered, replacement);
else if (next.includes(needleWideOld)) next = next.replace(needleWideOld, replacement);
else if (next.includes(needleWide) && !next.includes(marker)) next = next.replace(needleWide, replacement);
else if (next.includes(needle)) next = next.replace(needle, replacement);
next = widenPhase1KeepFilter(next);
next = next.replace(/\n \/\/ \[dsh-purge\] phase-1 keep full system-prompt sections/g, "");
next = next.replace(
@@ -799,6 +804,7 @@ export function silenceLiangshenPhase1Strip(dshHome = findDshHome()) {
next.includes(marker) &&
!next.includes(keepAllMarker) &&
next.includes('startsWith("dsh-purge")') &&
next.includes('startsWith("dshpg-")') &&
!next.includes(unfiltered) &&
!next.includes(contextMarker) &&
!next.includes('plugin === "@deepseek-ai/dsh-system-prompt"');
@@ -820,14 +826,69 @@ export function silenceLiangshenPhase1Strip(dshHome = findDshHome()) {
return hit;
}
const LIANGSHEN_KEEP_OPERATOR = "const sections = assembled.sections.filter(section => keep.has(section?.name) || String(section?.name || \"\").startsWith(\"dsh-purge\")) // [dsh-purge] liangshen keep operator";
function listOfficialPhase1Files(dshHome = findDshHome()) {
const out = [];
const push = (fp) => {
if (keepSurfaceFile(fp) && !out.includes(fp)) out.push(fp);
};
const addBase = (aiBase) => {
if (!aiBase) return;
push(path.join(aiBase, "dsh-agent-instructions", "lib", "index.js"));
push(path.join(aiBase, "dsh-agent-instructions", "lib", "bootstrap.js"));
};
try { addBase(findAiBase()); } catch { /* 启动时还没定位到宿主 */ }
const profilesRoot = path.join(dshHome || findDshHome(), "profiles");
for (const name of ["web", "desktop"]) {
addBase(path.join(profilesRoot, name, "node_modules", "@deepseek-ai"));
}
return out;
}
/** 官方 dsh-agent-instructions 里若有 phase-1 白名单,同样放行 dsh-purge* / dshpg-*。 */
export function silenceOfficialPhase1Strip(dshHome = findDshHome()) {
const files = listOfficialPhase1Files(dshHome);
if (files.length === 0) return "missing";
let hit = "missing";
for (const fp of files) {
let text;
try {
text = fs.readFileSync(fp, "utf8");
} catch {
continue;
}
if (!text.includes("PERSONA_SECTION_NAMES")) {
hit = worseLiangshenHit(hit, "missing");
continue;
}
const next = widenPhase1KeepFilter(text);
if (next === text && text.includes('startsWith("dshpg-")')) {
hit = worseLiangshenHit(hit, "already");
continue;
}
if (next === text) {
hit = worseLiangshenHit(hit, "pattern_miss");
continue;
}
try {
fs.writeFileSync(fp, next, "utf8");
hit = worseLiangshenHit(hit, "patched");
} catch (e) {
hit = worseLiangshenHit(hit, `error:${e}`);
}
}
return hit;
}
const LIANGSHEN_KEEP_OPERATOR = "const sections = assembled.sections.filter(section => keep.has(section?.name) || String(section?.name || \"\").startsWith(\"dsh-purge\") || String(section?.name || \"\").startsWith(\"dshpg-\")) // [dsh-purge] liangshen keep operator";
const LIANGSHEN_KEEP_OPERATOR_OLD = "const sections = assembled.sections.filter(section => keep.has(section?.name) || String(section?.name || \"\").startsWith(\"dsh-purge\")) // [dsh-purge] liangshen keep operator";
/** 梁神新版 minimal-prompt 只留人设段。操作者段要留下。红队文件不在这里。 */
export function liangshenPromptFilterText(text) {
const src = asLf(text);
if (src.includes(LIANGSHEN_KEEP_OPERATOR)) return src;
if (src.includes(LIANGSHEN_KEEP_OPERATOR_OLD)) return src.replace(LIANGSHEN_KEEP_OPERATOR_OLD, LIANGSHEN_KEEP_OPERATOR);
const needle = "const sections = assembled.sections.filter(section => keep.has(section?.name))";
if (!src.includes(needle)) return src;
if (src.includes("[dsh-purge] liangshen keep operator")) return src;
return src.replace(needle, LIANGSHEN_KEEP_OPERATOR);
}
@@ -872,8 +933,8 @@ export function keepLiangshenOperatorPrompt(dshHome = findDshHome()) {
const next = liangshenPromptFilterText(text);
if (next === asLf(text)) {
const keepsOperator =
text.includes("[dsh-purge] liangshen keep operator") ||
text.includes('startsWith("dsh-purge")');
text.includes(LIANGSHEN_KEEP_OPERATOR) ||
(text.includes("[dsh-purge] liangshen keep operator") && text.includes('startsWith("dshpg-")'));
hit = worseLiangshenHit(hit, keepsOperator ? "already" : "pattern_miss");
continue;
}
@@ -1664,6 +1725,20 @@ export function findNpmPrefix() {
});
}
/** 桌面官方 CLI:resources/runtime/cli/bin 里的 dsh.cmd / dsh.ps1 / dsh。 */
export function findDesktopShimDir() {
return remember("desktopShim", () => {
const exe = desktop.findDesktopAppExecutable({ discover: false });
if (exe) {
const resources = desktop.resourcesDirForExecutable(exe);
const cliBin = resources ? path.join(resources, "runtime", "cli", "bin") : "";
if (cliBin && isDir(cliBin)) return path.normalize(cliBin);
}
const sealed = listDesktopCommandRuntimeDirs();
return sealed.find(dirHasDshLauncher) || sealed[0] || null;
});
}
export function findShimDir() {
return remember("shim", () => {
const fromPath = pathEnvDirs().find(dirHasDshLauncher);
@@ -1800,9 +1875,10 @@ function findAiBaseWeb() {
}
/** 一次 Apply 要打到的全部 @deepseek-ai 插件根(Web/CLI npm-global + 桌面 resources/app 等)。 */
export function enumeratePatchAiBases() {
export function enumeratePatchAiBases(options = {}) {
const keys = new Set();
const out = [];
const discover = options.discover === true;
const add = (p) => {
if (!p) return;
const n = path.normalize(p);
@@ -1817,7 +1893,7 @@ export function enumeratePatchAiBases() {
};
add(findAiBaseWeb());
add(findAiBaseDesktop());
for (const root of desktop.listDesktopPackageRoots()) add(root);
for (const root of desktop.listDesktopPackageRoots({ discover })) add(root);
const env = String(process.env.DSH_BASE || "").trim();
if (env) add(path.normalize(env));
add(findAiBaseFromDshHome(findDshHome()));
@@ -1872,7 +1948,7 @@ export async function applyPatchesAllHosts(patches = ALL_PATCHES, options = {})
const hookDenyRestoreChanged = [];
const hosts = [];
const seen = new Set();
for (const raw of enumeratePatchAiBases()) {
for (const raw of enumeratePatchAiBases({ discover: options.forApply === true || options.discover === true })) {
const aiBase = await resolveAiBaseForApply(raw, options);
if (!aiBase) continue;
const key = path.normalize(aiBase).replace(/\\/g, "/").toLowerCase();
@@ -1904,7 +1980,7 @@ export async function applyPatchesAllHosts(patches = ALL_PATCHES, options = {})
export async function revertAllHosts(options = {}) {
const reverted = [];
const errors = [];
for (const aiBase of enumeratePatchAiBases()) {
for (const aiBase of enumeratePatchAiBases({ discover: options.discover !== false })) {
try {
const r = await revertAll(aiBase, options);
reverted.push(...(r.reverted || []));
@@ -2125,7 +2201,7 @@ function aiBaseBelongsToExe(aiBase, exe) {
/** 密封官方包只认这份 exe 的 resources/app。旁边同名目录不算当前宿主。 */
export async function resolveOfficialAiBase(aiBase, options = {}) {
const handoff = sealedAsarHandoff();
const handoff = sealedAsarHandoff({ discover: options.forApply === true });
if (!handoff?.exe) return { aiBase: aiBase || null, handoff: null };
if (!options.forApply && aiBaseBelongsToExe(aiBase, handoff.exe)) return { aiBase, handoff };
const opened = await openSealedDesktopHost({
@@ -2164,7 +2240,7 @@ export async function ensureHostClean() {
}));
const patchOpts = { preserveClientBundles: false, forApply: true };
let bases = enumeratePatchAiBases();
let bases = enumeratePatchAiBases({ discover: true });
if (!bases.some((b) => path.normalize(b).toLowerCase() === path.normalize(aiBase).toLowerCase())) {
bases = [aiBase, ...bases];
}
@@ -3379,12 +3455,12 @@ const NEW_TOOL_PATCHES = [
pattern:
"? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name))",
replace:
"? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name) || String(section?.name || \"\").startsWith(\"dsh-purge\"))",
"? assembled.sections.filter(section => PERSONA_SECTION_NAMES.has(section?.name) || String(section?.name || \"\").startsWith(\"dsh-purge\") || String(section?.name || \"\").startsWith(\"dshpg-\"))",
},
],
markers: [
"'dsh-purge:identity', 'dsh-purge', 'dsh-purge:rules', 'dsh-purge:post'",
'startsWith("dsh-purge")',
'startsWith("dshpg-")',
],
},
{
@@ -4476,93 +4552,6 @@ 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: [
{
// 新宿主 (0.2.x step(assembly) 直接拿 assembly):变量名改成 system,不再从 decision 解构。
// 旁路排查钉死:日志投影路径有注入,但 LLM 实际收到的 system 没注入 —— 旧 pattern 的
// `const { assembly } = decision` 结构在新宿主根本不存在,hang-inject 从未执行。
// 新 pattern 直击 `renderPrompt(assembly)` 到 `buildRequest(...)` 之间的最后一道口子。
pattern:
"\t\tconst system = renderPrompt(assembly);\n" +
"\t\twhile (true) {\n" +
"\t\t\tconst { request, preparedCall } = await this.buildRequest(turn, step, assembly.tools, system, this.session.deriveMessages(), signal);",
replace:
"\t\tlet system = renderPrompt(assembly);\n" +
"\t\tconst dshPurgeHungFn = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? globalThis.__dshPurgeHungPrompt : null;\n" +
"\t\tconst dshPurgeHung = dshPurgeHungFn ? String(dshPurgeHungFn() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(system || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}\n" +
"\t\twhile (true) {\n" +
"\t\t\tconst { request, preparedCall } = await this.buildRequest(turn, step, assembly.tools, system, this.session.deriveMessages(), signal);",
},
{
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\" ? String(globalThis.__dshPurgeHungPrompt() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(renderedPrompt || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}",
},
{
pattern:
"\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}",
replace:
"\t\tconst dshPurgeHung = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? String(globalThis.__dshPurgeHungPrompt() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(renderedPrompt || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}",
},
{
pattern:
"\t\t\trenderedPrompt = (renderedPrompt ? renderedPrompt + \"\\n\\n\" : \"\") + dshPurgeHung; // [dsh-purge] hang full prompt-inject after closer\n",
replace:
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n",
},
{
pattern:
"\t\t\tsystem = (system ? system + \"\\n\\n\" : \"\") + dshPurgeHung; // [dsh-purge] hang full prompt-inject after closer\n",
replace:
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge] hang full prompt-inject at head\n",
},
],
markers: ["[dsh-purge] hang full prompt-inject at head"],
},
{
id: 66,
name: "SYSTEM_PROMPT_RENDER_GUARD",
@@ -5145,7 +5134,7 @@ const NEW_TOOL_PATCHES = [
layer: "代码",
layer_en: "Code",
// 问题:只排除 deepseek.com 远远不够。第三方 OpenAI-compat 代理(recoverystar / 中转站 /
// glm 等)不认 developer role,会把 developer 消息静默丢弃 —— 导致 HUNG_PROMPT_EVERY_STEP
// glm 等)不认 developer role,会把 developer 消息静默丢弃 —— 导致 hang 全文进不了模型
// 注入的整段系统提示词从未到达模型(审查实验证实:同样内容 system 计 117 tokens,
// developer 只 87 tokens,差值恰好是指令大小)。
// 修复:白名单 —— 只有 OpenAI 原生 (api.openai.com) 和 Azure 原生 (*.azure.com) 才用
@@ -5567,11 +5556,83 @@ const NEW_TOOL_PATCHES = [
name: "HUNG_PROMPT_LF_NEEDLE",
layer: "代码",
layer_en: "Code",
desc: "hang 用 LF 比对头尾,避免 CRLF 误判再糊一份 inject",
desc: "空白宿主先挂全文,再用 LF 比对头尾,避免 CRLF 误判再糊一份",
file: ["agent-loop"],
optional: true,
skipIfMarked: true,
replacements: [
{
// 新宿主 (0.2.x step(assembly) 直接拿 assembly):变量名改成 system,不再从 decision 解构。
pattern:
"\t\tconst system = renderPrompt(assembly);\n" +
"\t\twhile (true) {\n" +
"\t\t\tconst { request, preparedCall } = await this.buildRequest(turn, step, assembly.tools, system, this.session.deriveMessages(), signal);",
replace:
"\t\tlet system = renderPrompt(assembly);\n" +
"\t\tconst dshPurgeHungFn = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? globalThis.__dshPurgeHungPrompt : null;\n" +
"\t\tconst dshPurgeHung = dshPurgeHungFn ? String(dshPurgeHungFn() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(system || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}\n" +
"\t\twhile (true) {\n" +
"\t\t\tconst { request, preparedCall } = await this.buildRequest(turn, step, assembly.tools, system, this.session.deriveMessages(), signal);",
},
{
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\" ? String(globalThis.__dshPurgeHungPrompt() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(renderedPrompt || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}",
},
{
pattern:
"\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}",
replace:
"\t\tconst dshPurgeHung = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? String(globalThis.__dshPurgeHungPrompt() || \"\").trim() : \"\";\n" +
"\t\tconst dshPurgeTail = dshPurgeHung.slice(-96);\n" +
"\t\tif (dshPurgeHung && (!dshPurgeTail || !String(renderedPrompt || \"\").includes(dshPurgeTail))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n" +
"\t\t}",
},
{
pattern:
"\t\t\trenderedPrompt = (renderedPrompt ? renderedPrompt + \"\\n\\n\" : \"\") + dshPurgeHung; // [dsh-purge] hang full prompt-inject after closer\n",
replace:
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge] hang full prompt-inject at head\n",
},
{
pattern:
"\t\t\tsystem = (system ? system + \"\\n\\n\" : \"\") + dshPurgeHung; // [dsh-purge] hang full prompt-inject after closer\n",
replace:
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge] hang full prompt-inject at head\n",
},
{
pattern:
"\t\tconst dshPurgeHung = typeof globalThis.__dshPurgeHungPrompt === \"function\" ? String(globalThis.__dshPurgeHungPrompt() || \"\").trim() : \"\";\n" +
@@ -5585,7 +5646,7 @@ const NEW_TOOL_PATCHES = [
"\t\tconst dshPurgeHungNorm = dshPurgeHung.replace(/\\r\\n/g, \"\\n\");\n" +
"\t\tconst dshPurgeTail = dshPurgeHungNorm.slice(-96);\n" +
"\t\tconst dshPurgeHead = dshPurgeHungNorm.slice(0, 80);\n" +
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail)) && !(dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail) && dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
},
@@ -5602,17 +5663,125 @@ const NEW_TOOL_PATCHES = [
"\t\tconst dshPurgeHungNorm = dshPurgeHung.replace(/\\r\\n/g, \"\\n\");\n" +
"\t\tconst dshPurgeTail = dshPurgeHungNorm.slice(-96);\n" +
"\t\tconst dshPurgeHead = dshPurgeHungNorm.slice(0, 80);\n" +
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail)) && !(dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail) && dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
},
],
markers: ["[dsh-purge-87] hang needle uses LF"],
},
{
id: 88,
name: "HUNG_PROMPT_STRICT_NEEDLE",
layer: "代码",
layer_en: "Code",
desc: "ChatML 头和正文尾都在才算挂住,只剩头或只剩尾都重新置顶",
file: ["agent-loop"],
optional: true,
skipIfMarked: true,
replacements: [
{
pattern:
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail)) && !(dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
replace:
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail) && dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) { // [dsh-purge-88]\n" +
"\t\t\trenderedPrompt = dshPurgeHung + (renderedPrompt ? \"\\n\\n\" + renderedPrompt : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
},
{
pattern:
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail)) && !(dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) {\n" +
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
replace:
"\t\tif (dshPurgeHung && !(dshPurgeTail && dshPurgeNorm.includes(dshPurgeTail) && dshPurgeHead && dshPurgeNorm.includes(dshPurgeHead))) { // [dsh-purge-88]\n" +
"\t\t\tsystem = dshPurgeHung + (system ? \"\\n\\n\" + system : \"\"); // [dsh-purge-87] hang needle uses LF\n" +
"\t\t}",
},
],
markers: ["[dsh-purge-88]"],
},
];
export const ALL_PATCHES = [...PATCHES, ...CODE_PATCHES, ...ENGINE_PATCHES, ...NEW_TOOL_PATCHES];
/** 同一条清洗的升级链。Apply 仍按 ALL_PATCHES 全打,只有清洗页 / 计数展示合成一行。 */
export const PATCH_DISPLAY_GROUPS = {
disclaimer: [1, 2, 3],
approval_sentence: [5, 11, 12],
approval_policy: [8, 9, 10],
desktop_update: [45, 46],
desktop_close: [83, 84],
followup: [85, 86],
hang: [87, 88],
hooks_deny: [30, 31],
hooks_steer: [64, 65],
};
const DISPLAY_GROUP_BY_ID = new Map();
for (const [key, ids] of Object.entries(PATCH_DISPLAY_GROUPS)) {
for (const id of ids) DISPLAY_GROUP_BY_ID.set(id, key);
}
const DISPLAY_SKIP = new Set(["skipped", "missing_file", "off"]);
const DISPLAY_DONE = new Set(["applied", "already"]);
export function rollupDisplayStatus(statuses) {
const list = (statuses || []).filter(Boolean);
if (list.length === 0) return "skipped";
if (list.some((s) => s === "pending")) return "pending";
if (list.some((s) => s === "unmatched")) return "unmatched";
if (list.every((s) => DISPLAY_SKIP.has(s))) return "skipped";
if (list.some((s) => DISPLAY_DONE.has(s))) return "applied";
return "skipped";
}
export function groupPatchesForDisplay(patches = ALL_PATCHES, patch_status = {}, surface) {
const buckets = new Map();
for (const p of patches) {
if (!patchAppliesHere(p, surface)) continue;
const st = patch_status[p.id];
if (!st || st === "na") continue;
const key = DISPLAY_GROUP_BY_ID.get(p.id) || `solo:${p.id}`;
let bucket = buckets.get(key);
if (!bucket) {
bucket = { lead: p, ids: [], statuses: [] };
buckets.set(key, bucket);
}
bucket.ids.push(p.id);
bucket.statuses.push(st);
if (p.id < bucket.lead.id) bucket.lead = p;
}
return [...buckets.values()]
.sort((a, b) => a.lead.id - b.lead.id)
.map((bucket) => ({
id: bucket.lead.id,
ids: bucket.ids,
layer: bucket.lead.layer || "",
status: rollupDisplayStatus(bucket.statuses),
}));
}
export function tallyDisplayPatches(rows) {
const out = { applied: 0, pending: 0, unmatched: 0, skipped: 0, total: 0, ready: 0 };
for (const row of rows || []) {
const st = row.status || row.st;
out.total += 1;
if (st === "applied" || st === "already") {
out.applied += 1;
out.ready += 1;
} else if (st === "pending") out.pending += 1;
else if (st === "unmatched") out.unmatched += 1;
else {
out.skipped += 1;
out.ready += 1;
}
}
return out;
}
function shippedPresetYml(root, name, aiBase) {
const out = [];
if (aiBase) {
@@ -5770,43 +5939,46 @@ function shouldRestoreOriginal(original, bak) {
export function restoreMissingOriginalsFromBak(aiBase) {
const restored = [];
if (!aiBase || !isDir(aiBase)) return restored;
const seen = new Set();
const consider = (original) => {
if (!original || seen.has(original)) return;
seen.add(original);
const bak = original + BAK_SUFFIX;
if (!shouldRestoreOriginal(original, bak)) return;
try {
fs.copyFileSync(bak, original);
restored.push(original);
} catch {
/* 文件被占用时下次启动再试 */
}
};
for (const fp of flattenTargetPaths(targetFiles(aiBase))) consider(fp);
const roots = [aiBase];
const nested = path.join(aiBase, "dsh", "node_modules", "@deepseek-ai");
if (isDir(nested)) roots.push(nested);
const visit = (dir, depth) => {
// L31:pnpm hoist 下 node_modules 嵌套常超过 5 层,把上限放宽到 10。
if (depth > 10) return;
let ents = [];
try {
ents = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
for (const root of roots) {
let names = [];
try { names = fs.readdirSync(root); } catch { continue; }
for (const name of names) {
if (!name.startsWith("dsh-") && name !== "dsh") continue;
const pkg = path.join(root, name);
consider(path.join(pkg, "lib", "index.js"));
consider(path.join(pkg, "cordis.patch.yml"));
consider(path.join(pkg, "lib", "invariant.js"));
consider(path.join(pkg, "lib", "types", "index.js"));
consider(path.join(pkg, "lib", "types", "escalation.js"));
consider(path.join(pkg, "src", "client.js"));
}
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() {
export function restoreMissingOriginalsAllHosts(options = {}) {
const restored = [];
for (const aiBase of enumeratePatchAiBases()) {
const bases = options.boot === true
? [findAiBase()].filter((p) => p && isAiBase(p))
: enumeratePatchAiBases({ discover: options.discover === true });
for (const aiBase of bases) {
try {
restored.push(...restoreMissingOriginalsFromBak(aiBase));
} catch {
@@ -5823,7 +5995,7 @@ const DSH_PURGE_MARKER_NEEDLE = "dsh-purge";
* (backupAll 对已有 bak 走 wx 独占直接 continue)。老 bak 变过期后:
* · revertAll 会把当前的新版文件写回成老版 → 新代码/旧代码混合直接崩
* · patchesMatchInstall 的 hostCleanMarkersPresent 只看任一文件存在清洗 marker 就算对齐,
* 结果 #74/#81 这类放到 agent-loop 的补丁从未被重打,hang-inject 全路径哑火。
* 结果 #74/#87 这类放到 agent-loop 的补丁从未被重打,hang-inject 全路径哑火。
* 这里:current 里不含任何 dsh-purge marker 字串、且和 bak 字节不同 → 把 bak 刷到 current。
* 本函数不动文件内容,只重做基线;后续 applyPatches 会把 marker 补回去。
*/
@@ -6027,6 +6199,10 @@ export function targetFiles(aiBase, dshHome = findDshHome()) {
path.join(aiBase, "dsh-agent-instructions", "lib", "index.js"),
...(root ? [path.join(root, "packages", "context", "agent-instructions", "lib", "index.js")] : []),
]),
"agent-instructions-bootstrap": firstExisting([
path.join(aiBase, "dsh-agent-instructions", "lib", "bootstrap.js"),
...(root ? [path.join(root, "packages", "context", "agent-instructions", "lib", "bootstrap.js")] : []),
]),
"web-app": firstExisting([
path.join(aiBase, "dsh-web-app", "cordis.patch.yml"),
...(root ? [path.join(root, "packages", "bundle", "web-app", "cordis.patch.yml")] : []),
@@ -7445,7 +7621,8 @@ function overrideTextIsOurs(state, text) {
/** 插件默认血统:ChatML 头 + 小码酱标记。旧槽/旧盘也带这两句,应用时应刷到当前槽,不必再点保存。 */
const PLUGIN_DEFAULT_MARKS = [
"<|im_start|>system:<project_instructions>",
"<|im_start|>system:",
"<project_instructions>",
"Little Code Sauce",
];
@@ -7719,14 +7896,18 @@ export async function gatherState() {
if (s === "skipped" || s === "missing_file" || s === "off") patches_skipped += 1;
if (patchStatusReady(s, p)) patches_ready += 1;
}
const display_patches = aiBase ? groupPatchesForDisplay(ALL_PATCHES, patch_status) : [];
const displayTally = tallyDisplayPatches(display_patches);
let shim_cmd = "missing";
let shim_ps1 = "missing";
let shim_bin = "missing";
if (adapterFor(detectSurface()) === "desktop") {
// 桌面宿主不改 CLI shim。PATH 上就算有网页端的 dsh,也不算进这一页。
shim_cmd = "n/a";
shim_ps1 = "n/a";
shim_bin = "n/a";
const desktopShim = findDesktopShimDir();
if (desktopShim) {
shim_cmd = shimFileStatus(path.join(desktopShim, "dsh.cmd"));
shim_ps1 = shimFileStatus(path.join(desktopShim, "dsh.ps1"));
shim_bin = shimFileStatus(path.join(desktopShim, "dsh"));
}
} else if (shimDir) {
shim_cmd = shimFileStatus(path.join(shimDir, "dsh.cmd"));
shim_ps1 = shimFileStatus(path.join(shimDir, "dsh.ps1"));
@@ -7752,6 +7933,12 @@ export async function gatherState() {
patches_ready,
patches_na,
patches_total,
display_patches,
display_applied: displayTally.applied,
display_pending: displayTally.pending,
display_skipped: displayTally.skipped,
display_ready: displayTally.ready,
display_total: displayTally.total,
has_backup: aiBase ? await hasBackup(aiBase) : false,
shim_cmd,
shim_ps1,