dsh-client-auto-continue 0.20.1 → 0.20.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.
package/lib/index.js CHANGED
@@ -1645,6 +1645,7 @@ var freshState = () => ({
1645
1645
  streamTail: "",
1646
1646
  streamLastSegment: "",
1647
1647
  streamRepeatRun: 0,
1648
+ streamWindow: [],
1648
1649
  loopFired: false,
1649
1650
  loopRetryTimer: void 0,
1650
1651
  cooldownRetryTimer: void 0,
@@ -1824,6 +1825,8 @@ var CHILD_CAUSE_COPY = {
1824
1825
  var isLoopReason = (reason) => reason === LOOP_COMPLETED_REASON || reason === LOOP_SILENT_REASON;
1825
1826
  var STREAM_NEAR_DUPLICATE_MAX_CHARS = 2048;
1826
1827
  var STREAM_TAIL_MAX_CHARS = 4096;
1828
+ var STREAM_WINDOW_SEGMENTS = 40;
1829
+ var STREAM_WINDOW_MAX_UNIQUE = 8;
1827
1830
  var visibleText = (text) => text.replace(/\p{Cf}/gu, "").trim();
1828
1831
  function snapshotSessionEvents(session) {
1829
1832
  const compatible = session;
@@ -2433,6 +2436,7 @@ var AutoContinueRunner = class {
2433
2436
  const normalized = this.normalizedSegment(segment);
2434
2437
  const config = this.getConfig();
2435
2438
  if (normalized === "") return;
2439
+ if (this.noteStreamWindow(sessionId, state, normalized)) return;
2436
2440
  if (normalized.length < config.loopShortChars) {
2437
2441
  state.streamLastSegment = "";
2438
2442
  state.streamRepeatRun = 0;
@@ -2452,6 +2456,25 @@ var AutoContinueRunner = class {
2452
2456
  this.interruptLoop(sessionId, state);
2453
2457
  }
2454
2458
  }
2459
+ /**
2460
+ * 滚动窗口唯一率: 不看段长, 也不要求相邻, 所以短行交替复读(A/B/A/B)和短行
2461
+ * 连续复读都能命中。没有字母数字的段(分隔线、代码围栏、括号)不进窗口。
2462
+ * 返回 true 表示已打断。
2463
+ */
2464
+ noteStreamWindow(sessionId, state, normalized) {
2465
+ if (!/[\p{L}\p{N}]/u.test(normalized)) return false;
2466
+ const window = state.streamWindow;
2467
+ window.push(normalized);
2468
+ if (window.length > STREAM_WINDOW_SEGMENTS) window.shift();
2469
+ if (window.length < STREAM_WINDOW_SEGMENTS) return false;
2470
+ const unique = new Set(window).size;
2471
+ if (unique > STREAM_WINDOW_MAX_UNIQUE) return false;
2472
+ this.log(
2473
+ `检测到流式消息内复读 ${sessionId}: 最近 ${STREAM_WINDOW_SEGMENTS} 段只有 ${unique} 种不同文本`
2474
+ );
2475
+ this.interruptLoop(sessionId, state);
2476
+ return state.loopFired;
2477
+ }
2455
2478
  onAssistantChunk(sessionId, state, event) {
2456
2479
  if (!this.sessionConfig(sessionId).loopGuard || !state.running || state.loopFired) return;
2457
2480
  const chunk = this.assistantChunkText(event);
@@ -2464,7 +2487,16 @@ var AutoContinueRunner = class {
2464
2487
  state.streamTail = tail;
2465
2488
  return;
2466
2489
  }
2467
- state.streamTail = tail.slice(-STREAM_TAIL_MAX_CHARS);
2490
+ const lines = tail.split("\n");
2491
+ const rest = lines.pop() ?? "";
2492
+ for (const line of lines) {
2493
+ const normalized = this.normalizedSegment(line);
2494
+ if (normalized !== "" && this.noteStreamWindow(sessionId, state, normalized)) {
2495
+ state.streamTail = "";
2496
+ return;
2497
+ }
2498
+ }
2499
+ state.streamTail = rest.slice(-STREAM_TAIL_MAX_CHARS);
2468
2500
  if (/\S/u.test(tail)) {
2469
2501
  state.streamLastSegment = "";
2470
2502
  state.streamRepeatRun = 0;
@@ -2496,6 +2528,7 @@ var AutoContinueRunner = class {
2496
2528
  if (streamTail !== "") this.noteStreamSegment(sessionId, state, streamTail);
2497
2529
  state.streamLastSegment = "";
2498
2530
  state.streamRepeatRun = 0;
2531
+ state.streamWindow = [];
2499
2532
  this.checkLoop(sessionId, state);
2500
2533
  }
2501
2534
  /** 两个循环信号的公共检查; 命中且本回合未打断过则打断。 */
@@ -2558,6 +2591,7 @@ var AutoContinueRunner = class {
2558
2591
  state.streamTail = "";
2559
2592
  state.streamLastSegment = "";
2560
2593
  state.streamRepeatRun = 0;
2594
+ state.streamWindow = [];
2561
2595
  state.loopFired = false;
2562
2596
  state.turnOutput = "empty";
2563
2597
  state.outputTurn = event.data.turn;
@@ -2627,6 +2661,7 @@ var AutoContinueRunner = class {
2627
2661
  state.streamTail = "";
2628
2662
  state.streamLastSegment = "";
2629
2663
  state.streamRepeatRun = 0;
2664
+ state.streamWindow = [];
2630
2665
  state.tools.resetRepeat();
2631
2666
  const cooldown = this.cooldownFor(state);
2632
2667
  const remaining = cooldown - (Date.now() - state.lastAttemptAt);
@@ -164,6 +164,12 @@ export declare class AutoContinueRunner {
164
164
  private isNearDuplicateSegment;
165
165
  private withinEditDistance;
166
166
  private noteStreamSegment;
167
+ /**
168
+ * 滚动窗口唯一率: 不看段长, 也不要求相邻, 所以短行交替复读(A/B/A/B)和短行
169
+ * 连续复读都能命中。没有字母数字的段(分隔线、代码围栏、括号)不进窗口。
170
+ * 返回 true 表示已打断。
171
+ */
172
+ private noteStreamWindow;
167
173
  private onAssistantChunk;
168
174
  private onAssistantMessage;
169
175
  /** 两个循环信号的公共检查; 命中且本回合未打断过则打断。 */
@@ -394,6 +394,8 @@ export interface SessionState {
394
394
  streamLastSegment: string;
395
395
  /** 流式消息内连续近似重复长段计数。 */
396
396
  streamRepeatRun: number;
397
+ /** 流式消息最近若干段(或超长段的行)的归一化文本, 用于唯一率检测。 */
398
+ streamWindow: string[];
397
399
  /** 本回合已触发过 loop guard(防重复打断)。 */
398
400
  loopFired: boolean;
399
401
  /** loop 重启的延迟定时器(冷却结束后再 schedule)。 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-client-auto-continue",
3
3
  "description": "Automatically resume interrupted DSH conversations, with an optional autonomous loop.",
4
- "version": "0.20.1",
4
+ "version": "0.20.2",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/types/index.d.ts",
@@ -256,6 +256,15 @@ const isLoopReason = (reason: string): boolean =>
256
256
  const STREAM_NEAR_DUPLICATE_MAX_CHARS = 2_048;
257
257
  /** Bound unfinished text copied between token chunks when a provider emits no paragraph break. */
258
258
  const STREAM_TAIL_MAX_CHARS = 4_096;
259
+ /**
260
+ * Rolling window of recent stream segments (paragraphs, or lines of an overlong
261
+ * paragraph). A degenerate stream cycles through a handful of short lines
262
+ * (`Writing.` / `Then: fill domain, go.`), which neither the long-segment gate nor
263
+ * the adjacent near-duplicate check sees; a nearly unique-free window does.
264
+ */
265
+ const STREAM_WINDOW_SEGMENTS = 40;
266
+ /** At most this many distinct segments in a full window counts as a loop (20%). */
267
+ const STREAM_WINDOW_MAX_UNIQUE = 8;
259
268
 
260
269
  /** Ignore invisible formatting only for detection, never rewrite the stored message. */
261
270
  const visibleText = (text: string): string => text.replace(/\p{Cf}/gu, '').trim();
@@ -1071,6 +1080,7 @@ export class AutoContinueRunner {
1071
1080
  const normalized = this.normalizedSegment(segment);
1072
1081
  const config = this.getConfig();
1073
1082
  if (normalized === '') return;
1083
+ if (this.noteStreamWindow(sessionId, state, normalized)) return;
1074
1084
  if (normalized.length < config.loopShortChars) {
1075
1085
  state.streamLastSegment = '';
1076
1086
  state.streamRepeatRun = 0;
@@ -1094,6 +1104,26 @@ export class AutoContinueRunner {
1094
1104
  }
1095
1105
  }
1096
1106
 
1107
+ /**
1108
+ * 滚动窗口唯一率: 不看段长, 也不要求相邻, 所以短行交替复读(A/B/A/B)和短行
1109
+ * 连续复读都能命中。没有字母数字的段(分隔线、代码围栏、括号)不进窗口。
1110
+ * 返回 true 表示已打断。
1111
+ */
1112
+ private noteStreamWindow(sessionId: SessionId, state: SessionState, normalized: string): boolean {
1113
+ if (!/[\p{L}\p{N}]/u.test(normalized)) return false;
1114
+ const window = state.streamWindow;
1115
+ window.push(normalized);
1116
+ if (window.length > STREAM_WINDOW_SEGMENTS) window.shift();
1117
+ if (window.length < STREAM_WINDOW_SEGMENTS) return false;
1118
+ const unique = new Set(window).size;
1119
+ if (unique > STREAM_WINDOW_MAX_UNIQUE) return false;
1120
+ this.log(
1121
+ `检测到流式消息内复读 ${sessionId}: 最近 ${STREAM_WINDOW_SEGMENTS} 段只有 ${unique} 种不同文本`,
1122
+ );
1123
+ this.interruptLoop(sessionId, state);
1124
+ return state.loopFired;
1125
+ }
1126
+
1097
1127
  private onAssistantChunk(
1098
1128
  sessionId: SessionId,
1099
1129
  state: SessionState,
@@ -1110,7 +1140,17 @@ export class AutoContinueRunner {
1110
1140
  state.streamTail = tail;
1111
1141
  return;
1112
1142
  }
1113
- state.streamTail = tail.slice(-STREAM_TAIL_MAX_CHARS);
1143
+ // 超长且一直没有空行: 把完整的行送进滚动窗口, 单换行分隔的刷屏也能被看到。
1144
+ const lines = tail.split('\n');
1145
+ const rest = lines.pop() ?? '';
1146
+ for (const line of lines) {
1147
+ const normalized = this.normalizedSegment(line);
1148
+ if (normalized !== '' && this.noteStreamWindow(sessionId, state, normalized)) {
1149
+ state.streamTail = '';
1150
+ return;
1151
+ }
1152
+ }
1153
+ state.streamTail = rest.slice(-STREAM_TAIL_MAX_CHARS);
1114
1154
  if (/\S/u.test(tail)) {
1115
1155
  state.streamLastSegment = '';
1116
1156
  state.streamRepeatRun = 0;
@@ -1150,6 +1190,7 @@ export class AutoContinueRunner {
1150
1190
  if (streamTail !== '') this.noteStreamSegment(sessionId, state, streamTail);
1151
1191
  state.streamLastSegment = '';
1152
1192
  state.streamRepeatRun = 0;
1193
+ state.streamWindow = [];
1153
1194
  this.checkLoop(sessionId, state);
1154
1195
  }
1155
1196
 
@@ -1222,6 +1263,7 @@ export class AutoContinueRunner {
1222
1263
  state.streamTail = '';
1223
1264
  state.streamLastSegment = '';
1224
1265
  state.streamRepeatRun = 0;
1266
+ state.streamWindow = [];
1225
1267
  state.loopFired = false;
1226
1268
  state.turnOutput = 'empty';
1227
1269
  state.outputTurn = event.data.turn;
@@ -1304,6 +1346,7 @@ export class AutoContinueRunner {
1304
1346
  state.streamTail = '';
1305
1347
  state.streamLastSegment = '';
1306
1348
  state.streamRepeatRun = 0;
1349
+ state.streamWindow = [];
1307
1350
  state.tools.resetRepeat();
1308
1351
  // 重启受冷却约束(防紧密打断循环): 等剩余冷却结束后再调度
1309
1352
  const cooldown = this.cooldownFor(state);
@@ -993,6 +993,8 @@ export interface SessionState {
993
993
  streamLastSegment: string;
994
994
  /** 流式消息内连续近似重复长段计数。 */
995
995
  streamRepeatRun: number;
996
+ /** 流式消息最近若干段(或超长段的行)的归一化文本, 用于唯一率检测。 */
997
+ streamWindow: string[];
996
998
  /** 本回合已触发过 loop guard(防重复打断)。 */
997
999
  loopFired: boolean;
998
1000
  /** loop 重启的延迟定时器(冷却结束后再 schedule)。 */
@@ -1036,6 +1038,7 @@ export const freshState = (): SessionState => ({
1036
1038
  streamTail: '',
1037
1039
  streamLastSegment: '',
1038
1040
  streamRepeatRun: 0,
1041
+ streamWindow: [],
1039
1042
  loopFired: false,
1040
1043
  loopRetryTimer: undefined,
1041
1044
  cooldownRetryTimer: undefined,