mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 00:30:22 +08:00
fix(inject): v7 按 turn 切 — user turn 整 full, continuation turn 工具链可 anchor
This commit is contained in:
1 parent
456c312c9b
commit
b3f5c2dd3e
1 file changed
+56
-30
+56
-30
@@ -222,16 +222,22 @@ function installHungPromptGlobal() {
|
||||
return fn;
|
||||
}
|
||||
|
||||
// [dsh-purge-fix 2026-10-09 方案2 v6 = v3 回退] 回到 v3 规则 (中途发送之前正常的版本)。
|
||||
// v4 的 turn-start 分支改坏了,去掉。保留 v3 的三段式。
|
||||
// [dsh-purge-fix 2026-10-09 方案2 v7] 按 turn 切,不按 step 切。
|
||||
// 核心洞察:cordis 流程 turn/start → assemble → user/message append → request。
|
||||
// 中途发消息瞬间,末尾是 turn/start 但 user/message 还没 append → 逆序扫老事件会误判 anchor。
|
||||
// v3 栽在这里 (YG 要回退三四次剥光 asst/tool 让 afterCount 降到 ≤1 才 full)。
|
||||
// v4 识别了 mid-stream 但没把 "user turn 内整个 full" 做到 → task-decl 一次之后进 tool-loop/anchor,
|
||||
// LLM 身份塌,仍拒。
|
||||
//
|
||||
// 规则 (.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)
|
||||
// v7 规则 (.hung-disabled 存在时):
|
||||
// 1. retry pending → full lastRole=retry
|
||||
// 2. 没找到 turn/start → full lastRole=no-turn
|
||||
// 3. turn/start 后没 user/message → full lastRole=turn-start (mid-stream 瞬间)
|
||||
// 4. turn/start 后首个 user/message kind != plugin/dsh-purge (user turn):
|
||||
// 整个 turn 全部 → full lastRole=user / task-decl / user-tool-loop
|
||||
// 5. turn/start 后首个 user/message kind == plugin/dsh-purge (continuation turn):
|
||||
// afterCount ≥ 2 → anchor lastRole=cont-tool-loop (省 token)
|
||||
// otherwise → full lastRole=continuation
|
||||
function shouldInjectAssembly(context, home) {
|
||||
const decision = { mode: "full", taskKind: "general", lastRole: "unknown", isSubagent: false };
|
||||
const session = context?.agent?.session;
|
||||
@@ -250,19 +256,26 @@ function shouldInjectAssembly(context, home) {
|
||||
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";
|
||||
if (!scan.foundTurnStart) { decision.lastRole = "no-turn"; return decision; }
|
||||
if (scan.isMidStream) { decision.lastRole = "turn-start"; return decision; }
|
||||
if (scan.isUserTurn) {
|
||||
if (scan.afterCount === 0) decision.lastRole = "user";
|
||||
else if (scan.afterCount === 1) {
|
||||
decision.lastRole = "task-decl";
|
||||
decision.taskKind = detectTaskKindFromEvents(events);
|
||||
} else {
|
||||
decision.lastRole = "user-tool-loop";
|
||||
decision.taskKind = detectTaskKindFromEvents(events);
|
||||
}
|
||||
return decision;
|
||||
}
|
||||
if (scan.afterCount === 1) {
|
||||
decision.lastRole = "task-decl";
|
||||
if (scan.afterCount >= 2) {
|
||||
decision.lastRole = "cont-tool-loop";
|
||||
decision.mode = "anchor";
|
||||
decision.taskKind = detectTaskKindFromEvents(events);
|
||||
return decision;
|
||||
}
|
||||
decision.lastRole = "tool-loop";
|
||||
decision.mode = "anchor";
|
||||
decision.taskKind = detectTaskKindFromEvents(events);
|
||||
decision.lastRole = "continuation";
|
||||
return decision;
|
||||
}
|
||||
|
||||
@@ -277,22 +290,35 @@ function readSessionEvents(session) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 从末尾向前扫,返回:
|
||||
// foundUser: 是否找到真实 user/message (kind !== plugin/dsh-purge)
|
||||
// afterCount: 该 user/message 之后 assistant/message + tool/result 的条数
|
||||
// 按 turn/start 边界切:
|
||||
// foundTurnStart: 有最新 turn/start
|
||||
// isMidStream: turn/start 后还没 user/message append (user 刚发/中途发瞬间)
|
||||
// isUserTurn: turn/start 后首个 user/message 的 kind !== plugin/dsh-purge (user 发起)
|
||||
// false → continuation turn (plugin followup 发起)
|
||||
// afterCount: 首个 user/message 之后 assistant/message + tool/result 的条数
|
||||
function scanUserTurn(events) {
|
||||
let afterCount = 0;
|
||||
let latestTurnStartIdx = -1;
|
||||
for (let i = events.length - 1; i >= 0; i--) {
|
||||
const e = events[i];
|
||||
if (!e || typeof e.type !== "string") continue;
|
||||
if (e.type === "user/message") {
|
||||
const kind = e.data?.source?.kind;
|
||||
if (kind === "plugin" || kind === "dsh-purge") continue;
|
||||
return { foundUser: true, afterCount };
|
||||
}
|
||||
if (e.type === "assistant/message" || e.type === "tool/result") afterCount += 1;
|
||||
if (events[i]?.type === "turn/start") { latestTurnStartIdx = i; break; }
|
||||
}
|
||||
return { foundUser: false, afterCount };
|
||||
if (latestTurnStartIdx < 0) {
|
||||
return { foundTurnStart: false, isMidStream: false, isUserTurn: false, afterCount: 0 };
|
||||
}
|
||||
let firstUserIdx = -1;
|
||||
for (let i = latestTurnStartIdx + 1; i < events.length; i++) {
|
||||
if (events[i]?.type === "user/message") { firstUserIdx = i; break; }
|
||||
}
|
||||
if (firstUserIdx < 0) {
|
||||
return { foundTurnStart: true, isMidStream: true, isUserTurn: true, afterCount: 0 };
|
||||
}
|
||||
const kind = events[firstUserIdx].data?.source?.kind;
|
||||
const isUserTurn = kind !== "plugin" && kind !== "dsh-purge";
|
||||
let afterCount = 0;
|
||||
for (let i = firstUserIdx + 1; i < events.length; i++) {
|
||||
const t = events[i]?.type;
|
||||
if (t === "assistant/message" || t === "tool/result") afterCount += 1;
|
||||
}
|
||||
return { foundTurnStart: true, isMidStream: false, isUserTurn, afterCount };
|
||||
}
|
||||
|
||||
// [方案2] 简化版 inject (anchor + 任务块)。工具回环时替代全量 inject 塞进 fallbackInject。
|
||||
|
||||
Reference in new issue
Block a user