dsh-client-auto-continue 0.20.0 → 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/CHANGELOG.md +16 -0
- package/README.md +4 -2
- package/README.ru.md +4 -2
- package/README.zh.md +4 -2
- package/docs/releases/v0.20.1.md +39 -0
- package/docs/releases/v0.20.2.md +49 -0
- package/lib/client.js.map +2 -2
- package/lib/index.js +84 -1
- package/lib/types/host/engine.d.ts +16 -0
- package/lib/types/shared/core.d.ts +2 -0
- package/package.json +1 -1
- package/src/host/engine.ts +123 -1
- package/src/shared/core.ts +3 -0
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,
|
|
@@ -1784,6 +1785,7 @@ var MANUAL_NOTIFICATION_REASON = "manual:notification";
|
|
|
1784
1785
|
var SWEEP_OBSERVE_BATCH = 16;
|
|
1785
1786
|
var STORED_READ_TIMEOUT_MS = 5e3;
|
|
1786
1787
|
var SWEEP_MAX_FAILURES = 3;
|
|
1788
|
+
var STORED_SWEEP_READ_LIMIT = 32;
|
|
1787
1789
|
var RESTART_CANCEL_CAUSES = /* @__PURE__ */ new Set(["disposed", "legacy"]);
|
|
1788
1790
|
var DECISION_CANCEL_CAUSES = /* @__PURE__ */ new Set(["user", "parent", "hook"]);
|
|
1789
1791
|
var CHILD_SWEEP_LIMIT = 8;
|
|
@@ -1796,6 +1798,7 @@ var HANDOFF_COPY = {
|
|
|
1796
1798
|
ru: (child, code) => `Дочерняя сессия ${child} прервалась из-за ${code} и больше не может быть продолжена напрямую. Проверь её состояние и продолжи или заверши.`
|
|
1797
1799
|
};
|
|
1798
1800
|
var GOALS_SERVICE_KEY = "goals";
|
|
1801
|
+
var PROJECTION_CACHE_SERVICE_KEY = "sessionProjectionCache";
|
|
1799
1802
|
var CHILD_CAUSE_COPY = {
|
|
1800
1803
|
zh: {
|
|
1801
1804
|
"open-turn": "回合开始后中断",
|
|
@@ -1822,6 +1825,8 @@ var CHILD_CAUSE_COPY = {
|
|
|
1822
1825
|
var isLoopReason = (reason) => reason === LOOP_COMPLETED_REASON || reason === LOOP_SILENT_REASON;
|
|
1823
1826
|
var STREAM_NEAR_DUPLICATE_MAX_CHARS = 2048;
|
|
1824
1827
|
var STREAM_TAIL_MAX_CHARS = 4096;
|
|
1828
|
+
var STREAM_WINDOW_SEGMENTS = 40;
|
|
1829
|
+
var STREAM_WINDOW_MAX_UNIQUE = 8;
|
|
1825
1830
|
var visibleText = (text) => text.replace(/\p{Cf}/gu, "").trim();
|
|
1826
1831
|
function snapshotSessionEvents(session) {
|
|
1827
1832
|
const compatible = session;
|
|
@@ -1979,6 +1984,9 @@ var AutoContinueRunner = class {
|
|
|
1979
1984
|
/** 存储会话的连续观察/物化失败次数, 触达 SWEEP_MAX_FAILURES 后不再重试。 */
|
|
1980
1985
|
this.storedSweepFailures = /* @__PURE__ */ new Map();
|
|
1981
1986
|
this.storedSweepAnnounced = false;
|
|
1987
|
+
/** 本次启动已花掉的存储会话冷读配额(跨轮累计, 启动扫描窗口内不再重置)。 */
|
|
1988
|
+
this.storedReadIds = /* @__PURE__ */ new Set();
|
|
1989
|
+
this.storedBudgetLogged = false;
|
|
1982
1990
|
this.stopStoredSweep = new AbortController();
|
|
1983
1991
|
this.disposeSessionEvents = ctx.on("session/event", (session, event) => {
|
|
1984
1992
|
try {
|
|
@@ -2428,6 +2436,7 @@ var AutoContinueRunner = class {
|
|
|
2428
2436
|
const normalized = this.normalizedSegment(segment);
|
|
2429
2437
|
const config = this.getConfig();
|
|
2430
2438
|
if (normalized === "") return;
|
|
2439
|
+
if (this.noteStreamWindow(sessionId, state, normalized)) return;
|
|
2431
2440
|
if (normalized.length < config.loopShortChars) {
|
|
2432
2441
|
state.streamLastSegment = "";
|
|
2433
2442
|
state.streamRepeatRun = 0;
|
|
@@ -2447,6 +2456,25 @@ var AutoContinueRunner = class {
|
|
|
2447
2456
|
this.interruptLoop(sessionId, state);
|
|
2448
2457
|
}
|
|
2449
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
|
+
}
|
|
2450
2478
|
onAssistantChunk(sessionId, state, event) {
|
|
2451
2479
|
if (!this.sessionConfig(sessionId).loopGuard || !state.running || state.loopFired) return;
|
|
2452
2480
|
const chunk = this.assistantChunkText(event);
|
|
@@ -2459,7 +2487,16 @@ var AutoContinueRunner = class {
|
|
|
2459
2487
|
state.streamTail = tail;
|
|
2460
2488
|
return;
|
|
2461
2489
|
}
|
|
2462
|
-
|
|
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);
|
|
2463
2500
|
if (/\S/u.test(tail)) {
|
|
2464
2501
|
state.streamLastSegment = "";
|
|
2465
2502
|
state.streamRepeatRun = 0;
|
|
@@ -2491,6 +2528,7 @@ var AutoContinueRunner = class {
|
|
|
2491
2528
|
if (streamTail !== "") this.noteStreamSegment(sessionId, state, streamTail);
|
|
2492
2529
|
state.streamLastSegment = "";
|
|
2493
2530
|
state.streamRepeatRun = 0;
|
|
2531
|
+
state.streamWindow = [];
|
|
2494
2532
|
this.checkLoop(sessionId, state);
|
|
2495
2533
|
}
|
|
2496
2534
|
/** 两个循环信号的公共检查; 命中且本回合未打断过则打断。 */
|
|
@@ -2553,6 +2591,7 @@ var AutoContinueRunner = class {
|
|
|
2553
2591
|
state.streamTail = "";
|
|
2554
2592
|
state.streamLastSegment = "";
|
|
2555
2593
|
state.streamRepeatRun = 0;
|
|
2594
|
+
state.streamWindow = [];
|
|
2556
2595
|
state.loopFired = false;
|
|
2557
2596
|
state.turnOutput = "empty";
|
|
2558
2597
|
state.outputTurn = event.data.turn;
|
|
@@ -2622,6 +2661,7 @@ var AutoContinueRunner = class {
|
|
|
2622
2661
|
state.streamTail = "";
|
|
2623
2662
|
state.streamLastSegment = "";
|
|
2624
2663
|
state.streamRepeatRun = 0;
|
|
2664
|
+
state.streamWindow = [];
|
|
2625
2665
|
state.tools.resetRepeat();
|
|
2626
2666
|
const cooldown = this.cooldownFor(state);
|
|
2627
2667
|
const remaining = cooldown - (Date.now() - state.lastAttemptAt);
|
|
@@ -3495,6 +3535,7 @@ var AutoContinueRunner = class {
|
|
|
3495
3535
|
}
|
|
3496
3536
|
}
|
|
3497
3537
|
let observed = 0;
|
|
3538
|
+
const roots = [];
|
|
3498
3539
|
for (const record2 of records) {
|
|
3499
3540
|
if (this.disposed || Date.now() >= deadline || this.getConfig().paused || !this.getConfig().scanOnBoot) return;
|
|
3500
3541
|
if (typeof record2?.header?.id !== "string" || record2.header.id === "") {
|
|
@@ -3510,6 +3551,24 @@ var AutoContinueRunner = class {
|
|
|
3510
3551
|
continue;
|
|
3511
3552
|
}
|
|
3512
3553
|
if (record2.live || this.storedSwept.has(sessionId)) continue;
|
|
3554
|
+
roots.push({ sessionId, header: record2.header, recency: 0, index: roots.length });
|
|
3555
|
+
}
|
|
3556
|
+
if (roots.length > 0) {
|
|
3557
|
+
const cache = this.projectionCache();
|
|
3558
|
+
for (const root of roots) root.recency = this.storedRecency(cache, root.header);
|
|
3559
|
+
roots.sort((left, right) => right.recency - left.recency || left.index - right.index);
|
|
3560
|
+
}
|
|
3561
|
+
for (const root of roots) {
|
|
3562
|
+
if (this.disposed || Date.now() >= deadline || this.getConfig().paused || !this.getConfig().scanOnBoot) return;
|
|
3563
|
+
if (!this.storedReadIds.has(root.sessionId) && this.storedReadIds.size >= STORED_SWEEP_READ_LIMIT) {
|
|
3564
|
+
if (!this.storedBudgetLogged) {
|
|
3565
|
+
this.storedBudgetLogged = true;
|
|
3566
|
+
this.log(`存储扫描冷读已达上限 ${STORED_SWEEP_READ_LIMIT} 个会话, 其余会话本次启动跳过`);
|
|
3567
|
+
}
|
|
3568
|
+
return;
|
|
3569
|
+
}
|
|
3570
|
+
const sessionId = root.sessionId;
|
|
3571
|
+
this.storedReadIds.add(sessionId);
|
|
3513
3572
|
this.storedSwept.add(sessionId);
|
|
3514
3573
|
observed += 1;
|
|
3515
3574
|
let observation;
|
|
@@ -3595,6 +3654,30 @@ var AutoContinueRunner = class {
|
|
|
3595
3654
|
}
|
|
3596
3655
|
}
|
|
3597
3656
|
}
|
|
3657
|
+
/** The optional projection checkpoint cache; absent on hosts that do not ship it. */
|
|
3658
|
+
projectionCache() {
|
|
3659
|
+
try {
|
|
3660
|
+
return this.ctx.get(PROJECTION_CACHE_SERVICE_KEY);
|
|
3661
|
+
} catch {
|
|
3662
|
+
return void 0;
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
/**
|
|
3666
|
+
* 冷读排序提示: 投影检查点里的最近活动时间, 没有检查点时回退到会话创建时间。
|
|
3667
|
+
* 检查点只会落后于日志、不会超前, 所以这个数只用来排序, 不用来跳过会话。
|
|
3668
|
+
*/
|
|
3669
|
+
storedRecency(cache, header) {
|
|
3670
|
+
const created = typeof header.createdAt === "number" ? header.createdAt : 0;
|
|
3671
|
+
if (cache === void 0) return created;
|
|
3672
|
+
try {
|
|
3673
|
+
const block = cache.cachedSnapshot(header, ["timeContext", "sessionListMetadata"]);
|
|
3674
|
+
const values = block?.values;
|
|
3675
|
+
const at = values?.timeContext?.lastMessageTime ?? values?.sessionListMetadata?.lastPromptAt;
|
|
3676
|
+
return typeof at === "number" && Number.isFinite(at) ? at : created;
|
|
3677
|
+
} catch {
|
|
3678
|
+
return created;
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3598
3681
|
/**
|
|
3599
3682
|
* 子会话裁决。子会话本身永不直接续跑(父代理权威, 见 README), 但启动扫描要
|
|
3600
3683
|
* 知道它是否在重启时被打断, 才能在自己的 root 会话上补一次带清单的唤醒。
|
|
@@ -69,6 +69,9 @@ export declare class AutoContinueRunner {
|
|
|
69
69
|
/** 存储会话的连续观察/物化失败次数, 触达 SWEEP_MAX_FAILURES 后不再重试。 */
|
|
70
70
|
private readonly storedSweepFailures;
|
|
71
71
|
private storedSweepAnnounced;
|
|
72
|
+
/** 本次启动已花掉的存储会话冷读配额(跨轮累计, 启动扫描窗口内不再重置)。 */
|
|
73
|
+
private readonly storedReadIds;
|
|
74
|
+
private storedBudgetLogged;
|
|
72
75
|
private bootScanTimer;
|
|
73
76
|
private wakeBootScan;
|
|
74
77
|
private storedSweep;
|
|
@@ -161,6 +164,12 @@ export declare class AutoContinueRunner {
|
|
|
161
164
|
private isNearDuplicateSegment;
|
|
162
165
|
private withinEditDistance;
|
|
163
166
|
private noteStreamSegment;
|
|
167
|
+
/**
|
|
168
|
+
* 滚动窗口唯一率: 不看段长, 也不要求相邻, 所以短行交替复读(A/B/A/B)和短行
|
|
169
|
+
* 连续复读都能命中。没有字母数字的段(分隔线、代码围栏、括号)不进窗口。
|
|
170
|
+
* 返回 true 表示已打断。
|
|
171
|
+
*/
|
|
172
|
+
private noteStreamWindow;
|
|
164
173
|
private onAssistantChunk;
|
|
165
174
|
private onAssistantMessage;
|
|
166
175
|
/** 两个循环信号的公共检查; 命中且本回合未打断过则打断。 */
|
|
@@ -264,6 +273,13 @@ export declare class AutoContinueRunner {
|
|
|
264
273
|
* 则启动扫描永远看不到它们; 谓词与上方扫描一致, 仅失败类原因。
|
|
265
274
|
*/
|
|
266
275
|
private sweepStoredSessions;
|
|
276
|
+
/** The optional projection checkpoint cache; absent on hosts that do not ship it. */
|
|
277
|
+
private projectionCache;
|
|
278
|
+
/**
|
|
279
|
+
* 冷读排序提示: 投影检查点里的最近活动时间, 没有检查点时回退到会话创建时间。
|
|
280
|
+
* 检查点只会落后于日志、不会超前, 所以这个数只用来排序, 不用来跳过会话。
|
|
281
|
+
*/
|
|
282
|
+
private storedRecency;
|
|
267
283
|
/**
|
|
268
284
|
* 子会话裁决。子会话本身永不直接续跑(父代理权威, 见 README), 但启动扫描要
|
|
269
285
|
* 知道它是否在重启时被打断, 才能在自己的 root 会话上补一次带清单的唤醒。
|
|
@@ -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.
|
|
4
|
+
"version": "0.20.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/types/index.d.ts",
|
package/src/host/engine.ts
CHANGED
|
@@ -129,6 +129,13 @@ const SWEEP_OBSERVE_BATCH = 16;
|
|
|
129
129
|
const STORED_READ_TIMEOUT_MS = 5000;
|
|
130
130
|
/** 单个存储会话连续观察/物化失败达此数后放弃, 避免坏日志反复占满每轮观察配额。 */
|
|
131
131
|
const SWEEP_MAX_FAILURES = 3;
|
|
132
|
+
/**
|
|
133
|
+
* 一次启动最多冷读多少个存储会话。冷读要整份解码日志(zstd → 逐行 JSON.parse),
|
|
134
|
+
* 几百个会话连着读完足以把宿主堆(默认约 3.4GB)打满; 配额把启动期的内存和耗时
|
|
135
|
+
* 都封住。每个会话只计一次(失败重试、暂停后重读不再扣配额)。排序每次启动都一样,
|
|
136
|
+
* 配额之外的会话等于被跳过——它们最近活动最早, 在 freshMs 窗口内几乎不可能还新鲜。
|
|
137
|
+
*/
|
|
138
|
+
const STORED_SWEEP_READ_LIMIT = 32;
|
|
132
139
|
|
|
133
140
|
/** Why a child session needs its root session woken after a restart. */
|
|
134
141
|
type ChildInterruptCause = 'open-turn' | 'disposed' | 'interrupted' | 'max-tokens' | 'error';
|
|
@@ -148,6 +155,14 @@ interface SessionHeaderLike {
|
|
|
148
155
|
readonly parentSession?: SessionId;
|
|
149
156
|
}
|
|
150
157
|
|
|
158
|
+
/** The projection cache's zero-I/O listing read, as far as this module uses it. */
|
|
159
|
+
interface ProjectionCacheLike {
|
|
160
|
+
cachedSnapshot(
|
|
161
|
+
meta: SessionHeaderLike,
|
|
162
|
+
keys?: readonly string[],
|
|
163
|
+
): { readonly values: Record<string, unknown> } | undefined;
|
|
164
|
+
}
|
|
165
|
+
|
|
151
166
|
/** Cancel causes a host shutdown or a restart produces; only these justify a wake-up. */
|
|
152
167
|
const RESTART_CANCEL_CAUSES: ReadonlySet<string> = new Set(['disposed', 'legacy']);
|
|
153
168
|
/** Cancel causes that record an explicit human or parent decision; never recover these. */
|
|
@@ -173,6 +188,8 @@ const HANDOFF_COPY = {
|
|
|
173
188
|
|
|
174
189
|
/** Host service key for goals (0.2.x); older hosts simply do not register it. */
|
|
175
190
|
const GOALS_SERVICE_KEY = 'goals';
|
|
191
|
+
/** Host service key for the projection checkpoint cache; older hosts do not register it. */
|
|
192
|
+
const PROJECTION_CACHE_SERVICE_KEY = 'sessionProjectionCache';
|
|
176
193
|
|
|
177
194
|
/** Copy for each child interruption cause, keyed by UI locale. */
|
|
178
195
|
const CHILD_CAUSE_COPY = {
|
|
@@ -239,6 +256,15 @@ const isLoopReason = (reason: string): boolean =>
|
|
|
239
256
|
const STREAM_NEAR_DUPLICATE_MAX_CHARS = 2_048;
|
|
240
257
|
/** Bound unfinished text copied between token chunks when a provider emits no paragraph break. */
|
|
241
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;
|
|
242
268
|
|
|
243
269
|
/** Ignore invisible formatting only for detection, never rewrite the stored message. */
|
|
244
270
|
const visibleText = (text: string): string => text.replace(/\p{Cf}/gu, '').trim();
|
|
@@ -499,6 +525,9 @@ export class AutoContinueRunner {
|
|
|
499
525
|
/** 存储会话的连续观察/物化失败次数, 触达 SWEEP_MAX_FAILURES 后不再重试。 */
|
|
500
526
|
private readonly storedSweepFailures = new Map<SessionId, number>();
|
|
501
527
|
private storedSweepAnnounced = false;
|
|
528
|
+
/** 本次启动已花掉的存储会话冷读配额(跨轮累计, 启动扫描窗口内不再重置)。 */
|
|
529
|
+
private readonly storedReadIds = new Set<SessionId>();
|
|
530
|
+
private storedBudgetLogged = false;
|
|
502
531
|
private bootScanTimer: ReturnType<typeof setTimeout> | undefined;
|
|
503
532
|
private wakeBootScan: (() => void) | undefined;
|
|
504
533
|
private storedSweep: Promise<void> | undefined;
|
|
@@ -1051,6 +1080,7 @@ export class AutoContinueRunner {
|
|
|
1051
1080
|
const normalized = this.normalizedSegment(segment);
|
|
1052
1081
|
const config = this.getConfig();
|
|
1053
1082
|
if (normalized === '') return;
|
|
1083
|
+
if (this.noteStreamWindow(sessionId, state, normalized)) return;
|
|
1054
1084
|
if (normalized.length < config.loopShortChars) {
|
|
1055
1085
|
state.streamLastSegment = '';
|
|
1056
1086
|
state.streamRepeatRun = 0;
|
|
@@ -1074,6 +1104,26 @@ export class AutoContinueRunner {
|
|
|
1074
1104
|
}
|
|
1075
1105
|
}
|
|
1076
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
|
+
|
|
1077
1127
|
private onAssistantChunk(
|
|
1078
1128
|
sessionId: SessionId,
|
|
1079
1129
|
state: SessionState,
|
|
@@ -1090,7 +1140,17 @@ export class AutoContinueRunner {
|
|
|
1090
1140
|
state.streamTail = tail;
|
|
1091
1141
|
return;
|
|
1092
1142
|
}
|
|
1093
|
-
|
|
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);
|
|
1094
1154
|
if (/\S/u.test(tail)) {
|
|
1095
1155
|
state.streamLastSegment = '';
|
|
1096
1156
|
state.streamRepeatRun = 0;
|
|
@@ -1130,6 +1190,7 @@ export class AutoContinueRunner {
|
|
|
1130
1190
|
if (streamTail !== '') this.noteStreamSegment(sessionId, state, streamTail);
|
|
1131
1191
|
state.streamLastSegment = '';
|
|
1132
1192
|
state.streamRepeatRun = 0;
|
|
1193
|
+
state.streamWindow = [];
|
|
1133
1194
|
this.checkLoop(sessionId, state);
|
|
1134
1195
|
}
|
|
1135
1196
|
|
|
@@ -1202,6 +1263,7 @@ export class AutoContinueRunner {
|
|
|
1202
1263
|
state.streamTail = '';
|
|
1203
1264
|
state.streamLastSegment = '';
|
|
1204
1265
|
state.streamRepeatRun = 0;
|
|
1266
|
+
state.streamWindow = [];
|
|
1205
1267
|
state.loopFired = false;
|
|
1206
1268
|
state.turnOutput = 'empty';
|
|
1207
1269
|
state.outputTurn = event.data.turn;
|
|
@@ -1284,6 +1346,7 @@ export class AutoContinueRunner {
|
|
|
1284
1346
|
state.streamTail = '';
|
|
1285
1347
|
state.streamLastSegment = '';
|
|
1286
1348
|
state.streamRepeatRun = 0;
|
|
1349
|
+
state.streamWindow = [];
|
|
1287
1350
|
state.tools.resetRepeat();
|
|
1288
1351
|
// 重启受冷却约束(防紧密打断循环): 等剩余冷却结束后再调度
|
|
1289
1352
|
const cooldown = this.cooldownFor(state);
|
|
@@ -2315,6 +2378,13 @@ export class AutoContinueRunner {
|
|
|
2315
2378
|
}
|
|
2316
2379
|
}
|
|
2317
2380
|
let observed = 0;
|
|
2381
|
+
// 先只做零成本的筛除, 把要花冷读的根会话收进候选集, 再统一排序、按配额读。
|
|
2382
|
+
const roots: {
|
|
2383
|
+
sessionId: SessionId;
|
|
2384
|
+
header: SessionHeaderLike & { createdAt?: number };
|
|
2385
|
+
recency: number;
|
|
2386
|
+
index: number;
|
|
2387
|
+
}[] = [];
|
|
2318
2388
|
for (const record of records) {
|
|
2319
2389
|
if (this.disposed || Date.now() >= deadline || this.getConfig().paused || !this.getConfig().scanOnBoot) return;
|
|
2320
2390
|
if (typeof record?.header?.id !== 'string' || record.header.id === '') {
|
|
@@ -2333,6 +2403,24 @@ export class AutoContinueRunner {
|
|
|
2333
2403
|
continue;
|
|
2334
2404
|
}
|
|
2335
2405
|
if (record.live || this.storedSwept.has(sessionId)) continue;
|
|
2406
|
+
roots.push({ sessionId, header: record.header, recency: 0, index: roots.length });
|
|
2407
|
+
}
|
|
2408
|
+
if (roots.length > 0) {
|
|
2409
|
+
const cache = this.projectionCache();
|
|
2410
|
+
for (const root of roots) root.recency = this.storedRecency(cache, root.header);
|
|
2411
|
+
roots.sort((left, right) => right.recency - left.recency || left.index - right.index);
|
|
2412
|
+
}
|
|
2413
|
+
for (const root of roots) {
|
|
2414
|
+
if (this.disposed || Date.now() >= deadline || this.getConfig().paused || !this.getConfig().scanOnBoot) return;
|
|
2415
|
+
if (!this.storedReadIds.has(root.sessionId) && this.storedReadIds.size >= STORED_SWEEP_READ_LIMIT) {
|
|
2416
|
+
if (!this.storedBudgetLogged) {
|
|
2417
|
+
this.storedBudgetLogged = true;
|
|
2418
|
+
this.log(`存储扫描冷读已达上限 ${STORED_SWEEP_READ_LIMIT} 个会话, 其余会话本次启动跳过`);
|
|
2419
|
+
}
|
|
2420
|
+
return;
|
|
2421
|
+
}
|
|
2422
|
+
const sessionId = root.sessionId;
|
|
2423
|
+
this.storedReadIds.add(sessionId);
|
|
2336
2424
|
this.storedSwept.add(sessionId);
|
|
2337
2425
|
observed += 1;
|
|
2338
2426
|
let observation: StoredObservation | undefined;
|
|
@@ -2430,6 +2518,40 @@ export class AutoContinueRunner {
|
|
|
2430
2518
|
}
|
|
2431
2519
|
}
|
|
2432
2520
|
|
|
2521
|
+
/** The optional projection checkpoint cache; absent on hosts that do not ship it. */
|
|
2522
|
+
private projectionCache(): ProjectionCacheLike | undefined {
|
|
2523
|
+
try {
|
|
2524
|
+
return this.ctx.get(PROJECTION_CACHE_SERVICE_KEY) as ProjectionCacheLike | undefined;
|
|
2525
|
+
} catch {
|
|
2526
|
+
// 排序提示拿不到时回退到创建时间, 不能影响裁决。
|
|
2527
|
+
return undefined;
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
|
|
2531
|
+
/**
|
|
2532
|
+
* 冷读排序提示: 投影检查点里的最近活动时间, 没有检查点时回退到会话创建时间。
|
|
2533
|
+
* 检查点只会落后于日志、不会超前, 所以这个数只用来排序, 不用来跳过会话。
|
|
2534
|
+
*/
|
|
2535
|
+
private storedRecency(
|
|
2536
|
+
cache: ProjectionCacheLike | undefined,
|
|
2537
|
+
header: SessionHeaderLike & { createdAt?: number },
|
|
2538
|
+
): number {
|
|
2539
|
+
const created = typeof header.createdAt === 'number' ? header.createdAt : 0;
|
|
2540
|
+
if (cache === undefined) return created;
|
|
2541
|
+
try {
|
|
2542
|
+
const block = cache.cachedSnapshot(header, ['timeContext', 'sessionListMetadata']);
|
|
2543
|
+
const values = block?.values as {
|
|
2544
|
+
timeContext?: { lastMessageTime?: number };
|
|
2545
|
+
sessionListMetadata?: { lastPromptAt?: number };
|
|
2546
|
+
} | undefined;
|
|
2547
|
+
const at = values?.timeContext?.lastMessageTime ?? values?.sessionListMetadata?.lastPromptAt;
|
|
2548
|
+
return typeof at === 'number' && Number.isFinite(at) ? at : created;
|
|
2549
|
+
} catch {
|
|
2550
|
+
// 缓存读失败只影响排序, 不影响裁决。
|
|
2551
|
+
return created;
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2433
2555
|
/**
|
|
2434
2556
|
* 子会话裁决。子会话本身永不直接续跑(父代理权威, 见 README), 但启动扫描要
|
|
2435
2557
|
* 知道它是否在重启时被打断, 才能在自己的 root 会话上补一次带清单的唤醒。
|
package/src/shared/core.ts
CHANGED
|
@@ -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,
|