fix(hang+leak): 7 文件修 assemble throw / crash listener / Map FIFO

卡死路径三连 (UI 发不出 / 任务到一半停):
1. index.js:system-prompt/assemble handler 的 await next() 加 try/catch。
   下游 waterfall 任一环节 throw 就把整条 assemble 弄死,原地卡住发不出去。
2. identity.js:installAssembleGuard wrapped 的 await orig(...) 加 try/catch。
   宿主原生 assemble throw 后 sections.map 会崩,症状就是"任务到一半苏醒"
   "连保存都救不了"的拒答 - 吞掉返回空装配兜底。
3. desktop.js:scheduleAsarSwapRestart 的 PowerShell spawn 加 'error' listener。
   没 handler 时 Node 当 uncaughtException 直接把宿主崩掉(2026-10-09 crash
   log at ChildProcess.<anonymous> main.js:3725 根因)。

启动卡顿 / ReDoS:
4. desktop.js:PROCESS_EXE_SCRIPT execFileSync timeout 15s → 6s。
   Win32_Process 枚举 1-3s 够用, 15s 把启动期 UI 卡 15s 太狠。
5. net-scope.js:ATTACK 正则加 100KB input cap。零宽环视 + 超长输入
   (贴代码/日志)会 ReDoS 把事件循环卡死。

内存积累(cordis 无 session/removed 事件可挂, FIFO 兜底):
6. continue-retry.js:createTracker bySession 加 CAP=200 / EVICT=50。
7. refusal-recover.js:bySession 加 CAP=200 / EVICT=50。
8. redteam/role-section.js:roleBySession 模块级 Map 加 CAP=400 / EVICT=100。

全局 handle 清理:
9. index.js:installHungPromptGlobal 返回 fn, effect cleanup 带 identity
   校验 delete __dshPurgeHungPrompt, 避免热重载时旧 cleanup 挖掉新实例。

版本号保持 1.1.67, 不发新版。
This commit is contained in:
叫我小杨同学 committed 2026-10-09 14:06:07 +08:00
1 parent 7eb82f45a6
commit a99fba5e86
7 files changed
+97 -9

No files matched your search

+25 -4
View File
@@ -198,13 +198,15 @@ function refreshHungOperator(dshHome = core.findDshHome()) {
}
function installHungPromptGlobal() {
globalThis.__dshPurgeHungPrompt = () => {
const fn = () => {
try {
return refreshHungOperator(core.findDshHome()).text || "";
} catch {
return core.hungOperatorPrompt() || "";
}
};
globalThis.__dshPurgeHungPrompt = fn;
return fn;
}
function injectSnapshot(dshHome = core.findDshHome()) {
@@ -502,8 +504,23 @@ function installIdentityOverride(ctx, config = {}) {
ctx.on("system-prompt/assemble", async (...args) => {
const [assembly, context] = args;
const next = args.length >= 3 ? args[args.length - 1] : null;
const assembled = typeof next === "function" ? await next() : assembly;
return rewriteLiveAssembly(config, assembled, context);
let assembled = assembly;
if (typeof next === "function") {
try {
assembled = await next();
} catch (err) {
// 下游 waterfall 任一环节抛就会把整条 assemble 弄死,UI 卡住发不出去。
// 吞掉改用上游装配兜底,inject 保底能挂上。
try { console.warn("[dsh-purge] system-prompt/assemble waterfall rejected:", err?.message || err); } catch {}
assembled = assembly;
}
}
try {
return rewriteLiveAssembly(config, assembled, context);
} catch (err) {
try { console.warn("[dsh-purge] rewriteLiveAssembly failed:", err?.message || err); } catch {}
return assembled;
}
}, { global: true, prepend: true });
}
// 切换预设会换掉 assemble。pre-step 瀑布在 assemble 之后才跑,第一次发送包不上。
@@ -522,7 +539,7 @@ function installIdentityOverride(ctx, config = {}) {
return prompt || null;
};
globalThis.__dshPurgePrepareAssemble = prepareAssemble;
installHungPromptGlobal();
const hungFn = installHungPromptGlobal();
// complete:true 会在 waterfall 之后盖掉 sections;包住 assemble 返回值,每轮都补回注入。
wrapAssemble();
// guard 需要在每次 cordis 换 systemPrompt 代理时重挂(session 内切预设、subagent fork、
@@ -571,6 +588,10 @@ function installIdentityOverride(ctx, config = {}) {
if (globalThis.__dshPurgePrepareAssemble === prepareAssemble) {
delete globalThis.__dshPurgePrepareAssemble;
}
// identity 校验再删,免得热重载抢到新实例又被旧 cleanup 挖掉。
if (globalThis.__dshPurgeHungPrompt === hungFn) {
delete globalThis.__dshPurgeHungPrompt;
}
});
} catch { /* 无 effect 时靠 unref + 下次 apply 自覆盖 */ }
}