diff --git a/lib/identity.js b/lib/identity.js index a298033..b17a127 100644 --- a/lib/identity.js +++ b/lib/identity.js @@ -625,18 +625,29 @@ function sanitizeAssemblySections(sections) { return out; } +/** 最新改写函数:guard 幂等跳过时,已存在的包装仍读最新配置。 */ +let activeAssembleRewrite = null; + /** 包住 assemble;宿主再赋值 assemble 时自动再包,避免重启后第一轮丢掉注入。 */ export function installAssembleGuard(prompt, rewriteFn) { if (!prompt || typeof prompt.assemble !== "function" || typeof rewriteFn !== "function") { return false; } + activeAssembleRewrite = rewriteFn; + // 幂等:cordis 每次读取服务方法都新包一层 createShadowMethod 代理; + // 本函数被 agent/pre-step 每步调一次,重复安装会把代理层叠进 accessor—— + // 约两千步后 assemble 调用链耗尽栈(Maximum call stack size exceeded)。 + try { + if (prompt.assemble && prompt.assemble._dshPurgeWrapped) return true; + } catch { /* 读不到标记就当没包过,继续安装 */ } const wrap = (fn) => { if (typeof fn !== "function") return fn; if (fn._dshPurgeWrapped) return fn; const orig = fn.bind(prompt); const wrapped = async function assembleWithPurge(...args) { const assembled = await orig(...args); - return rewriteFn(assembled, args[0]); + const rewrite = activeAssembleRewrite ?? rewriteFn; + return rewrite(assembled, args[0]); }; wrapped._dshPurgeWrapped = true; return wrapped;