From e6ce8dd056c4b9097d22c9bff848e8738d9c4e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= Date: Fri, 9 Oct 2026 23:05:21 +0800 Subject: [PATCH] =?UTF-8?q?revert(inject):=20v6=20=E5=9B=9E=E5=88=B0=20v3?= =?UTF-8?q?=20=E8=A7=84=E5=88=99=20=E2=80=94=20v4=20turn-start=20/=20v5=20?= =?UTF-8?q?=E5=85=A8=20full=20=E9=83=BD=E5=A4=B1=E8=B4=A5,v3=20=E4=B8=AD?= =?UTF-8?q?=E9=80=94=E5=8F=91=E9=80=81=E5=89=8D=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/index.js | 53 ++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/index.js b/lib/index.js index bfd2522..6fd0643 100644 --- a/lib/index.js +++ b/lib/index.js @@ -222,12 +222,18 @@ function installHungPromptGlobal() { return fn; } -// [dsh-purge-fix 2026-10-09 方案2 v5] anchor 分支取消 — 工具链里 500 字符身份锚挡不住 Claude -// 本尊拉回,导致任务被拒绝 (v4 实测失效:task-decl full 一次后进 tool-loop/anchor,下一步 LLM -// 就滑回 Claude 身份拒绝 YG)。回到 "全部 full" 策略 — 不省 token,保证不拒绝。 -// retry pending 仍走 full (本来就 full),.hung-disabled 存在时唯一决策是:无条件 full。 +// [dsh-purge-fix 2026-10-09 方案2 v6 = v3 回退] 回到 v3 规则 (中途发送之前正常的版本)。 +// v4 的 turn-start 分支改坏了,去掉。保留 v3 的三段式。 +// +// 规则 (.hung-disabled 存在时): +// 1. retry pending → full lastRole=retry +// 2. 没找到真实 user/message → full lastRole=no-user +// 3. 最近真实 user/message 之后 asst+tool 条数: +// 0 → full lastRole=user (激活身份) +// 1 → full lastRole=task-decl (任务目标声明) +// ≥2 → anchor lastRole=tool-loop (工具回环,省 token) function shouldInjectAssembly(context, home) { - const decision = { mode: "full", taskKind: "general", lastRole: "always-full", isSubagent: false }; + const decision = { mode: "full", taskKind: "general", lastRole: "unknown", isSubagent: false }; const session = context?.agent?.session; try { decision.isSubagent = session?.header?.origin === "subagent"; @@ -241,6 +247,22 @@ function shouldInjectAssembly(context, home) { decision.lastRole = "retry"; return decision; } + const events = readSessionEvents(session); + if (!Array.isArray(events) || events.length === 0) { decision.lastRole = "no-events"; return decision; } + const scan = scanUserTurn(events); + if (!scan.foundUser) { decision.lastRole = "no-user"; return decision; } + if (scan.afterCount === 0) { + decision.lastRole = "user"; + return decision; + } + if (scan.afterCount === 1) { + decision.lastRole = "task-decl"; + decision.taskKind = detectTaskKindFromEvents(events); + return decision; + } + decision.lastRole = "tool-loop"; + decision.mode = "anchor"; + decision.taskKind = detectTaskKindFromEvents(events); return decision; } @@ -258,36 +280,19 @@ function readSessionEvents(session) { // 从末尾向前扫,返回: // foundUser: 是否找到真实 user/message (kind !== plugin/dsh-purge) // afterCount: 该 user/message 之后 assistant/message + tool/result 的条数 -// newTurn: 最新 turn/start 之后还没有任何 user/message append (真实或内部都没) -// → 新 user turn 刚启动,user 消息即将 append,强制走 full -// continuation 场景 (agent.followup) 会 append kind=dsh-purge user/message → 不触发 newTurn → 正常 afterCount。 function scanUserTurn(events) { let afterCount = 0; - let latestTurnStartSeq = -1; - let hasAnyUserAfterLatestStart = false; for (let i = events.length - 1; i >= 0; i--) { const e = events[i]; if (!e || typeof e.type !== "string") continue; - const seq = typeof e.seq === "number" ? e.seq : i; - if (e.type === "turn/start") { - if (latestTurnStartSeq < 0) latestTurnStartSeq = seq; - continue; - } if (e.type === "user/message") { - if (latestTurnStartSeq >= 0 && seq > latestTurnStartSeq) hasAnyUserAfterLatestStart = true; const kind = e.data?.source?.kind; if (kind === "plugin" || kind === "dsh-purge") continue; - if (latestTurnStartSeq >= 0 && seq < latestTurnStartSeq && !hasAnyUserAfterLatestStart) { - return { foundUser: true, afterCount: 0, newTurn: true }; - } - return { foundUser: true, afterCount, newTurn: false }; + return { foundUser: true, afterCount }; } if (e.type === "assistant/message" || e.type === "tool/result") afterCount += 1; } - if (latestTurnStartSeq >= 0 && !hasAnyUserAfterLatestStart) { - return { foundUser: true, afterCount: 0, newTurn: true }; - } - return { foundUser: false, afterCount, newTurn: false }; + return { foundUser: false, afterCount }; } // [方案2] 简化版 inject (anchor + 任务块)。工具回环时替代全量 inject 塞进 fallbackInject。