dsh-cot-anchor 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/client.js +35 -1
  2. package/lib/index.js +337 -52
  3. package/package.json +1 -1
package/lib/client.js CHANGED
@@ -584,6 +584,11 @@ window.__ModuleLoader__.load({
584
584
  'div',
585
585
  { style: { flex: 1, minWidth: 0 } },
586
586
  h('div', { style: { color: THEME.text, fontSize: 13 } }, entry.label),
587
+ h(
588
+ 'div',
589
+ { style: { color: THEME.textTertiary, fontSize: 10, marginTop: 1, fontFamily: 'monospace' } },
590
+ '源码键:' + entry.key
591
+ ),
587
592
  entry.hint
588
593
  ? h('div', { style: { color: THEME.textTertiary, fontSize: 12, marginTop: 2, lineHeight: 1.5 } }, entry.hint)
589
594
  : null,
@@ -614,6 +619,12 @@ window.__ModuleLoader__.load({
614
619
  : null,
615
620
 
616
621
  groups.map(function (group) {
622
+ var normal = []
623
+ var advanced = []
624
+ group.entries.forEach(function (e) {
625
+ if (e.advanced === true) advanced.push(e)
626
+ else normal.push(e)
627
+ })
617
628
  return h(
618
629
  'div',
619
630
  { key: group.name, style: { marginBottom: 18 } },
@@ -632,7 +643,30 @@ window.__ModuleLoader__.load({
632
643
  padding: '0 12px'
633
644
  }
634
645
  },
635
- group.entries.map(renderRow)
646
+ normal.map(renderRow),
647
+ advanced.length
648
+ ? h(
649
+ 'details',
650
+ { key: group.name + '-adv', style: { padding: '6px 0' } },
651
+ h(
652
+ 'summary',
653
+ {
654
+ style: {
655
+ fontSize: 12,
656
+ color: THEME.textSecondary,
657
+ cursor: 'pointer',
658
+ userSelect: 'none'
659
+ }
660
+ },
661
+ '高级参数(' + advanced.length + ' 项)'
662
+ ),
663
+ h(
664
+ 'div',
665
+ { style: { borderTop: '1px solid ' + THEME.border, marginTop: 4 } },
666
+ advanced.map(renderRow)
667
+ )
668
+ )
669
+ : null
636
670
  )
637
671
  )
638
672
  }),
package/lib/index.js CHANGED
@@ -1030,7 +1030,15 @@ function wantsSoftCut(fullVisibleText, minReasoningChars) {
1030
1030
  // execution announcement (write/edit/refactor/run/…) is the phase-B cut
1031
1031
  // this detector targets. The matched verb is prepended so the gate judges
1032
1032
  // the announced action itself, not the object that follows it.
1033
- if (transitionLeadsToInvestigation(transitionVerb + tail.slice(transitionEnd))) return false;
1033
+ //
1034
+ // 修复④:保留单次调查前缀放行(设计初衷:放走一次性的"我先看看配置"),
1035
+ // 但若最近尾窗内同一调查前缀重复 ≥ INVESTIGATIVE_PREFIX_REPEAT_MAX 次
1036
+ // 且动作动词远多于执行动词(典型空转特征),则收紧为可切。
1037
+ const tightenInfo = shouldTightenInvestigativePrefix(fullVisibleText);
1038
+ if (transitionLeadsToInvestigation(transitionVerb + tail.slice(transitionEnd))) {
1039
+ if (!tightenInfo.triggered) return false;
1040
+ // 触发收紧:跳过 investigation gate,让 transition 判定继续通过
1041
+ }
1034
1042
 
1035
1043
  // The transition must be in the LAST sentence-ish chunk, not buried mid-answer.
1036
1044
  const lastBreak = Math.max(tail.lastIndexOf("。"), tail.lastIndexOf("!"), tail.lastIndexOf("?"), tail.lastIndexOf("\n"), tail.lastIndexOf("."), tail.lastIndexOf(";"), tail.lastIndexOf(";"));
@@ -1167,6 +1175,85 @@ const CASCADING_PATTERNS = [
1167
1175
  */
1168
1176
  const INVESTIGATIVE_TRANSITION_PREFIXES = ["我先", "接下来我", "接下来", "下一步我", "下一步", "下面我", "下面", "现在开始", "现在就", "然后我", "然后", "我接下来", "那就先", "先将"];
1169
1177
 
1178
+ // ── 修复④:transition 调查前缀分组复核 ──
1179
+ // INVESTIGATIVE_TRANSITION_PREFIXES 单次放行(放走"我先看看配置"),但"让我 curl 一下"
1180
+ // 被它被动放行后若反复出现(≥ 前缀重复上限)且无执行动词,就收紧为可切。
1181
+ // 这些前缀在泄漏样本 tail3000 里出现 25 次("我先"15 + "然后"8 + 其他 2),动作动词
1182
+ // 计数 53,执行动词计数仅 1,明显失衡。
1183
+ const INVESTIGATIVE_PREFIX_REPEAT_MAX = 6; // 最近尾窗内同一前缀命中 ≥ 此值触发收紧
1184
+ const INVESTIGATIVE_PREFIX_WINDOW_CHARS = 8000; // 计数窗口与 REPEAT_WINDOW_CHARS 同阶
1185
+ const INVESTIGATIVE_EXEC_VERBS = new Set([
1186
+ "write", "edit", "rewrite", "refactor", "delete", "replace", "deploy",
1187
+ "implement", "update", "build", "run", "create", "add", "make", "fix",
1188
+ "move", "open", "创建", "写", "改", "执行", "调用", "生成", "删除",
1189
+ "替换", "重构", "部署", "更新", "编译", "跑"
1190
+ ]);
1191
+
1192
+ /**
1193
+ * 修复④:最近尾窗内 INVESTIGATIVE_TRANSITION_PREFIXES 各前缀的分组命中计数。
1194
+ * @param {string} fullVisibleText
1195
+ * @returns {{prefix:string, count:number}[]} 按 count 降序
1196
+ */
1197
+ function countInvestigativePrefixes(fullVisibleText) {
1198
+ if (typeof fullVisibleText !== "string") return [];
1199
+ const tail = fullVisibleText.slice(-INVESTIGATIVE_PREFIX_WINDOW_CHARS);
1200
+ const counts = new Map();
1201
+ for (const p of INVESTIGATIVE_TRANSITION_PREFIXES) {
1202
+ const re = new RegExp(p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
1203
+ re.lastIndex = 0;
1204
+ const m = tail.match(re);
1205
+ if (m && m.length > 0) counts.set(p, m.length);
1206
+ }
1207
+ return [...counts.entries()]
1208
+ .map(([prefix, count]) => ({ prefix, count }))
1209
+ .sort((a, b) => b.count - a.count);
1210
+ }
1211
+
1212
+ /**
1213
+ * 修复④:动作动词(tool verb)与执行动词(exec verb)计数对比。
1214
+ * 动作动词 = curl/fetch/web_fetch/访问/打开/探/查/验证/试一下;执行动词 = 写/改/创建/执行...
1215
+ * 动作动词走正则(中英文混合,带边界),执行动词同样走正则字面量覆盖 CJK + Latin。
1216
+ * @param {string} text
1217
+ * @returns {{actionCount:number, execCount:number}}
1218
+ */
1219
+ function countActionVsExecVerbs(text) {
1220
+ if (typeof text !== "string") return { actionCount: 0, execCount: 0 };
1221
+ const tail = text.slice(-CHURN_WINDOW_CHARS);
1222
+ // 动作动词:工具名(curl/fetch/web_fetch) + 执行类单字(访问/打开) + 探/查 的独立形式
1223
+ // 探/查 排除合成词中的裸字(检查/查看/探查/查核/查寻/确认/探察/探测 里的"探/查"不算独立动作动词)
1224
+ // 负向后视 排除前字 + 负向前瞻 排除后字(两边都防)
1225
+ const actionRe = /(?:\bcurl\b|\bfetch\b|\bweb[-_ ]?fetch\b|访问|打开|验证|试一下|看看|探一下|查一下|(?<![检确勘观])探(?!察|测|查|究)|(?<![检查核询])查(?!查|找|核|询|看|实))/gi;
1226
+ actionRe.lastIndex = 0;
1227
+ const actionCount = (tail.match(actionRe) || []).length;
1228
+ const execRe = /(?:\bwrite\b|\bedit\b|\brewrite\b|\brefactor\b|\bdelete\b|\breplace\b|\bdeploy\b|\bimplement\b|\bupdate\b|\bbuild\b|\brun\b|\bcreate\b|\badd\b|\bmake\b|\bfix\b|\bmove\b|创建|写|改|执行|调用|生成|删除|替换|重构|部署|更新|编译|跑)/gi;
1229
+ execRe.lastIndex = 0;
1230
+ const execCount = (tail.match(execRe) || []).length;
1231
+ return { actionCount, execCount };
1232
+ }
1233
+
1234
+ /**
1235
+ * 修复④:是否触发"调查前缀收紧"。
1236
+ * 条件:① 某调查前缀在最近窗口内重复 ≥ INVESTIGATIVE_PREFIX_REPEAT_MAX 次;
1237
+ * ② 动作动词计数 ≥ 执行动词计数 × 5(动作远多于执行,典型空转特征)。
1238
+ * 触发时 wantsSoftCut 里不再被 transitionLeadsToInvestigation 放行。
1239
+ * @param {string} fullVisibleText
1240
+ * @returns {{triggered:boolean, topPrefix:string|null, repeatCount:number, actionCount:number, execCount:number}}
1241
+ */
1242
+ function shouldTightenInvestigativePrefix(fullVisibleText) {
1243
+ const prefixCounts = countInvestigativePrefixes(fullVisibleText);
1244
+ const top = prefixCounts[0];
1245
+ const { actionCount, execCount } = countActionVsExecVerbs(fullVisibleText);
1246
+ const repeatOK = top && top.count >= INVESTIGATIVE_PREFIX_REPEAT_MAX;
1247
+ const actionDom = actionCount >= execCount * 5 && actionCount > 0;
1248
+ return {
1249
+ triggered: repeatOK && actionDom,
1250
+ topPrefix: top?.prefix ?? null,
1251
+ repeatCount: top?.count ?? 0,
1252
+ actionCount,
1253
+ execCount
1254
+ };
1255
+ }
1256
+
1170
1257
  /**
1171
1258
  * Single-character investigative verbs (Chinese) and their 2-char look-ahead
1172
1259
  * steps. A transition whose leading action is one of these ("我先读…", "下一步我看…")
@@ -1443,6 +1530,118 @@ function renderNumberRunawayAnchor(nr) {
1443
1530
  ].join("\n");
1444
1531
  }
1445
1532
 
1533
+ // ── 修复②:n-gram 语义指纹循环检测 ──
1534
+ // 现有 findRepeatingTail 找字节级相同块,措辞漂移型循环(每句数字变、动词结构不变)
1535
+ // 抓不到。本修复用数字归一 + n-gram 频率旁路,运行时零 LLM。
1536
+
1537
+ /** 修复②:短语级重复的 n-gram 粒度下限(设计文档中文 n=5,允许英文/中文混合下最小 4)。 */
1538
+ const NGram_PHRASE_MIN = 5;
1539
+ /** 修复②:高频短语最小出现次数。 */
1540
+ const NGram_MIN_COUNT = 5;
1541
+ /** 修复②:最高频短语覆盖尾窗的占比下限(≥15% 字符被同短语反复占用即判漂移循环)。 */
1542
+ const NGram_COVERAGE_RATIO = 0.05;
1543
+ /** 修复②:停用词集合,过滤普通高频词以防误报。 */
1544
+ const NGram_STOPWORDS = new Set([
1545
+ "的", "了", "是", "我", "你", "他", "她", "它", "们", "这", "那", "就", "也", "都",
1546
+ "要", "会", "能", "可以", "让", "把", "给", "对", "从", "用", "到", "在", "和", "与",
1547
+ "为", "于", "以", "及", "或", "不", "没", "有", "一", "一个", "一些", "什么", "怎么",
1548
+ "怎样", "为什么", "因为", "所以", "但是", "然而", "不过", "而且", "然后", "现在",
1549
+ "好,", "好。", "好", "那么", "接下来", "现在就",
1550
+ // 运行状态标签(LISTENING=True / PID=xxx / exit code)——泄漏样本每段都重复,
1551
+ // 但它本身不是循环证据,必须过滤让真正的循环短语(好+动词+工具名)浮出来
1552
+ "listening", "true", "false", "pid", "digit", "DIGIT", "running", "exit", "code",
1553
+ "web_fetch", "curl", "fetch", "localhost"
1554
+ ]);
1555
+
1556
+ /**
1557
+ * 修复②:数字归一 + 小写 + 去标点空白,为 n-gram 指纹做准备。
1558
+ * PID=31760 → PID=DIGIT、端口号 / 路径里的数字同样处理。
1559
+ * 用字面量 DIGIT 而不是 <D>:尖括号属于要被 strip 的标点,归一符不能被自毁。
1560
+ * 先替换数字再 strip 标点——顺序关键。
1561
+ * @param {string} text
1562
+ * @returns {string}
1563
+ */
1564
+ function normalizeForNGram(text) {
1565
+ if (typeof text !== "string") return "";
1566
+ return text
1567
+ .toLowerCase()
1568
+ .replace(/\d+/g, "DIGIT")
1569
+ .replace(/[,,。.!!??;;::""''()()【】\[\]<>《》—–\-]/g, " ")
1570
+ .replace(/\s+/g, " ")
1571
+ .trim();
1572
+ }
1573
+
1574
+ /**
1575
+ * 修复②:尾窗 n-gram 频率统计。中文按单字符滑动(n=5),英文按词滑动(n=2)。
1576
+ * 混合文本场景退化为"以字符为单位 n=5"——够用且实现简单。
1577
+ * @param {string} normText 已过 normalizeForNGram
1578
+ * @param {number} nGramSize 短语粒度
1579
+ * @returns {{maxCount:number, maxGram:string, totalGrams:number, gramCount:Map<string, number>}|null}
1580
+ */
1581
+ function computeNGramFingerprint(normText, nGramSize) {
1582
+ if (typeof normText !== "string" || normText.length < nGramSize * 4) return null;
1583
+ const gramCount = new Map();
1584
+ let totalGrams = 0;
1585
+ for (let i = 0; i <= normText.length - nGramSize; i++) {
1586
+ const gram = normText.slice(i, i + nGramSize);
1587
+ // 跳过含空白的 gram(normalize 已把标点换成空格,避免跨标点短语虚高)
1588
+ if (gram.includes(" ") || gram.includes("\n") || gram.includes("\t")) continue;
1589
+ let v = gramCount.get(gram) ?? 0;
1590
+ gramCount.set(gram, ++v);
1591
+ totalGrams++;
1592
+ }
1593
+ if (totalGrams === 0) return null;
1594
+ let maxCount = 0;
1595
+ let maxGram = "";
1596
+ for (const [g, c] of gramCount) {
1597
+ if (c > maxCount) { maxCount = c; maxGram = g; }
1598
+ }
1599
+ return { maxCount, maxGram, totalGrams, gramCount };
1600
+ }
1601
+
1602
+ /**
1603
+ * 修复②:判定是否为"语义漂移型循环"——跳过停用词 gram 后取最高频非停用 gram,
1604
+ * 其出现次数 ≥ NGram_MIN_COUNT 且覆盖占尾窗比例 ≥ NGram_COVERAGE_RATIO。
1605
+ * 与 findRepeatingTail 并行,不破坏原逻辑。
1606
+ * @param {string} fullVisibleText
1607
+ * @returns {{maxGram:string,count:number,coverage:number}|null}
1608
+ */
1609
+ function detectNGramLoop(fullVisibleText) {
1610
+ if (typeof fullVisibleText !== "string" || fullVisibleText.length < REPEAT_MIN_TEXT_CHARS) return null;
1611
+ const tail = fullVisibleText.slice(-REPEAT_WINDOW_CHARS);
1612
+ const norm = normalizeForNGram(tail);
1613
+ const fp = computeNGramFingerprint(norm, NGram_PHRASE_MIN);
1614
+ if (!fp) return null;
1615
+
1616
+ // 取最高频非停用词 gram:先排序再跳过停用词条目
1617
+ const sorted = [...fp.gramCount.entries()].sort((a, b) => b[1] - a[1]);
1618
+ let chosenGram = "";
1619
+ let chosenCount = 0;
1620
+ for (const [gram, count] of sorted) {
1621
+ if (NGram_STOPWORDS.has(gram)) continue;
1622
+ // 停用词比例守卫:若 gram 中 ≥60% 字符在停用词表里,同样跳过
1623
+ const stopCharRatio = [...gram].filter((ch) => NGram_STOPWORDS.has(ch)).length / gram.length;
1624
+ if (stopCharRatio >= 0.6) continue;
1625
+ chosenGram = gram;
1626
+ chosenCount = count;
1627
+ break;
1628
+ }
1629
+ if (!chosenGram || chosenCount < NGram_MIN_COUNT) return null;
1630
+ const coverage = chosenCount * NGram_PHRASE_MIN / tail.length;
1631
+ if (coverage < NGram_COVERAGE_RATIO) return null;
1632
+ return { maxGram: chosenGram, count: chosenCount, coverage };
1633
+ }
1634
+
1635
+ /** 修复②:n-gram 锚点。 */
1636
+ function renderNGramLoopAnchor(info) {
1637
+ return [
1638
+ `COT anchor — 语义循环(n-gram 指纹):最近尾窗里高频短语「${info.maxGram}」重复出现 ${info.count} 次(覆盖占比约 ${(info.coverage * 100).toFixed(1)}%)。`,
1639
+ "数字每轮都在变但动词结构每轮相同,说明你在同一个行动提议上打转。",
1640
+ "立即停止。要么真的发出 tool_call,要么直接跳到结果。",
1641
+ `(NGram loop: phrase "${info.maxGram}" x${info.count}, coverage ${(info.coverage * 100).toFixed(1)}%. Stop cycling.)`
1642
+ ].join("\n");
1643
+ }
1644
+
1446
1645
  /**
1447
1646
  * Text spellings that a model invents for tool calls. DSH has no parser for
1448
1647
  * any of these: a tool call only exists as a structured tool-call block
@@ -1712,6 +1911,20 @@ let CHURN_MIN_HITS = 18;
1712
1911
  /** Churn is only meaningful in an already long generation (lowered to fire sooner). */
1713
1912
  let CHURN_MIN_TEXT_CHARS = 800;
1714
1913
 
1914
+ // ── 修复①:churn 行动提议分支 ──
1915
+ // 现有 CHURN_PHRASES 全是"思维重启"(让我想想 / re-read / reconsider),
1916
+ // 漏了"行动提议"——工具动词反复出现但零 tool_call 的异常形态。
1917
+ // 阈值沿用 CHURN_MIN_HITS=18,普通技术文本行动动词命中 0-3/3000 字,泄漏样本约 75/3000。
1918
+ const CHURN_ACTION_PHRASES = [
1919
+ /让我.{0,10}?(?:curl|fetch|web[-_ ]?fetch|试一下|访问|看看|探|查|验证|打开)/g,
1920
+ /我先.{0,10}?(?:curl|fetch|web[-_ ]?fetch|试一下|访问|看看|探|查)/g,
1921
+ /现在.{0,10}?(?:curl|fetch|试一下|访问|看看)/g,
1922
+ /直接.{0,6}?(?:curl|fetch|web[-_ ]?fetch|访问|打开)/g,
1923
+ /先.{0,6}?(?:curl|fetch|web[-_ ]?fetch|试一下|看看|访问|探|查)/g,
1924
+ /(?:curl|fetch|web[-_ ]?fetch).{0,3}?(?:一下|看看|试试)/g
1925
+ ];
1926
+ const CHURN_ACTION_MIN_HITS = 18;
1927
+
1715
1928
  /**
1716
1929
  * Count restart-phrase hits in the trailing window.
1717
1930
  * @param {string} fullVisibleText
@@ -1742,6 +1955,19 @@ function detectChurn(fullVisibleText) {
1742
1955
  return { hits };
1743
1956
  }
1744
1957
 
1958
+ /**
1959
+ * 修复①:检测"行动提议型空转"——尾窗内工具动词前缀 / 后缀形态的正则计数 ≥ 阈值。
1960
+ * 与 detectChurn 并行:CHURN_PHRASES 抓思维重启,CHURN_ACTION_PHRASES 抓行动提议。
1961
+ * @param {string} fullVisibleText
1962
+ * @returns {{hits:number}|null}
1963
+ */
1964
+ function detectActionChurn(fullVisibleText) {
1965
+ if (typeof fullVisibleText !== "string" || fullVisibleText.length < CHURN_MIN_TEXT_CHARS) return null;
1966
+ const hits = countChurnHits(fullVisibleText, CHURN_ACTION_PHRASES);
1967
+ if (hits < CHURN_ACTION_MIN_HITS) return null;
1968
+ return { hits };
1969
+ }
1970
+
1745
1971
  /**
1746
1972
  * Render the anchor that breaks a churn loop.
1747
1973
  * @param {{hits:number}} churn
@@ -1756,6 +1982,20 @@ function renderChurnAnchor(churn) {
1756
1982
  ].join("\n");
1757
1983
  }
1758
1984
 
1985
+ /**
1986
+ * 修复①:行动提议型空转锚点。与 renderChurnAnchor 并列,用语明确指向"说了要调用但从未调用"。
1987
+ * @param {{hits:number}} churn
1988
+ * @returns {string}
1989
+ */
1990
+ function renderActionChurnAnchor(churn) {
1991
+ return [
1992
+ `COT anchor — 行动提议空转:最近约 ${CHURN_WINDOW_CHARS} 字里出现了 ${churn.hits} 次"让我 curl / web_fetch"这类行动提议,但全程没有实际工具调用。`,
1993
+ "立即停止空喊工具名。不要再说\"让我 curl 一下\"、\"先试试 web_fetch\"这类没有执行的提议。",
1994
+ "要么真的发出 tool_call(带完整参数),要么直接跳到结果;不要再重复同一个行动提议。",
1995
+ `(Action-proposal stall: ${churn.hits} tool-verb proposals in the last ~${CHURN_WINDOW_CHARS} chars with zero real tool_call. Stop proposing — either emit the tool_call or give the result.)`
1996
+ ].join("\n");
1997
+ }
1998
+
1759
1999
  /**
1760
2000
  * 缺陷 3 修复(2026-10-07 升级版):churn 每 turn 命中计数。键 = sessionId,值为
1761
2001
  * { lastTurn, count }:count 是本 turn handler 返回 churn cut 决策的次数。计数口径
@@ -2764,9 +3004,9 @@ const SETTINGS_SCHEMA = [
2764
3004
  hint: "思考短于此长度时不提取结论、不注入。" },
2765
3005
  { key: "maxPoints", label: "锚点最多结论条数", type: "number", min: 1, max: 8, group: "锚点内容" },
2766
3006
  { key: "maxPointChars", label: "单条结论最大字数", type: "number", min: 40, max: 1000, group: "锚点内容" },
2767
- { key: "enableLengthConservationCheck", label: "长度守恒校验", type: "boolean", group: "锚点内容",
3007
+ { key: "enableLengthConservationCheck", advanced: true, label: "长度守恒校验", type: "boolean", group: "锚点内容",
2768
3008
  hint: "注入前先核对锚点正文和每条结论逐字一致,防止摘要被截断或丢字;对不上就放弃这次注入。" },
2769
- { key: "transitionTextMode", label: "思考与正文如何分开(内核支持后生效)", type: "select", group: "锚点内容",
3009
+ { key: "transitionTextMode", advanced: true, label: "思考与正文如何分开(内核支持后生效)", type: "select", group: "锚点内容",
2770
3010
  options: [
2771
3011
  { value: "separateThreshold", label: "按字数阈值自动分开(默认)" },
2772
3012
  { value: "disable", label: "不分开,整段当作正文处理" }
@@ -2774,77 +3014,77 @@ const SETTINGS_SCHEMA = [
2774
3014
  hint: "决定怎么把模型的「思考」和「正文」区分开来。内核还没支持这一项,选什么都不会改变行为,先按默认放着即可。" },
2775
3015
  { key: "enableExecDisciplineBlock", label: "随锚点注入命令执行纪律块", type: "boolean", group: "锚点内容",
2776
3016
  hint: "默认开启。开启时会把「执行命令要守规矩」这段提醒和结论摘要一起插回上下文;关闭就完全不插这段提醒,结论摘要和其他识别、去重功能都不受影响。" },
2777
- { key: "numberRunMinTextChars", label: "数字流启用最小文本长度", type: "number", min: 100, max: 50000, group: "数字流参数" },
2778
- { key: "numberRunMinChars", label: "数字流最短跨度", type: "number", min: 50, max: 20000, group: "数字流参数" },
2779
- { key: "numberRunMaxGap", label: "数字流最大间隙", type: "number", min: 1, max: 200, group: "数字流参数",
3017
+ { key: "numberRunMinTextChars", advanced: true, label: "数字流启用最小文本长度", type: "number", min: 100, max: 50000, group: "数字流参数" },
3018
+ { key: "numberRunMinChars", advanced: true, label: "数字流最短跨度", type: "number", min: 50, max: 20000, group: "数字流参数" },
3019
+ { key: "numberRunMaxGap", advanced: true, label: "数字流最大间隙", type: "number", min: 1, max: 200, group: "数字流参数",
2780
3020
  hint: "相邻数字 token 之间允许的最大字符间隔。" },
2781
- { key: "numberRunMinDigitRatio", label: "数字流最小数字占比", type: "number", min: 0, max: 1, group: "数字流参数" },
2782
- { key: "numberRunMinIncreaseRatio", label: "数字流最小递增占比", type: "number", min: 0, max: 1, group: "数字流参数" },
3021
+ { key: "numberRunMinDigitRatio", advanced: true, label: "数字流最小数字占比", type: "number", min: 0, max: 1, group: "数字流参数" },
3022
+ { key: "numberRunMinIncreaseRatio", advanced: true, label: "数字流最小递增占比", type: "number", min: 0, max: 1, group: "数字流参数" },
2783
3023
 
2784
- { key: "repeatMinCount", label: "长块重复次数阈值", type: "number", min: 2, max: 10, group: "重复循环参数",
3024
+ { key: "repeatMinCount", advanced: true, label: "长块重复次数阈值", type: "number", min: 2, max: 10, group: "重复循环参数",
2785
3025
  hint: "长内容(超过下方\"短周期分界\")重复达到此次数即判定打转。" },
2786
- { key: "repeatMinCountShort", label: "短周期重复次数阈值", type: "number", min: 2, max: 30, group: "重复循环参数",
3026
+ { key: "repeatMinCountShort", advanced: true, label: "短周期重复次数阈值", type: "number", min: 2, max: 30, group: "重复循环参数",
2787
3027
  hint: "短内容循环需重复更多次才判定打转;调低则更激进。" },
2788
- { key: "repeatUltraShortMinCount", label: "超短周期重复次数阈值", type: "number", min: 5, max: 200, group: "重复循环参数",
3028
+ { key: "repeatUltraShortMinCount", advanced: true, label: "超短周期重复次数阈值", type: "number", min: 5, max: 200, group: "重复循环参数",
2789
3029
  hint: "周期 2~11 字的极小循环(如 `er4er4…` token 卡死)须重复到这么多次才判定;正常强调/分隔线(====、-----)不构成威胁。" },
2790
- { key: "repeatMinPeriod", label: "最小周期字数", type: "number", min: 4, max: 500, group: "重复循环参数" },
2791
- { key: "repeatShortPeriod", label: "短周期分界字数", type: "number", min: 16, max: 2000, group: "重复循环参数" },
2792
- { key: "repeatMaxPeriod", label: "最大周期字数", type: "number", min: 500, max: 50000, group: "重复循环参数" },
2793
- { key: "repeatWindowChars", label: "重复检查窗口", type: "number", min: 1000, max: 60000, group: "重复循环参数",
3030
+ { key: "repeatMinPeriod", advanced: true, label: "最小周期字数", type: "number", min: 4, max: 500, group: "重复循环参数" },
3031
+ { key: "repeatShortPeriod", advanced: true, label: "短周期分界字数", type: "number", min: 16, max: 2000, group: "重复循环参数" },
3032
+ { key: "repeatMaxPeriod", advanced: true, label: "最大周期字数", type: "number", min: 500, max: 50000, group: "重复循环参数" },
3033
+ { key: "repeatWindowChars", advanced: true, label: "重复检查窗口", type: "number", min: 1000, max: 60000, group: "重复循环参数",
2794
3034
  hint: "只在文本尾部这段长度内做周期性预筛(性能保护)。" },
2795
- { key: "repeatProbeChars", label: "周期探针长度", type: "number", min: 16, max: 2000, group: "重复循环参数" },
2796
- { key: "repeatMinTextChars", label: "启用最小文本长度", type: "number", min: 500, max: 50000, group: "重复循环参数" },
3035
+ { key: "repeatProbeChars", advanced: true, label: "周期探针长度", type: "number", min: 16, max: 2000, group: "重复循环参数" },
3036
+ { key: "repeatMinTextChars", advanced: true, label: "启用最小文本长度", type: "number", min: 500, max: 50000, group: "重复循环参数" },
2797
3037
 
2798
- { key: "churnMinHits", label: "空转词命中阈值", type: "number", min: 5, max: 100, group: "语义空转参数",
3038
+ { key: "churnMinHits", advanced: true, label: "空转词命中阈值", type: "number", min: 5, max: 100, group: "语义空转参数",
2799
3039
  hint: "窗口内出现这么多次\"让我想想/重新分析\"类用语即判定空转;调低则更激进。" },
2800
- { key: "churnWindowChars", label: "空转统计窗口", type: "number", min: 500, max: 20000, group: "语义空转参数" },
2801
- { key: "churnMinTextChars", label: "启用最小文本长度", type: "number", min: 500, max: 50000, group: "语义空转参数" },
3040
+ { key: "churnWindowChars", advanced: true, label: "空转统计窗口", type: "number", min: 500, max: 20000, group: "语义空转参数" },
3041
+ { key: "churnMinTextChars", advanced: true, label: "启用最小文本长度", type: "number", min: 500, max: 50000, group: "语义空转参数" },
2802
3042
 
2803
- { key: "softCutScanTail", label: "转折句扫描窗口", type: "number", min: 40, max: 2000, group: "转折句参数" },
2804
- { key: "softCutTransitionWindow", label: "转折词距句末窗口", type: "number", min: 8, max: 500, group: "转折句参数" },
2805
- { key: "softCutMinFollowChars", label: "转折词后最少展开字数", type: "number", min: 2, max: 200, group: "转折句参数",
3043
+ { key: "softCutScanTail", advanced: true, label: "转折句扫描窗口", type: "number", min: 40, max: 2000, group: "转折句参数" },
3044
+ { key: "softCutTransitionWindow", advanced: true, label: "转折词距句末窗口", type: "number", min: 8, max: 500, group: "转折句参数" },
3045
+ { key: "softCutMinFollowChars", advanced: true, label: "转折词后最少展开字数", type: "number", min: 2, max: 200, group: "转折句参数",
2806
3046
  hint: "转折词(如\"我先\")之后至少输出这么多字才允许切断,防止在句子刚开头就切断思考、截出半句结论。" },
2807
- { key: "softCutGapThreshold", label: "结论-转折最大间距", type: "number", min: 0, max: 400, group: "转折句参数",
3047
+ { key: "softCutGapThreshold", advanced: true, label: "结论-转折最大间距", type: "number", min: 0, max: 400, group: "转折句参数",
2808
3048
  hint: "Tier-1 结论标记(如\"所以\")与后续转折宣告(如\"接下来\")之间允许的最大字符距离。超过则认为是子阶段小结论而非阶段边界,拒绝软切。默认 80。" },
2809
- { key: "pseudoToolScanTail", label: "伪调用扫描窗口", type: "number", min: 200, max: 20000, group: "转折句参数" },
3049
+ { key: "pseudoToolScanTail", advanced: true, label: "伪调用扫描窗口", type: "number", min: 200, max: 20000, group: "转折句参数" },
2810
3050
 
2811
- { key: "enableLlmRefine", label: "启用 LLM 提炼结论", type: "boolean", group: "LLM 提炼",
3051
+ { key: "enableLlmRefine", advanced: true, label: "启用 LLM 提炼结论", type: "boolean", group: "LLM 提炼",
2812
3052
  hint: "用一次小模型调用把整段思考压成摘要式结论(比摘句更准)。开启后每个助手回合多一次小调用;失败会自动退回打分提取。provider/model 留空则沿用会话当前模型。" },
2813
- { key: "llmRefineProvider", label: "提炼用 provider(留空=跟随会话)", type: "text", group: "LLM 提炼" },
2814
- { key: "llmRefineModel", label: "提炼用 model(留空=跟随会话)", type: "text", group: "LLM 提炼" },
2815
- { key: "llmRefineMaxTokens", label: "提炼输出上限", type: "number", min: 64, max: 2000, group: "LLM 提炼" },
2816
- { key: "llmRefineTimeoutMs", label: "提炼超时(毫秒)", type: "number", min: 3000, max: 120000, group: "LLM 提炼" },
2817
- { key: "llmRefineMaxInputChars", label: "提炼输入截断字数", type: "number", min: 1000, max: 60000, group: "LLM 提炼" },
3053
+ { key: "llmRefineProvider", advanced: true, label: "提炼用 provider(留空=跟随会话)", type: "text", group: "LLM 提炼" },
3054
+ { key: "llmRefineModel", advanced: true, label: "提炼用 model(留空=跟随会话)", type: "text", group: "LLM 提炼" },
3055
+ { key: "llmRefineMaxTokens", advanced: true, label: "提炼输出上限", type: "number", min: 64, max: 2000, group: "LLM 提炼" },
3056
+ { key: "llmRefineTimeoutMs", advanced: true, label: "提炼超时(毫秒)", type: "number", min: 3000, max: 120000, group: "LLM 提炼" },
3057
+ { key: "llmRefineMaxInputChars", advanced: true, label: "提炼输入截断字数", type: "number", min: 1000, max: 60000, group: "LLM 提炼" },
2818
3058
 
2819
- { key: "enableHarvest", label: "启用 CoT 静默采集", type: "boolean", group: "CoT 采集",
3059
+ { key: "enableHarvest", advanced: true, label: "启用 CoT 静默采集", type: "boolean", group: "CoT 采集",
2820
3060
  hint: "关闭时零采集、零额外调用、零磁盘写入。开启后每个助手回合都会留一条样本,用于事后归纳「现有检测器看不见」的打转形态。" },
2821
- { key: "harvestIncludeToolTrace", label: "记录工具调用轨迹", type: "boolean", group: "CoT 采集",
3061
+ { key: "harvestIncludeToolTrace", advanced: true, label: "记录工具调用轨迹", type: "boolean", group: "CoT 采集",
2822
3062
  hint: "记录该步调用了哪些工具、是否报错,用于分析「反复重试同一工具」。" },
2823
- { key: "harvestMaxRecords", label: "样本条数上限", type: "number", min: 20, max: 2000, group: "CoT 采集" },
2824
- { key: "harvestRetentionDays", label: "样本保留天数", type: "number", min: 1, max: 90, group: "CoT 采集" },
2825
- { key: "harvestMinTextChars", label: "存全文的最小思考字数", type: "number", min: 500, max: 50000, group: "CoT 采集" },
2826
- { key: "harvestMaxTextChars", label: "单条全文上限", type: "number", min: 500, max: 40000, group: "CoT 采集" },
2827
- { key: "harvestFlushDebounceMs", label: "落盘去抖(毫秒)", type: "number", min: 500, max: 60000, group: "CoT 采集",
3063
+ { key: "harvestMaxRecords", advanced: true, label: "样本条数上限", type: "number", min: 20, max: 2000, group: "CoT 采集" },
3064
+ { key: "harvestRetentionDays", advanced: true, label: "样本保留天数", type: "number", min: 1, max: 90, group: "CoT 采集" },
3065
+ { key: "harvestMinTextChars", advanced: true, label: "存全文的最小思考字数", type: "number", min: 500, max: 50000, group: "CoT 采集" },
3066
+ { key: "harvestMaxTextChars", advanced: true, label: "单条全文上限", type: "number", min: 500, max: 40000, group: "CoT 采集" },
3067
+ { key: "harvestFlushDebounceMs", advanced: true, label: "落盘去抖(毫秒)", type: "number", min: 500, max: 60000, group: "CoT 采集",
2828
3068
  hint: "这段时间内的多次事件合并成一次追加写,避免每个 token 步都落盘。" },
2829
3069
 
2830
- { key: "enableAutoAnalyze", label: "自动分析", type: "boolean", group: "CoT 分析",
3070
+ { key: "enableAutoAnalyze", advanced: true, label: "自动分析", type: "boolean", group: "CoT 分析",
2831
3071
  hint: "未分析样本达到下方阈值时自动跑一次归纳;关闭则只能手动点「立即分析」。依赖「启用 CoT 静默采集」——开自动分析时会自动把采集也打开。",
2832
3072
  dependsOn: "enableHarvest" },
2833
- { key: "analyzeTriggerSamples", label: "自动分析触发条数", type: "number", min: 5, max: 500, group: "CoT 分析" },
2834
- { key: "analyzeProvider", label: "分析用 provider(留空=跟随样本)", type: "text", group: "CoT 分析" },
2835
- { key: "analyzeModel", label: "分析用 model(留空=跟随样本)", type: "text", group: "CoT 分析" },
2836
- { key: "analyzeMaxTokens", label: "分析输出上限", type: "number", min: 256, max: 4000, group: "CoT 分析" },
2837
- { key: "analyzeTimeoutMs", label: "分析超时(毫秒)", type: "number", min: 5000, max: 180000, group: "CoT 分析" },
2838
- { key: "analyzeMaxInputChars", label: "分析输入截断字数", type: "number", min: 2000, max: 120000, group: "CoT 分析" },
2839
- { key: "analyzeMaxSamples", label: "单批样本数", type: "number", min: 3, max: 60, group: "CoT 分析" },
2840
- { key: "analyzeRunBudgetMs", label: "单次分析总时长上限(毫秒)", type: "number", min: 0, max: 1800000, group: "CoT 分析" },
2841
-
2842
- { key: "enableLearnedPatterns", label: "加载已采纳的学习模式", type: "boolean", group: "CoT 增补",
3073
+ { key: "analyzeTriggerSamples", advanced: true, label: "自动分析触发条数", type: "number", min: 5, max: 500, group: "CoT 分析" },
3074
+ { key: "analyzeProvider", advanced: true, label: "分析用 provider(留空=跟随样本)", type: "text", group: "CoT 分析" },
3075
+ { key: "analyzeModel", advanced: true, label: "分析用 model(留空=跟随样本)", type: "text", group: "CoT 分析" },
3076
+ { key: "analyzeMaxTokens", advanced: true, label: "分析输出上限", type: "number", min: 256, max: 4000, group: "CoT 分析" },
3077
+ { key: "analyzeTimeoutMs", advanced: true, label: "分析超时(毫秒)", type: "number", min: 5000, max: 180000, group: "CoT 分析" },
3078
+ { key: "analyzeMaxInputChars", advanced: true, label: "分析输入截断字数", type: "number", min: 2000, max: 120000, group: "CoT 分析" },
3079
+ { key: "analyzeMaxSamples", advanced: true, label: "单批样本数", type: "number", min: 3, max: 60, group: "CoT 分析" },
3080
+ { key: "analyzeRunBudgetMs", advanced: true, label: "单次分析总时长上限(毫秒)", type: "number", min: 0, max: 1800000, group: "CoT 分析" },
3081
+
3082
+ { key: "enableLearnedPatterns", advanced: true, label: "加载已采纳的学习模式", type: "boolean", group: "CoT 增补",
2843
3083
  hint: "关闭后运行时只用出厂模式;patterns.json 保留不删,随时可再打开。" },
2844
- { key: "learnedMaxPhrases", label: "学习模式条数上限", type: "number", min: 4, max: 200, group: "CoT 增补" },
2845
- { key: "learnedMaxShift", label: "阈值允许最大偏移", type: "number", min: 0, max: 50, group: "CoT 增补",
3084
+ { key: "learnedMaxPhrases", advanced: true, label: "学习模式条数上限", type: "number", min: 4, max: 200, group: "CoT 增补" },
3085
+ { key: "learnedMaxShift", advanced: true, label: "阈值允许最大偏移", type: "number", min: 0, max: 50, group: "CoT 增补",
2846
3086
  hint: "学习层对出厂阈值的调整幅度上限,防止判定漂移。" },
2847
- { key: "learnedShadowRounds", label: "影子期命中次数(0=直接生效)", type: "number", min: 0, max: 200, group: "CoT 增补",
3087
+ { key: "learnedShadowRounds", advanced: true, label: "影子期命中次数(0=直接生效)", type: "number", min: 0, max: 200, group: "CoT 增补",
2848
3088
  hint: "学习模式首次生效时只统计「本会打断」的次数、不真的打断;累计够次数后再由你决定转正。" }
2849
3089
  ];
2850
3090
 
@@ -4358,6 +4598,26 @@ function apply(ctx, config) {
4358
4598
  };
4359
4599
  }
4360
4600
 
4601
+ // (2a) 修复②:n-gram 语义指纹循环。与 findRepeatingTail 并行,措辞漂移型
4602
+ // 循环(每句数字变、动词结构不变)字节级抓不到,但数字归一 + n-gram 频率
4603
+ // 能稳定定位。运行时零 LLM:哈希分窗 + HashMap 计数。
4604
+ const ngramLoop = settings.enableRepeat ? detectNGramLoop(fullText) : null;
4605
+ if (ngramLoop) {
4606
+ noteSoftCutFired();
4607
+ emitCotAnchorEvent(ctx, EVENT_SOFTCUT_TAKEN, {
4608
+ ...evidenceBase(payload?.sessionId, payload?.turn, payload?.step),
4609
+ cutClass: "heuristic",
4610
+ detector: "ngram-loop"
4611
+ });
4612
+ return {
4613
+ kind: "cut",
4614
+ budgetClass: "heuristic",
4615
+ cutClass: "heuristic",
4616
+ contexts: [buildAnchorContext(renderNGramLoopAnchor(ngramLoop),
4617
+ `cot-anchor: ngram-loop "${ngramLoop.maxGram}" x${ngramLoop.count}`)]
4618
+ };
4619
+ }
4620
+
4361
4621
  // Number runaway: a monotone-increasing run of bare numbers. Distinct
4362
4622
  // from byte-repetition (findRepeatingTail above) and churn (below);
4363
4623
  // cut to the run start so the runaway copies never reach the session.
@@ -4408,6 +4668,31 @@ function apply(ctx, config) {
4408
4668
  };
4409
4669
  }
4410
4670
 
4671
+ // (3a) 修复①:行动提议型空转并行分支。CHURN_ACTION_PHRASES 抓"让我 curl"这类
4672
+ // 无实际 tool_call 的动词反复出现。阈值 CHURN_ACTION_MIN_HITS=18,普通技术文本
4673
+ // 行动动词命中 0-3/3000 字,泄漏样本 ~75/3000 字,余量充足。
4674
+ // 独立于 enableChurn,默认与 enableChurn 同开关语义——通过 enableChurn 统一门禁,
4675
+ // 无需新增 settings key;如需独立控制可后续扩展 enableActionChurn。
4676
+ const actionChurn = !settings.enableChurn || insideUnclosedCodeFence(fullText) ? null : detectActionChurn(fullText);
4677
+ if (actionChurn) {
4678
+ noteSoftCutFired();
4679
+ const churnKey = typeof payload?.sessionId === "string" ? payload.sessionId : "";
4680
+ const churnOccurrence = recordChurnCut(churnKey, payload?.turn);
4681
+ const anchorText = renderActionChurnAnchor(actionChurn)
4682
+ + (churnOccurrence >= 2 ? renderChurnEscalation(churnOccurrence) : "");
4683
+ emitCotAnchorEvent(ctx, EVENT_SOFTCUT_TAKEN, {
4684
+ ...evidenceBase(payload?.sessionId, payload?.turn, payload?.step),
4685
+ cutClass: "heuristic",
4686
+ detector: "action-churn"
4687
+ });
4688
+ return {
4689
+ kind: "cut",
4690
+ budgetClass: "heuristic",
4691
+ cutClass: "heuristic",
4692
+ contexts: [buildAnchorContext(anchorText, `cot-anchor: action-churn x${actionChurn.hits}`)]
4693
+ };
4694
+ }
4695
+
4411
4696
  // (4) A→"接下来我要…"→B transition inside one generation.
4412
4697
  // The cut is destructive, so it requires an EXPLICIT conclusion
4413
4698
  // ("所以/因此/综上/therefore …") already present — a density-only
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cot-anchor",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "思考锚点:从模型上一段思考中提取已确立结论,在工具结果后注入,减少重复推导;并可识别打转与伪工具调用",
5
5
  "keywords": [
6
6
  "dsh",