dsh-client-auto-continue 0.14.1 → 0.14.3
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/README.md +13 -4
- package/README.zh.md +13 -4
- package/lib/client.js +10 -7
- package/lib/client.js.map +2 -2
- package/lib/index.js +105 -65
- package/lib/types/client/dsh-store-compat.d.ts +1 -1
- package/lib/types/client/settings-form.d.ts +3 -1
- package/lib/types/host/engine.d.ts +4 -0
- package/package.json +3 -3
- package/src/client/dsh-store-compat.ts +1 -1
- package/src/client/index.ts +2 -1
- package/src/client/locales.ts +2 -2
- package/src/client/settings-card.tsx +3 -2
- package/src/client/settings-form.ts +8 -3
- package/src/host/engine.ts +113 -62
package/src/host/engine.ts
CHANGED
|
@@ -101,6 +101,9 @@ const STREAM_NEAR_DUPLICATE_MAX_CHARS = 2_048;
|
|
|
101
101
|
/** Bound unfinished text copied between token chunks when a provider emits no paragraph break. */
|
|
102
102
|
const STREAM_TAIL_MAX_CHARS = 4_096;
|
|
103
103
|
|
|
104
|
+
/** Ignore invisible formatting only for detection, never rewrite the stored message. */
|
|
105
|
+
const visibleText = (text: string): string => text.replace(/\p{Cf}/gu, '').trim();
|
|
106
|
+
|
|
104
107
|
/** 通知桥事件: host 引擎产生, browser 侧订阅展示(Notification / 动作按钮)。 */
|
|
105
108
|
export interface HostNotice {
|
|
106
109
|
/** 稳定标识(供 browser 去重)。 */
|
|
@@ -128,9 +131,21 @@ type CompatibleSession = Session & {
|
|
|
128
131
|
/** Read one stable event-log snapshot on both legacy and DSH 0.1.2 hosts. */
|
|
129
132
|
function snapshotSessionEvents(session: Session): readonly SessionEvent[] {
|
|
130
133
|
const compatible = session as CompatibleSession;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new TypeError('session
|
|
134
|
+
const snapshot = compatible.snapshotEvents;
|
|
135
|
+
const events = typeof snapshot === 'function' ? snapshot.call(session) : compatible.events;
|
|
136
|
+
if (!Array.isArray(events)) throw new TypeError('session history is not an event array');
|
|
137
|
+
// Do not filter damaged entries: one could have been a manual stop, newer turn,
|
|
138
|
+
// or tool result. An incomplete history is not safe evidence for a continuation.
|
|
139
|
+
for (let index = 0; index < events.length; index += 1) {
|
|
140
|
+
const event = events[index];
|
|
141
|
+
if (event === null || typeof event !== 'object' || Array.isArray(event) ||
|
|
142
|
+
typeof event.type !== 'string' || event.type === '' ||
|
|
143
|
+
!Number.isSafeInteger(event.seq) || event.seq < 0 ||
|
|
144
|
+
!Number.isFinite(event.time) || event.time < 0) {
|
|
145
|
+
throw new TypeError(`invalid session event envelope at index ${index}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return events;
|
|
134
149
|
}
|
|
135
150
|
|
|
136
151
|
/** Interpret host failure payloads without trusting persisted or plugin-provided event shapes. */
|
|
@@ -219,6 +234,8 @@ export class AutoContinueRunner {
|
|
|
219
234
|
private readonly disposeInboxEvents: () => void;
|
|
220
235
|
private disposed = false;
|
|
221
236
|
private readonly bootScannedSessions = new WeakSet<Session>();
|
|
237
|
+
/** Also bound diagnostics when even an agent's session getter is unavailable. */
|
|
238
|
+
private readonly bootScanErrors = new WeakSet<object>();
|
|
222
239
|
private bootScanTimer: ReturnType<typeof setTimeout> | undefined;
|
|
223
240
|
private wakeBootScan: (() => void) | undefined;
|
|
224
241
|
|
|
@@ -411,6 +428,8 @@ export class AutoContinueRunner {
|
|
|
411
428
|
// 任何新鲜调用都代表进展(即使缺关联 id);旧帧重放不能清短句 streak。
|
|
412
429
|
if (state.tools.recordCall(event)) {
|
|
413
430
|
state.shortRun = 0;
|
|
431
|
+
state.sameTextRun = 0;
|
|
432
|
+
state.lastAssistantText = '';
|
|
414
433
|
if (this.tracksOutput(state, event)) state.turnOutput = 'visible';
|
|
415
434
|
}
|
|
416
435
|
} else if (event.type === 'tool/result') {
|
|
@@ -425,7 +444,7 @@ export class AutoContinueRunner {
|
|
|
425
444
|
const state = this.state(sessionId);
|
|
426
445
|
if (this.tracksOutput(state, event)) {
|
|
427
446
|
const chunk = event.data.chunk;
|
|
428
|
-
const visible = (chunk.type === 'text-delta' && chunk.text
|
|
447
|
+
const visible = (chunk.type === 'text-delta' && visibleText(chunk.text) !== '') ||
|
|
429
448
|
chunk.type === 'tool-call-delta' ||
|
|
430
449
|
(chunk.type === 'block-end' && this.hasVisibleContent([chunk.block])) ||
|
|
431
450
|
(chunk.type === 'block-start' && chunk.blockType !== 'reasoning' && chunk.blockType !== 'text');
|
|
@@ -456,7 +475,7 @@ export class AutoContinueRunner {
|
|
|
456
475
|
if (part === null || typeof part !== 'object') return true;
|
|
457
476
|
const block = part as { type?: unknown; text?: unknown };
|
|
458
477
|
if (block.type === 'reasoning') return false;
|
|
459
|
-
if (block.type === 'text') return typeof block.text !== 'string' || block.text
|
|
478
|
+
if (block.type === 'text') return typeof block.text !== 'string' || visibleText(block.text) !== '';
|
|
460
479
|
return true;
|
|
461
480
|
});
|
|
462
481
|
}
|
|
@@ -476,7 +495,7 @@ export class AutoContinueRunner {
|
|
|
476
495
|
}
|
|
477
496
|
|
|
478
497
|
private normalizedSegment(text: string): string {
|
|
479
|
-
return text.replace(/\s+/g, ' ')
|
|
498
|
+
return visibleText(text).replace(/\s+/g, ' ');
|
|
480
499
|
}
|
|
481
500
|
|
|
482
501
|
private isNearDuplicateSegment(left: string, right: string): boolean {
|
|
@@ -585,14 +604,14 @@ export class AutoContinueRunner {
|
|
|
585
604
|
): void {
|
|
586
605
|
if (!this.getConfig().loopGuard) return;
|
|
587
606
|
const text = this.assistantText(event);
|
|
588
|
-
const trimmed = text
|
|
607
|
+
const trimmed = visibleText(text);
|
|
589
608
|
// 相同文本重复(不限长度): 模型反复输出完全相同的消息是最强的循环信号,
|
|
590
609
|
// 例如 "Let me test variants of the regex..." 连续 7 遍
|
|
591
610
|
if (trimmed !== '' && trimmed === state.lastAssistantText) {
|
|
592
611
|
state.sameTextRun += 1;
|
|
593
612
|
} else {
|
|
594
613
|
state.lastAssistantText = trimmed;
|
|
595
|
-
state.sameTextRun = 1;
|
|
614
|
+
state.sameTextRun = trimmed === '' ? 0 : 1;
|
|
596
615
|
}
|
|
597
616
|
// 短句计数(长度 < loopShortChars 且落在时间窗内): 空转信号
|
|
598
617
|
if (trimmed.length < this.getConfig().loopShortChars) {
|
|
@@ -1207,6 +1226,7 @@ export class AutoContinueRunner {
|
|
|
1207
1226
|
const now = Date.now();
|
|
1208
1227
|
const candidates: {
|
|
1209
1228
|
session: Session;
|
|
1229
|
+
sessionId: SessionId;
|
|
1210
1230
|
events: readonly SessionEvent[];
|
|
1211
1231
|
lastEnd: SessionEvent<'turn/end'>;
|
|
1212
1232
|
reasonKind: string;
|
|
@@ -1215,78 +1235,109 @@ export class AutoContinueRunner {
|
|
|
1215
1235
|
listIndex: number;
|
|
1216
1236
|
}[] = [];
|
|
1217
1237
|
for (const agent of this.ctx.agents.list()) {
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
(event
|
|
1237
|
-
);
|
|
1238
|
-
if (lastEnd === undefined || reasonKind === undefined || (!isNonHumanReason(reasonKind) && !silent) ||
|
|
1239
|
-
lastEnd.time < now - config.freshMs || superseded) {
|
|
1240
|
-
this.bootScannedSessions.add(session);
|
|
1241
|
-
continue;
|
|
1242
|
-
}
|
|
1243
|
-
let failure: FailureFacts | undefined;
|
|
1244
|
-
if (reasonKind === 'error') {
|
|
1245
|
-
failure = parseFailureFacts((lastEnd.data.reason as { error?: unknown }).error);
|
|
1246
|
-
if (failure === undefined) {
|
|
1247
|
-
this.bootScannedSessions.add(session);
|
|
1248
|
-
console.error(`[auto-continue] 忽略畸形扫描 turn/end ${session.id}: error details 无法解释`);
|
|
1249
|
-
continue;
|
|
1238
|
+
let session: Session | undefined;
|
|
1239
|
+
let sessionId: SessionId | undefined;
|
|
1240
|
+
try {
|
|
1241
|
+
session = agent.session;
|
|
1242
|
+
// Check quarantine before reading metadata that may have failed previously.
|
|
1243
|
+
if (this.bootScannedSessions.has(session) || session.header?.origin === 'subagent') continue;
|
|
1244
|
+
sessionId = session.id;
|
|
1245
|
+
if (typeof sessionId !== 'string' || sessionId === '') throw new TypeError('session id is unavailable');
|
|
1246
|
+
const state = this.state(sessionId);
|
|
1247
|
+
if (state.pendingTimer !== undefined || state.consecutive >= config.maxConsecutive) continue;
|
|
1248
|
+
if (now - state.lastAttemptAt < this.cooldownFor(state)) continue;
|
|
1249
|
+
if (now < (this.pauseUntil.get(sessionId) ?? 0)) continue;
|
|
1250
|
+
const events = snapshotSessionEvents(session);
|
|
1251
|
+
// An empty, valid snapshot may still be waiting for history hydration.
|
|
1252
|
+
if (events.length === 0) continue;
|
|
1253
|
+
let lastEnd: SessionEvent<'turn/end'> | undefined;
|
|
1254
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
1255
|
+
const event = events[i];
|
|
1256
|
+
if (event?.type === 'turn/end') { lastEnd = event; break; }
|
|
1250
1257
|
}
|
|
1251
|
-
|
|
1258
|
+
const reasonKind = readReasonKind(lastEnd?.data.reason);
|
|
1259
|
+
const silent = reasonKind === 'no-visible-output' && config.resumeSilentTurns;
|
|
1260
|
+
const superseded = lastEnd !== undefined && events.some((event) =>
|
|
1261
|
+
event.seq > lastEnd!.seq && (event.type === 'turn/start' ||
|
|
1262
|
+
(event.type === 'user/message' && event.data.source.kind === 'user')),
|
|
1263
|
+
);
|
|
1264
|
+
if (lastEnd === undefined || reasonKind === undefined || (!isNonHumanReason(reasonKind) && !silent) ||
|
|
1265
|
+
lastEnd.time < now - config.freshMs || superseded) {
|
|
1252
1266
|
this.bootScannedSessions.add(session);
|
|
1253
|
-
this.onTurnFailure(session.id, 'scan:turn/end:error', failure);
|
|
1254
1267
|
continue;
|
|
1255
1268
|
}
|
|
1269
|
+
let failure: FailureFacts | undefined;
|
|
1270
|
+
if (reasonKind === 'error') {
|
|
1271
|
+
failure = parseFailureFacts((lastEnd.data.reason as { error?: unknown }).error);
|
|
1272
|
+
if (failure === undefined) {
|
|
1273
|
+
this.bootScannedSessions.add(session);
|
|
1274
|
+
console.error(`[auto-continue] 忽略畸形扫描 turn/end ${sessionId}: error details 无法解释`);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
if (config.classify && !isTransientFailure(failure, config.retryableErrorPatterns)) {
|
|
1278
|
+
this.bootScannedSessions.add(session);
|
|
1279
|
+
this.onTurnFailure(sessionId, 'scan:turn/end:error', failure);
|
|
1280
|
+
continue;
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
candidates.push({
|
|
1284
|
+
session, sessionId, events, lastEnd, reasonKind, failure,
|
|
1285
|
+
lastActivityAt: events.reduce((latest, event) => Math.max(latest, event.time), 0),
|
|
1286
|
+
listIndex: candidates.length,
|
|
1287
|
+
});
|
|
1288
|
+
} catch (error) {
|
|
1289
|
+
if (session !== null && typeof session === 'object') this.bootScannedSessions.add(session);
|
|
1290
|
+
const identity = session ?? agent;
|
|
1291
|
+
if (identity !== null && typeof identity === 'object') {
|
|
1292
|
+
if (this.bootScanErrors.has(identity)) continue;
|
|
1293
|
+
this.bootScanErrors.add(identity);
|
|
1294
|
+
}
|
|
1295
|
+
this.logBootScanFailure(sessionId, error);
|
|
1256
1296
|
}
|
|
1257
|
-
candidates.push({
|
|
1258
|
-
session, events, lastEnd, reasonKind, failure,
|
|
1259
|
-
lastActivityAt: events.reduce((latest, event) => Math.max(latest, event.time), 0),
|
|
1260
|
-
listIndex: candidates.length,
|
|
1261
|
-
});
|
|
1262
1297
|
}
|
|
1263
1298
|
// Limit recoveries, not healthy/permanent histories that cannot be resumed.
|
|
1264
1299
|
candidates.sort((left, right) => right.lastActivityAt - left.lastActivityAt || left.listIndex - right.listIndex);
|
|
1265
|
-
|
|
1300
|
+
let recoveries = 0;
|
|
1301
|
+
for (const candidate of candidates) {
|
|
1302
|
+
if (recoveries >= config.scanLimit) break;
|
|
1266
1303
|
if (this.disposed) return true;
|
|
1267
|
-
const { session, events, lastEnd, reasonKind, failure } = candidate;
|
|
1304
|
+
const { session, sessionId, events, lastEnd, reasonKind, failure } = candidate;
|
|
1268
1305
|
this.bootScannedSessions.add(session);
|
|
1269
|
-
const state = this.state(
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
state.lastFailureAt = lastEnd.time;
|
|
1277
|
-
this.onTurnFailure(session.id, scanReason, failure);
|
|
1278
|
-
} else {
|
|
1279
|
-
if (reasonKind === 'no-visible-output') {
|
|
1280
|
-
state.lastFailure = undefined;
|
|
1306
|
+
const state = this.state(sessionId);
|
|
1307
|
+
try {
|
|
1308
|
+
this.applyGuardFromEvents(state, events, lastEnd.seq);
|
|
1309
|
+
const scanReason = `scan:turn/end:${reasonKind}${reasonKind === 'no-visible-output' ? ':silent' : ''}`;
|
|
1310
|
+
this.log(`扫描发现中断 ${sessionId}(turn/end:${reasonKind}), 交给恢复策略处理`);
|
|
1311
|
+
if (failure !== undefined) {
|
|
1312
|
+
state.lastFailure = failure;
|
|
1281
1313
|
state.lastTurn = lastEnd.data.turn;
|
|
1282
1314
|
state.lastFailureAt = lastEnd.time;
|
|
1315
|
+
this.onTurnFailure(sessionId, scanReason, failure);
|
|
1316
|
+
} else {
|
|
1317
|
+
if (reasonKind === 'no-visible-output') {
|
|
1318
|
+
state.lastFailure = undefined;
|
|
1319
|
+
state.lastTurn = lastEnd.data.turn;
|
|
1320
|
+
state.lastFailureAt = lastEnd.time;
|
|
1321
|
+
}
|
|
1322
|
+
this.schedule(sessionId, scanReason);
|
|
1283
1323
|
}
|
|
1284
|
-
|
|
1324
|
+
recoveries += 1;
|
|
1325
|
+
} catch (error) {
|
|
1326
|
+
state.tools.reset();
|
|
1327
|
+
this.cancelPending(sessionId, '启动扫描失败');
|
|
1328
|
+
this.logBootScanFailure(sessionId, error);
|
|
1285
1329
|
}
|
|
1286
1330
|
}
|
|
1287
1331
|
return false;
|
|
1288
1332
|
}
|
|
1289
1333
|
|
|
1334
|
+
/** Captured identity only: even session.id can throw when the header is damaged. */
|
|
1335
|
+
private logBootScanFailure(sessionId: SessionId | undefined, error: unknown): void {
|
|
1336
|
+
console.error(`[auto-continue] 启动扫描跳过会话 ${sessionId ?? '(id unavailable)'}: ${
|
|
1337
|
+
error instanceof Error ? error.message : String(error)
|
|
1338
|
+
}`);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1290
1341
|
/** 从历史事件恢复上一步工具调用状态(扫描路径的幂等护栏)。 */
|
|
1291
1342
|
private applyGuardFromEvents(
|
|
1292
1343
|
state: SessionState,
|