From 929282144983ee171ebe26d920718615f676a8f9 Mon Sep 17 00:00:00 2001 From: 21hbguo <139569698+21hbguo@users.noreply.github.com> Date: Fri, 9 Oct 2026 09:41:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(identity):=20installAssembleGuard=20?= =?UTF-8?q?=E5=B9=82=E7=AD=89=EF=BC=8C=E6=B6=88=E9=99=A4=20assemble=20?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=B1=82=E6=AF=8F=E6=AD=A5=E5=8F=A0=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prompt.assemble 经 cordis traceable 代理读取时每次都新包一层 createShadowMethod 代理。installAssembleGuard 每次执行 `current = wrap(prompt.assemble)` 会把新代理无条件叠进 accessor—— wrap() 的 _dshPurgeWrapped 检查只能阻止重复包装函数本体,挡不住 代理层累积。该函数被 agent/pre-step 每步调用,约 2000 步后 systemPrompt.assemble 的调用链在真实 assemble 之前就耗尽 V8 栈 (Maximum call stack size exceeded,所有 turn 秒败)。 修复:检测到 prompt.assemble._dshPurgeWrapped 已存在时直接返回; 改写闭包改读模块级 activeAssembleRewrite,保持「最新配置生效」语义 (原实现下 wrapped 创建后 rewriteFn 实际从未更新过)。 --- lib/identity.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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;