dsh-client-auto-continue 0.14.0 → 0.14.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/README.md +14 -5
- package/README.zh.md +14 -5
- package/docs/banner-dark.svg +38 -1
- package/docs/banner-zh-dark.svg +38 -1
- package/docs/banner-zh.svg +38 -1
- package/docs/banner.svg +38 -1
- package/docs/demo-static.svg +115 -0
- package/docs/demo-zh-static.svg +115 -0
- package/docs/demo-zh.svg +147 -1
- package/docs/demo.svg +147 -1
- package/lib/index.js +97 -60
- package/lib/types/host/engine.d.ts +4 -0
- package/package.json +3 -3
- package/src/host/engine.ts +103 -57
package/src/host/engine.ts
CHANGED
|
@@ -128,9 +128,21 @@ type CompatibleSession = Session & {
|
|
|
128
128
|
/** Read one stable event-log snapshot on both legacy and DSH 0.1.2 hosts. */
|
|
129
129
|
function snapshotSessionEvents(session: Session): readonly SessionEvent[] {
|
|
130
130
|
const compatible = session as CompatibleSession;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new TypeError('session
|
|
131
|
+
const snapshot = compatible.snapshotEvents;
|
|
132
|
+
const events = typeof snapshot === 'function' ? snapshot.call(session) : compatible.events;
|
|
133
|
+
if (!Array.isArray(events)) throw new TypeError('session history is not an event array');
|
|
134
|
+
// Do not filter damaged entries: one could have been a manual stop, newer turn,
|
|
135
|
+
// or tool result. An incomplete history is not safe evidence for a continuation.
|
|
136
|
+
for (let index = 0; index < events.length; index += 1) {
|
|
137
|
+
const event = events[index];
|
|
138
|
+
if (event === null || typeof event !== 'object' || Array.isArray(event) ||
|
|
139
|
+
typeof event.type !== 'string' || event.type === '' ||
|
|
140
|
+
!Number.isSafeInteger(event.seq) || event.seq < 0 ||
|
|
141
|
+
!Number.isFinite(event.time) || event.time < 0) {
|
|
142
|
+
throw new TypeError(`invalid session event envelope at index ${index}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return events;
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
/** Interpret host failure payloads without trusting persisted or plugin-provided event shapes. */
|
|
@@ -219,6 +231,8 @@ export class AutoContinueRunner {
|
|
|
219
231
|
private readonly disposeInboxEvents: () => void;
|
|
220
232
|
private disposed = false;
|
|
221
233
|
private readonly bootScannedSessions = new WeakSet<Session>();
|
|
234
|
+
/** Also bound diagnostics when even an agent's session getter is unavailable. */
|
|
235
|
+
private readonly bootScanErrors = new WeakSet<object>();
|
|
222
236
|
private bootScanTimer: ReturnType<typeof setTimeout> | undefined;
|
|
223
237
|
private wakeBootScan: (() => void) | undefined;
|
|
224
238
|
|
|
@@ -1207,6 +1221,7 @@ export class AutoContinueRunner {
|
|
|
1207
1221
|
const now = Date.now();
|
|
1208
1222
|
const candidates: {
|
|
1209
1223
|
session: Session;
|
|
1224
|
+
sessionId: SessionId;
|
|
1210
1225
|
events: readonly SessionEvent[];
|
|
1211
1226
|
lastEnd: SessionEvent<'turn/end'>;
|
|
1212
1227
|
reasonKind: string;
|
|
@@ -1215,78 +1230,109 @@ export class AutoContinueRunner {
|
|
|
1215
1230
|
listIndex: number;
|
|
1216
1231
|
}[] = [];
|
|
1217
1232
|
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;
|
|
1233
|
+
let session: Session | undefined;
|
|
1234
|
+
let sessionId: SessionId | undefined;
|
|
1235
|
+
try {
|
|
1236
|
+
session = agent.session;
|
|
1237
|
+
// Check quarantine before reading metadata that may have failed previously.
|
|
1238
|
+
if (this.bootScannedSessions.has(session) || session.header?.origin === 'subagent') continue;
|
|
1239
|
+
sessionId = session.id;
|
|
1240
|
+
if (typeof sessionId !== 'string' || sessionId === '') throw new TypeError('session id is unavailable');
|
|
1241
|
+
const state = this.state(sessionId);
|
|
1242
|
+
if (state.pendingTimer !== undefined || state.consecutive >= config.maxConsecutive) continue;
|
|
1243
|
+
if (now - state.lastAttemptAt < this.cooldownFor(state)) continue;
|
|
1244
|
+
if (now < (this.pauseUntil.get(sessionId) ?? 0)) continue;
|
|
1245
|
+
const events = snapshotSessionEvents(session);
|
|
1246
|
+
// An empty, valid snapshot may still be waiting for history hydration.
|
|
1247
|
+
if (events.length === 0) continue;
|
|
1248
|
+
let lastEnd: SessionEvent<'turn/end'> | undefined;
|
|
1249
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
1250
|
+
const event = events[i];
|
|
1251
|
+
if (event?.type === 'turn/end') { lastEnd = event; break; }
|
|
1250
1252
|
}
|
|
1251
|
-
|
|
1253
|
+
const reasonKind = readReasonKind(lastEnd?.data.reason);
|
|
1254
|
+
const silent = reasonKind === 'no-visible-output' && config.resumeSilentTurns;
|
|
1255
|
+
const superseded = lastEnd !== undefined && events.some((event) =>
|
|
1256
|
+
event.seq > lastEnd!.seq && (event.type === 'turn/start' ||
|
|
1257
|
+
(event.type === 'user/message' && event.data.source.kind === 'user')),
|
|
1258
|
+
);
|
|
1259
|
+
if (lastEnd === undefined || reasonKind === undefined || (!isNonHumanReason(reasonKind) && !silent) ||
|
|
1260
|
+
lastEnd.time < now - config.freshMs || superseded) {
|
|
1252
1261
|
this.bootScannedSessions.add(session);
|
|
1253
|
-
this.onTurnFailure(session.id, 'scan:turn/end:error', failure);
|
|
1254
1262
|
continue;
|
|
1255
1263
|
}
|
|
1264
|
+
let failure: FailureFacts | undefined;
|
|
1265
|
+
if (reasonKind === 'error') {
|
|
1266
|
+
failure = parseFailureFacts((lastEnd.data.reason as { error?: unknown }).error);
|
|
1267
|
+
if (failure === undefined) {
|
|
1268
|
+
this.bootScannedSessions.add(session);
|
|
1269
|
+
console.error(`[auto-continue] 忽略畸形扫描 turn/end ${sessionId}: error details 无法解释`);
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
if (config.classify && !isTransientFailure(failure, config.retryableErrorPatterns)) {
|
|
1273
|
+
this.bootScannedSessions.add(session);
|
|
1274
|
+
this.onTurnFailure(sessionId, 'scan:turn/end:error', failure);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
candidates.push({
|
|
1279
|
+
session, sessionId, events, lastEnd, reasonKind, failure,
|
|
1280
|
+
lastActivityAt: events.reduce((latest, event) => Math.max(latest, event.time), 0),
|
|
1281
|
+
listIndex: candidates.length,
|
|
1282
|
+
});
|
|
1283
|
+
} catch (error) {
|
|
1284
|
+
if (session !== null && typeof session === 'object') this.bootScannedSessions.add(session);
|
|
1285
|
+
const identity = session ?? agent;
|
|
1286
|
+
if (identity !== null && typeof identity === 'object') {
|
|
1287
|
+
if (this.bootScanErrors.has(identity)) continue;
|
|
1288
|
+
this.bootScanErrors.add(identity);
|
|
1289
|
+
}
|
|
1290
|
+
this.logBootScanFailure(sessionId, error);
|
|
1256
1291
|
}
|
|
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
1292
|
}
|
|
1263
1293
|
// Limit recoveries, not healthy/permanent histories that cannot be resumed.
|
|
1264
1294
|
candidates.sort((left, right) => right.lastActivityAt - left.lastActivityAt || left.listIndex - right.listIndex);
|
|
1265
|
-
|
|
1295
|
+
let recoveries = 0;
|
|
1296
|
+
for (const candidate of candidates) {
|
|
1297
|
+
if (recoveries >= config.scanLimit) break;
|
|
1266
1298
|
if (this.disposed) return true;
|
|
1267
|
-
const { session, events, lastEnd, reasonKind, failure } = candidate;
|
|
1299
|
+
const { session, sessionId, events, lastEnd, reasonKind, failure } = candidate;
|
|
1268
1300
|
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;
|
|
1301
|
+
const state = this.state(sessionId);
|
|
1302
|
+
try {
|
|
1303
|
+
this.applyGuardFromEvents(state, events, lastEnd.seq);
|
|
1304
|
+
const scanReason = `scan:turn/end:${reasonKind}${reasonKind === 'no-visible-output' ? ':silent' : ''}`;
|
|
1305
|
+
this.log(`扫描发现中断 ${sessionId}(turn/end:${reasonKind}), 交给恢复策略处理`);
|
|
1306
|
+
if (failure !== undefined) {
|
|
1307
|
+
state.lastFailure = failure;
|
|
1281
1308
|
state.lastTurn = lastEnd.data.turn;
|
|
1282
1309
|
state.lastFailureAt = lastEnd.time;
|
|
1310
|
+
this.onTurnFailure(sessionId, scanReason, failure);
|
|
1311
|
+
} else {
|
|
1312
|
+
if (reasonKind === 'no-visible-output') {
|
|
1313
|
+
state.lastFailure = undefined;
|
|
1314
|
+
state.lastTurn = lastEnd.data.turn;
|
|
1315
|
+
state.lastFailureAt = lastEnd.time;
|
|
1316
|
+
}
|
|
1317
|
+
this.schedule(sessionId, scanReason);
|
|
1283
1318
|
}
|
|
1284
|
-
|
|
1319
|
+
recoveries += 1;
|
|
1320
|
+
} catch (error) {
|
|
1321
|
+
state.tools.reset();
|
|
1322
|
+
this.cancelPending(sessionId, '启动扫描失败');
|
|
1323
|
+
this.logBootScanFailure(sessionId, error);
|
|
1285
1324
|
}
|
|
1286
1325
|
}
|
|
1287
1326
|
return false;
|
|
1288
1327
|
}
|
|
1289
1328
|
|
|
1329
|
+
/** Captured identity only: even session.id can throw when the header is damaged. */
|
|
1330
|
+
private logBootScanFailure(sessionId: SessionId | undefined, error: unknown): void {
|
|
1331
|
+
console.error(`[auto-continue] 启动扫描跳过会话 ${sessionId ?? '(id unavailable)'}: ${
|
|
1332
|
+
error instanceof Error ? error.message : String(error)
|
|
1333
|
+
}`);
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1290
1336
|
/** 从历史事件恢复上一步工具调用状态(扫描路径的幂等护栏)。 */
|
|
1291
1337
|
private applyGuardFromEvents(
|
|
1292
1338
|
state: SessionState,
|