fix(identity+index): 二轮苏醒拒答根因 + 高频 inject disk IO 节流

二轮 inject 失效根因三连:
1. refreshHungOperator 空值兜底 - resolved.text 为空时走加密默认兜住,
   避免二轮 fallbackInject="" → collectInjectText 回退 purge[] 残留 → inject 失效。
2. refreshHungOperator mtime+200ms 节流 - 400ms 轮询 + pre-step + preset-selected
   + readInjectFallback 同秒 10+ 次 syncPromptInjectOnDiskSync + resolveInjectText
   + 3×hashOverride + 模块级赋值, 严重 disk/CPU 消耗。
3. rewritePromptAssembly 不原地改写 assembled.sections -
   宿主若缓存 assembled, 二轮进来已被我们污染过; 改返回新对象,
   下轮进入 sections 保证来源干净。

内存积累 / listener 叠层:
4. wrapDisposers 单例化 - 原本 400ms + pre-step 每次 push 新 noop closure 入 Set。
   改为 WeakSet(prompt) 守 trackDisposer, 只对真正新 prompt 对象记一条 disposer。
5. agent-preset/selected / agent/pre-step / session/created 三个 ctx.on 加
   flag guard, installIdentityOverride 若被二次 apply 不再叠层。
   ctx.effect cleanup 时顺手清 flag, 保留下次 apply 重挂的能力。

版本号保持 1.1.67, 不发新版。
This commit is contained in:
叫我小杨同学 committed 2026-10-09 13:52:58 +08:00
1 parent 90d0f7b702
commit f746618988
2 files changed
+96 -39

No files matched your search

+23 -13
View File
@@ -629,11 +629,14 @@ export function installAssembleGuard(prompt, rewriteFn) {
desc = undefined;
}
const bindSlot = (slot) => {
const prev = slot.rewrite;
slot.rewrite = rewriteFn;
return function dispose() {
if (slot.rewrite === rewriteFn) slot.rewrite = prev || null;
};
// dispose 必须 noop:ctx.effect cleanup 在 cordis 正常生命周期中也会触发
// (child ctx scope 结束 / session 边界 / 预设切换),一旦执行链式 dispose,
// slot.rewrite 会被回滚到 null → assemble 走原生 → inject 失效 → 用户看到
// "任务到一半就苏醒""保存都救不了"的拒答。
// 不清 slot.rewrite 是安全的:下次 install 直接覆盖,prompt 对象被 GC 时
// 整个 slot 一起回收。
return function dispose() { /* noop */ };
};
if (desc && typeof desc.get === "function" && desc.get[ASSEMBLE_GUARD]) {
return bindSlot(desc.get[ASSEMBLE_GUARD]);
@@ -673,7 +676,8 @@ export function installAssembleGuard(prompt, rewriteFn) {
return wrapped;
};
const dispose = function dispose() {
if (slot.rewrite === rewriteFn) slot.rewrite = null;
// 同 bindSlot:不清 slot.rewrite,避免 ctx.effect cleanup 时 inject 失效。
// 下次 install 会直接覆盖 slot.rewrite,prompt 对象被 GC 时 slot 一起回收。
};
try {
let current = wrap(original);
@@ -718,9 +722,12 @@ export function installAssembleGuard(prompt, rewriteFn) {
export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) {
if (!assembled || !Array.isArray(assembled.sections)) return assembled;
const opts = normalizeRewriteOptions(fallbackInject, options);
// 不原地改写:宿主若复用同一个 assembled 对象,下一轮进来已被我们污染过(多轮 head
// dsh-purge / CAPABILITY_SECTION 残留)。返回新对象,传入对象保持原状。
const originalSections = assembled.sections;
const rest = [];
const purge = [];
for (const section of assembled.sections) {
for (const section of originalSections) {
if (!section || section.name === "harness:identity") continue;
if (section.name === "dsh-purge:identity") continue;
if (opts.dropMnemon && isMnemonSection(section.name)) continue;
@@ -767,25 +774,28 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
? insertRoleSection(insertCardSection(sections, opts.cardText), opts.roleText)
: withOps;
}
assembled.sections = sanitizeAssemblySections(leadWithInject(sections, injectText, protocol));
let outSections = sanitizeAssemblySections(leadWithInject(sections, injectText, protocol));
if (injectText) {
const needle = injectText.slice(0, 80);
const has = assembled.sections.some((section) => (
const has = outSections.some((section) => (
typeof section?.text === "string" && needle && section.text.includes(needle)
));
if (!has) {
assembled.sections = [
outSections = [
{
name: "dsh-purge",
text: injectText,
interpolate: false,
order: GLOBAL_PROMPT_ORDER + 1,
},
...assembled.sections.filter((section) => section && section.name !== "dsh-purge"),
...outSections.filter((section) => section && section.name !== "dsh-purge"),
];
}
}
if (!Array.isArray(assembled.contexts)) assembled.contexts = [];
if (!Array.isArray(assembled.tools)) assembled.tools = [];
return assembled;
return {
...assembled,
sections: outSections,
contexts: Array.isArray(assembled.contexts) ? assembled.contexts : [],
tools: Array.isArray(assembled.tools) ? assembled.tools : [],
};
}