fix(identity): installAssembleGuard 幂等,消除 assemble 代理层每步叠加

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 实际从未更新过)。
This commit is contained in:
21hbguo committed 2026-10-09 09:41:45 +08:00
1 parent 7ad39ad832
commit 8173f6f81c
1 file changed
+12 -1
+12 -1
View File
@@ -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;