@keyou007/agent-bot 0.1.25 → 0.1.26
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 +4 -0
- package/README.md +2 -0
- package/README.zh.md +2 -0
- package/dist/feishu/CardRenderer.js +1 -0
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +1 -1
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/presentation/TurnStateReducer.js +1 -1
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +191 -12
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/state/StateStore.js +67 -3
- package/dist/state/StateStore.js.map +1 -1
- package/docs/technical-reference.md +1 -1
- package/docs/technical-reference.zh.md +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +1 -0
- package/src/feishu/CardRenderer.ts +1 -0
- package/src/feishu/FeishuTurnPresenter.ts +1 -1
- package/src/presentation/TurnStateReducer.ts +1 -1
- package/src/proxy/ProxySessionController.ts +191 -12
- package/src/runtime/types.ts +1 -1
- package/src/state/ExecutionProvenanceStore.ts +1 -1
- package/src/state/StateStore.ts +71 -3
|
@@ -54,9 +54,19 @@ const RESET_HISTORY_PAGE_SIZE = 10;
|
|
|
54
54
|
const REMOTE_TURN_HISTORY_REFRESH_INTERVAL_MS = 30_000;
|
|
55
55
|
const MAX_LLM_TURN_RETRIES = 3;
|
|
56
56
|
const QUEUED_PROMPT_RETRY_DELAY_MS = 5_000;
|
|
57
|
-
const RECOVERY_ACTIVITY_WINDOW_MS =
|
|
57
|
+
const RECOVERY_ACTIVITY_WINDOW_MS = 20 * 60 * 1_000;
|
|
58
58
|
const RECOVERY_RETRY_WINDOW_MS = 60_000;
|
|
59
59
|
const RECOVERY_HEARTBEAT_INTERVAL_MS = 60 * 1_000;
|
|
60
|
+
const RECOVERY_CONTINUATION_PROMPT = "上一轮因主机或 Agent Bot 重启而中断。请在当前会话和工作目录中继续完成尚未完成的任务,遵守已有用户要求。先核实已有执行结果,保留已完成的工作,不要重复已完成的操作;不要重新执行整条原始指令。";
|
|
61
|
+
const RECOVERY_RESULTS = {
|
|
62
|
+
reattached: "已重新连接原轮次,正在继续监控执行;未重新执行原指令。",
|
|
63
|
+
submitted: "已恢复此前确认尚未提交的请求,并提交执行。",
|
|
64
|
+
completed: "重启恢复检查完成:原轮次已完成,未重新执行原指令。",
|
|
65
|
+
failed: "重启恢复检查完成:原轮次已失败,未重新执行原指令。请查看执行详情后发送消息继续。",
|
|
66
|
+
cancelled: "重启恢复检查完成:请求已提交,但原轮次已中断。为避免重复执行,未重新执行原指令,也未自动创建续跑轮次;请发送“继续”续跑。",
|
|
67
|
+
continuing: "原轮次已中断,正在原任务中自动继续;未重新执行原指令。",
|
|
68
|
+
unknown: "重启恢复检查未完成:原执行结果待核实,未重新执行原指令。请核实执行记录。",
|
|
69
|
+
};
|
|
60
70
|
const SHELL_COMMAND_JOB_POLL_INTERVAL_MS = 1_000;
|
|
61
71
|
const SHELL_COMMAND_JOB_RELAUNCH_DELAY_MS = 5_000;
|
|
62
72
|
const APP_SERVER_RELEASE_POLL_INTERVAL_MS = 500;
|
|
@@ -261,6 +271,8 @@ export class ProxySessionController {
|
|
|
261
271
|
recoveryRetryTimers = new Map();
|
|
262
272
|
recoveryRetryStartedAt = new Map();
|
|
263
273
|
recoveredLiveAttempts = new Set();
|
|
274
|
+
recoveryResultNotices = new Set();
|
|
275
|
+
recoverySupersededSessions = new Set();
|
|
264
276
|
recoveryClosed = false;
|
|
265
277
|
pendingMessageReferences = new Map();
|
|
266
278
|
relatedReactionMessageIds = new Map();
|
|
@@ -315,6 +327,11 @@ export class ProxySessionController {
|
|
|
315
327
|
// retries cannot add duplicate reactions.
|
|
316
328
|
if (!this.store.claimInboundEvent(message.messageId, "message"))
|
|
317
329
|
return;
|
|
330
|
+
if (!message.text.trim().startsWith("/")) {
|
|
331
|
+
const currentId = this.store.getUserContext(message.contextKey)?.currentSessionId;
|
|
332
|
+
if (currentId)
|
|
333
|
+
this.recoverySupersededSessions.add(currentId);
|
|
334
|
+
}
|
|
318
335
|
const acceptedAt = Date.now();
|
|
319
336
|
const receivedAt = message.receivedAt ?? acceptedAt;
|
|
320
337
|
const deliveryDelayMs = message.createdAt === undefined
|
|
@@ -663,6 +680,13 @@ export class ProxySessionController {
|
|
|
663
680
|
const scopedAction = contextKey === resolvedAction.contextKey
|
|
664
681
|
? resolvedAction
|
|
665
682
|
: { ...resolvedAction, contextKey };
|
|
683
|
+
if (["settings_provider_select", "settings_model_select", "settings_thinking_select", "settings_permission_select",
|
|
684
|
+
"model_select", "turn_cancel", "session_stop", "turn_reset"].includes(String(scopedAction.value.action ?? ""))) {
|
|
685
|
+
const sessionId = String(scopedAction.value.sessionId ?? "")
|
|
686
|
+
|| this.store.getUserContext(contextKey)?.currentSessionId;
|
|
687
|
+
if (sessionId && this.store.getSessionForContext(sessionId, contextKey))
|
|
688
|
+
this.recoverySupersededSessions.add(sessionId);
|
|
689
|
+
}
|
|
666
690
|
const replyTarget = isThreadContextKey(contextKey) && action.messageId
|
|
667
691
|
? { messageId: action.messageId, replyInThread: true }
|
|
668
692
|
: undefined;
|
|
@@ -1048,6 +1072,8 @@ export class ProxySessionController {
|
|
|
1048
1072
|
this.recoveryRetryTimers.clear();
|
|
1049
1073
|
this.recoveryRetryStartedAt.clear();
|
|
1050
1074
|
this.recoveredLiveAttempts.clear();
|
|
1075
|
+
this.recoveryResultNotices.clear();
|
|
1076
|
+
this.recoverySupersededSessions.clear();
|
|
1051
1077
|
for (const schedule of this.appServerReleaseSchedules.values()) {
|
|
1052
1078
|
if (schedule.timer)
|
|
1053
1079
|
clearTimeout(schedule.timer);
|
|
@@ -2002,9 +2028,16 @@ export class ProxySessionController {
|
|
|
2002
2028
|
async runStartupRecovery() {
|
|
2003
2029
|
await this.finishPersistedCancellationRequests();
|
|
2004
2030
|
await this.recoverShellCommandJobs();
|
|
2031
|
+
const recoveryCutoff = Date.now() - RECOVERY_ACTIVITY_WINDOW_MS;
|
|
2032
|
+
const cutoff = new Date(recoveryCutoff).toISOString();
|
|
2033
|
+
for (const attempt of this.store.listRecentlyInterruptedTurnAttempts(cutoff)) {
|
|
2034
|
+
const session = this.store.getSession(attempt.localSessionId);
|
|
2035
|
+
if (session && this.canContinueAfterRestart(attempt, session)) {
|
|
2036
|
+
this.store.prepareInterruptedTurnAttemptRecovery(attempt.attemptId, cutoff);
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2005
2039
|
this.backfillTurnAttemptsForUpgrade();
|
|
2006
2040
|
const attempts = this.store.listIncompleteTurnAttempts();
|
|
2007
|
-
const recoveryCutoff = Date.now() - RECOVERY_ACTIVITY_WINDOW_MS;
|
|
2008
2041
|
const recentAttempts = attempts.filter((attempt) => isRecoveryAttemptRecent(attempt, recoveryCutoff));
|
|
2009
2042
|
if (recentAttempts.length > 0) {
|
|
2010
2043
|
this.logger.warn({ attemptIds: recentAttempts.map((attempt) => attempt.attemptId) }, "Recovering unfinished Agent Bot tasks after startup.");
|
|
@@ -2028,6 +2061,8 @@ export class ProxySessionController {
|
|
|
2028
2061
|
this.recoveryRetryStartedAt.delete(attempt.attemptId);
|
|
2029
2062
|
}
|
|
2030
2063
|
}
|
|
2064
|
+
if (this.recoveryClosed)
|
|
2065
|
+
return;
|
|
2031
2066
|
for (const pending of this.store.listPendingProviderSettings()) {
|
|
2032
2067
|
await this.applyPendingProviderWhenIdle(pending.localSessionId);
|
|
2033
2068
|
}
|
|
@@ -2035,6 +2070,9 @@ export class ProxySessionController {
|
|
|
2035
2070
|
}
|
|
2036
2071
|
async finishPersistedCancellationRequests() {
|
|
2037
2072
|
for (const attempt of this.store.listCancellationRequestedTurnAttempts()) {
|
|
2073
|
+
if (attempt.executionGeneration === this.store.executionGeneration(attempt.localSessionId)) {
|
|
2074
|
+
this.store.requestSessionExecutionStop(attempt.localSessionId);
|
|
2075
|
+
}
|
|
2038
2076
|
const session = this.store.getSession(attempt.localSessionId);
|
|
2039
2077
|
if (!attempt.turnId) {
|
|
2040
2078
|
if (attempt.submissionState !== "not_submitted") {
|
|
@@ -2119,6 +2157,13 @@ export class ProxySessionController {
|
|
|
2119
2157
|
const persistedTurnId = latest?.turnId;
|
|
2120
2158
|
const pendingTurnId = persistedTurnId?.startsWith("pending_") ? persistedTurnId : undefined;
|
|
2121
2159
|
const turnId = pendingTurnId ? undefined : persistedTurnId ?? session.lastTurnId;
|
|
2160
|
+
if (turnId && this.store.hasStoppedTurnAttemptForTurn(turnId)) {
|
|
2161
|
+
this.store.updateSession(session.localSessionId, { status: "ready" });
|
|
2162
|
+
continue;
|
|
2163
|
+
}
|
|
2164
|
+
const previousAttempt = turnId ? this.store.findLatestTurnAttemptByTurnId(turnId) : undefined;
|
|
2165
|
+
if (previousAttempt && !this.store.isLatestTurnAttempt(previousAttempt.attemptId))
|
|
2166
|
+
continue;
|
|
2122
2167
|
if (persistedTurnId && this.store.schedules.ownsTurn(persistedTurnId))
|
|
2123
2168
|
continue;
|
|
2124
2169
|
if (turnId && this.store.schedules.ownsTurn(turnId))
|
|
@@ -2135,12 +2180,13 @@ export class ProxySessionController {
|
|
|
2135
2180
|
turnId,
|
|
2136
2181
|
status: turnId ? "running" : "accepted",
|
|
2137
2182
|
submissionState: turnId ? "submitted" : "submission_unknown",
|
|
2138
|
-
createdAt: latest?.updatedAt ?? session.updatedAt,
|
|
2139
|
-
updatedAt: latest?.updatedAt ?? session.updatedAt,
|
|
2183
|
+
createdAt: previousAttempt?.createdAt ?? latest?.updatedAt ?? session.updatedAt,
|
|
2184
|
+
updatedAt: previousAttempt?.updatedAt ?? latest?.updatedAt ?? session.updatedAt,
|
|
2185
|
+
executionGeneration: previousAttempt?.executionGeneration,
|
|
2140
2186
|
});
|
|
2141
2187
|
}
|
|
2142
2188
|
}
|
|
2143
|
-
async expireStaleTurnAttempt(attempt, message = "执行中断已超过
|
|
2189
|
+
async expireStaleTurnAttempt(attempt, message = "执行中断已超过 20 分钟,未自动恢复。") {
|
|
2144
2190
|
if (attempt.submissionState !== "not_submitted") {
|
|
2145
2191
|
await this.markAttemptUnknown(attempt, `${message} 原执行结果待核实,未自动重发。`);
|
|
2146
2192
|
return;
|
|
@@ -2209,7 +2255,7 @@ export class ProxySessionController {
|
|
|
2209
2255
|
const replyTarget = attempt.replyMessageId
|
|
2210
2256
|
? { messageId: attempt.replyMessageId, replyInThread: true } : undefined;
|
|
2211
2257
|
await this.outbound.withReplyTarget(attempt.contextKey, replyTarget, async () => {
|
|
2212
|
-
await this.
|
|
2258
|
+
await this.notifyRecoveryResult(attempt, "unknown", message);
|
|
2213
2259
|
if (writerConflict && session)
|
|
2214
2260
|
await this.presentThreadWriterConflict(session, attempt.contextKey, error, false);
|
|
2215
2261
|
});
|
|
@@ -2219,7 +2265,7 @@ export class ProxySessionController {
|
|
|
2219
2265
|
}
|
|
2220
2266
|
return;
|
|
2221
2267
|
}
|
|
2222
|
-
this.store.
|
|
2268
|
+
this.store.markTurnAttemptRecovering(attemptId);
|
|
2223
2269
|
this.scheduleRecoveryRetry(attemptId, monitoredTurnId);
|
|
2224
2270
|
}
|
|
2225
2271
|
persistActiveTurnHeartbeats() {
|
|
@@ -2261,7 +2307,7 @@ export class ProxySessionController {
|
|
|
2261
2307
|
: undefined;
|
|
2262
2308
|
this.outbound.registerSession(session.localSessionId, contextKey, session.title, session.cwd, this.agentLabel(session.agentName), session.model, session.modelProvider);
|
|
2263
2309
|
if (announce) {
|
|
2264
|
-
await this.outbound.withReplyTarget(contextKey, replyTarget, () => this.outbound.sendText(contextKey, "
|
|
2310
|
+
await this.outbound.withReplyTarget(contextKey, replyTarget, () => this.outbound.sendText(contextKey, "检测到重启前存在未完成任务,正在核对远端执行状态并尝试恢复。")).catch((error) => {
|
|
2265
2311
|
this.logger.warn({ error, attemptId: attempt.attemptId, contextKey }, "Failed to send the task recovery notification.");
|
|
2266
2312
|
return undefined;
|
|
2267
2313
|
});
|
|
@@ -2288,6 +2334,10 @@ export class ProxySessionController {
|
|
|
2288
2334
|
}
|
|
2289
2335
|
}
|
|
2290
2336
|
const remoteTurnBelongsToAttempt = remoteTurnMatchesAttempt(remote, attempt);
|
|
2337
|
+
if (isPendingRecoveryContinuation(attempt)) {
|
|
2338
|
+
await this.submitRecoveryContinuation(attempt, session);
|
|
2339
|
+
return;
|
|
2340
|
+
}
|
|
2291
2341
|
if (remote && remoteTurnBelongsToAttempt && remote.lastTurnStatus === "completed") {
|
|
2292
2342
|
await this.reconcileRecoveredRemoteTurn(attempt, session, remote.lastTurnId);
|
|
2293
2343
|
return;
|
|
@@ -2301,7 +2351,7 @@ export class ProxySessionController {
|
|
|
2301
2351
|
return;
|
|
2302
2352
|
}
|
|
2303
2353
|
if (remote && isRemoteSessionActive(remote) && !remoteTurnBelongsToAttempt) {
|
|
2304
|
-
this.store.
|
|
2354
|
+
this.store.markTurnAttemptRecovering(attempt.attemptId);
|
|
2305
2355
|
if (announce) {
|
|
2306
2356
|
await this.outbound.withReplyTarget(contextKey, replyTarget, () => this.outbound.sendText(contextKey, "任务中检测到另一轮仍在执行;本次恢复将在它结束后重试。"));
|
|
2307
2357
|
}
|
|
@@ -2322,7 +2372,94 @@ export class ProxySessionController {
|
|
|
2322
2372
|
if (job)
|
|
2323
2373
|
await this.notifySchedule(job, `定时任务「${job.spec.name}」:${detail}`);
|
|
2324
2374
|
}
|
|
2375
|
+
canContinueAfterRestart(attempt, source) {
|
|
2376
|
+
const current = this.store.getSession(source.localSessionId);
|
|
2377
|
+
if (!current)
|
|
2378
|
+
return false;
|
|
2379
|
+
const sourceTurnId = isPendingRecoveryContinuation(attempt) ? attempt.recoveredFromTurnId : attempt.turnId;
|
|
2380
|
+
const priorSettings = sourceTurnId ? this.store.provenance.forTurn(sourceTurnId, source.localSessionId)?.settings : undefined;
|
|
2381
|
+
return !this.recoveryClosed && Boolean(current.remoteSessionId) && current.status !== "closed"
|
|
2382
|
+
&& current.remoteSessionId === source.remoteSessionId && current.lastTurnId === sourceTurnId
|
|
2383
|
+
&& current.agentName === source.agentName && this.runtimes.forAgent(current.agentName).kind === "codex"
|
|
2384
|
+
&& attempt.executionGeneration === this.store.executionGeneration(source.localSessionId)
|
|
2385
|
+
&& !this.sessionResets.has(source.localSessionId) && !this.recoverySupersededSessions.has(source.localSessionId)
|
|
2386
|
+
&& !this.store.getPendingProviderSettings(source.localSessionId)
|
|
2387
|
+
&& !this.store.schedules.getRun(attempt.attemptId) && this.store.isLatestTurnAttempt(attempt.attemptId)
|
|
2388
|
+
&& !this.store.hasUnknownTurnAttemptForSession(source.localSessionId)
|
|
2389
|
+
&& !this.store.hasCancellationRequestedForSession(source.localSessionId)
|
|
2390
|
+
&& this.store.countQueuedPrompts(source.localSessionId) === 0
|
|
2391
|
+
&& [source, ...(priorSettings ? [priorSettings] : [])].every((settings) => ["cwd", "agentName", "modelProvider", "model", "reasoningEffort", "permissionMode"]
|
|
2392
|
+
.every((key) => settings[key] === undefined || settings[key] === current[key]));
|
|
2393
|
+
}
|
|
2394
|
+
async assertRecoveryContinuation(loaded, attemptId) {
|
|
2395
|
+
const attempt = this.store.getTurnAttempt(attemptId);
|
|
2396
|
+
if (!attempt || !isPendingRecoveryContinuation(attempt) || !this.canContinueAfterRestart(attempt, loaded.record)) {
|
|
2397
|
+
throw new TurnStoppedBeforeSubmissionError();
|
|
2398
|
+
}
|
|
2399
|
+
const remote = await loaded.runtime.readRemoteSession?.(loaded.record.remoteSessionId);
|
|
2400
|
+
if (!remote || remote.lastTurnId !== attempt.recoveredFromTurnId || remote.lastTurnStatus !== "interrupted"
|
|
2401
|
+
|| isRemoteSessionActive(remote) || !this.canContinueAfterRestart(attempt, loaded.record)) {
|
|
2402
|
+
throw new TurnStoppedBeforeSubmissionError();
|
|
2403
|
+
}
|
|
2404
|
+
}
|
|
2405
|
+
async submitRecoveryContinuation(attempt, source) {
|
|
2406
|
+
try {
|
|
2407
|
+
await this.settingsOperations.run(source.localSessionId, async () => {
|
|
2408
|
+
if (!this.canContinueAfterRestart(attempt, source))
|
|
2409
|
+
throw new TurnStoppedBeforeSubmissionError();
|
|
2410
|
+
const current = this.store.getSession(source.localSessionId);
|
|
2411
|
+
const loaded = await this.loadSession({ ...current, contextKey: attempt.contextKey });
|
|
2412
|
+
await this.assertRecoveryContinuation(loaded, attempt.attemptId);
|
|
2413
|
+
await this.notifyRecoveryResult(attempt, "continuing");
|
|
2414
|
+
const turnId = await this.startTurnNow(loaded, attempt.promptText, attempt.replyMessageId ? { messageId: attempt.replyMessageId, replyInThread: true } : undefined, undefined, {
|
|
2415
|
+
attemptId: attempt.attemptId, executionGeneration: attempt.executionGeneration,
|
|
2416
|
+
trigger: "recovery_continuation", parentSourceTurnId: attempt.recoveredFromTurnId,
|
|
2417
|
+
recoveryContinuationTurnId: attempt.recoveredFromTurnId,
|
|
2418
|
+
messageId: attempt.messageId, displayPrompt: "重启后自动继续未完成的任务",
|
|
2419
|
+
});
|
|
2420
|
+
this.store.rebindPendingTurnMessages(source.localSessionId, attempt.recoveredFromTurnId, turnId);
|
|
2421
|
+
});
|
|
2422
|
+
}
|
|
2423
|
+
catch (error) {
|
|
2424
|
+
const current = this.store.getTurnAttempt(attempt.attemptId);
|
|
2425
|
+
if (current?.submissionState === "not_submitted") {
|
|
2426
|
+
this.store.updateTurnAttempt(attempt.attemptId, { status: error instanceof TurnStoppedBeforeSubmissionError ? "cancelled" : "failed" });
|
|
2427
|
+
await this.notifyRecoveryResult(attempt, "cancelled", error instanceof TurnStoppedBeforeSubmissionError
|
|
2428
|
+
? "任务、指令或运行设置已变化,已取消本次自动续跑。"
|
|
2429
|
+
: "重启后的自动续跑未能启动,请检查 Agent 状态后发送消息继续。");
|
|
2430
|
+
}
|
|
2431
|
+
if (!(error instanceof TurnStoppedBeforeSubmissionError)) {
|
|
2432
|
+
this.logger.warn({ error, attemptId: attempt.attemptId }, "Failed to continue an interrupted task after restart.");
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
finally {
|
|
2436
|
+
const latest = this.store.getSession(source.localSessionId);
|
|
2437
|
+
const active = this.runtimes.forAgent(source.agentName).getSession(source.localSessionId)?.activeTurnId;
|
|
2438
|
+
if (latest && latest.lastTurnId === attempt.recoveredFromTurnId && latest.status !== "closed" && !active) {
|
|
2439
|
+
this.store.updateSession(source.localSessionId, { status: "ready" });
|
|
2440
|
+
}
|
|
2441
|
+
if (!this.recoveryClosed)
|
|
2442
|
+
await this.scheduleNextQueuedPrompt(source.localSessionId);
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
async notifyRecoveryResult(attempt, result, message = RECOVERY_RESULTS[result]) {
|
|
2446
|
+
if (this.recoveryClosed || attempt.executionGeneration !== this.store.executionGeneration(attempt.localSessionId))
|
|
2447
|
+
return;
|
|
2448
|
+
const key = `${attempt.attemptId}:${result}`;
|
|
2449
|
+
if (this.recoveryResultNotices.has(key))
|
|
2450
|
+
return;
|
|
2451
|
+
this.recoveryResultNotices.add(key);
|
|
2452
|
+
const replyTarget = attempt.replyMessageId
|
|
2453
|
+
? { messageId: attempt.replyMessageId, replyInThread: true } : undefined;
|
|
2454
|
+
try {
|
|
2455
|
+
await this.outbound.withReplyTarget(attempt.contextKey, replyTarget, () => this.outbound.sendText(attempt.contextKey, message));
|
|
2456
|
+
}
|
|
2457
|
+
catch (error) {
|
|
2458
|
+
this.logger.warn({ error, attemptId: attempt.attemptId, result }, "Failed to send the task recovery result.");
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2325
2461
|
async markAttemptUnknown(attempt, message) {
|
|
2462
|
+
const recovering = this.recoveryRetryStartedAt.has(attempt.attemptId);
|
|
2326
2463
|
clearTimeout(this.recoveryRetryTimers.get(attempt.attemptId));
|
|
2327
2464
|
this.recoveryRetryTimers.delete(attempt.attemptId);
|
|
2328
2465
|
this.recoveryRetryStartedAt.delete(attempt.attemptId);
|
|
@@ -2344,8 +2481,11 @@ export class ProxySessionController {
|
|
|
2344
2481
|
}
|
|
2345
2482
|
if (turnId)
|
|
2346
2483
|
await this.outbound.onEvent({ type: "turn_status_unknown", sessionId: session.localSessionId, turnId, message });
|
|
2347
|
-
else
|
|
2484
|
+
else if (!recovering || this.store.schedules.getRun(attempt.attemptId))
|
|
2348
2485
|
await this.outbound.sendText(attempt.contextKey, message);
|
|
2486
|
+
if (recovering && !this.store.schedules.getRun(attempt.attemptId)) {
|
|
2487
|
+
await this.notifyRecoveryResult(attempt, "unknown", message);
|
|
2488
|
+
}
|
|
2349
2489
|
}
|
|
2350
2490
|
async reconcileRecoveredRemoteTurn(attempt, session, remoteTurnId) {
|
|
2351
2491
|
if (remoteTurnId && !attempt.turnId) {
|
|
@@ -2430,8 +2570,10 @@ export class ProxySessionController {
|
|
|
2430
2570
|
turnId, startedAt: continued.startedAt });
|
|
2431
2571
|
const synchronized = await loaded.runtime.synchronizeSession(session.localSessionId);
|
|
2432
2572
|
this.recoveryRetryStartedAt.delete(attempt.attemptId);
|
|
2433
|
-
if (synchronized.activeTurnId)
|
|
2573
|
+
if (synchronized.activeTurnId === turnId) {
|
|
2434
2574
|
this.recoveredLiveAttempts.add(attempt.attemptId);
|
|
2575
|
+
await this.notifyRecoveryResult(attempt, "reattached");
|
|
2576
|
+
}
|
|
2435
2577
|
return;
|
|
2436
2578
|
}
|
|
2437
2579
|
const oldCardTurnId = attempt.pendingTurnId ?? attempt.turnId;
|
|
@@ -2456,6 +2598,8 @@ export class ProxySessionController {
|
|
|
2456
2598
|
if (synchronized.activeTurnId) {
|
|
2457
2599
|
this.recoveredLiveAttempts.add(attempt.attemptId);
|
|
2458
2600
|
this.scheduleRecoveryRetry(attempt.attemptId, synchronized.activeTurnId);
|
|
2601
|
+
if (synchronized.activeTurnId === turnId)
|
|
2602
|
+
await this.notifyRecoveryResult(attempt, "reattached");
|
|
2459
2603
|
}
|
|
2460
2604
|
}
|
|
2461
2605
|
async continueInterruptedAttempt(attempt, session, sourceTurnId, replyTarget) {
|
|
@@ -2491,6 +2635,7 @@ export class ProxySessionController {
|
|
|
2491
2635
|
});
|
|
2492
2636
|
if (attempt.messageId)
|
|
2493
2637
|
await this.bindMessageReactionToTurn(attempt.messageId, session.localSessionId, turnId);
|
|
2638
|
+
await this.notifyRecoveryResult(attempt, "submitted");
|
|
2494
2639
|
}
|
|
2495
2640
|
async restorePersistedMessageReactions() {
|
|
2496
2641
|
const pending = this.store.listPendingMessageReactions();
|
|
@@ -2498,6 +2643,9 @@ export class ProxySessionController {
|
|
|
2498
2643
|
for (const reaction of pending) {
|
|
2499
2644
|
if (!reaction.turnId || !reaction.localSessionId)
|
|
2500
2645
|
continue;
|
|
2646
|
+
const continuation = this.store.findIncompleteTurnAttemptForSession(reaction.localSessionId);
|
|
2647
|
+
if (continuation && isPendingRecoveryContinuation(continuation) && continuation.recoveredFromTurnId === reaction.turnId)
|
|
2648
|
+
continue;
|
|
2501
2649
|
const session = this.store.getSession(reaction.localSessionId);
|
|
2502
2650
|
if (session?.lastTurnId !== reaction.turnId)
|
|
2503
2651
|
continue;
|
|
@@ -2520,6 +2668,7 @@ export class ProxySessionController {
|
|
|
2520
2668
|
await this.promptSession(record, contextKey, text, messageId, replyTarget, localImagePaths, displayPrompt);
|
|
2521
2669
|
}
|
|
2522
2670
|
async promptSession(record, contextKey, text, messageId, replyTarget, localImagePaths, displayPrompt) {
|
|
2671
|
+
this.recoverySupersededSessions.add(record.localSessionId);
|
|
2523
2672
|
const executionGeneration = this.store.executionGeneration(record.localSessionId);
|
|
2524
2673
|
if (this.llmRetryingSessions.has(record.localSessionId) || this.planHandoffs.has(record.localSessionId)) {
|
|
2525
2674
|
const queued = this.persistQueuedPrompt(record.localSessionId, contextKey, text, {
|
|
@@ -3629,6 +3778,8 @@ export class ProxySessionController {
|
|
|
3629
3778
|
this.store.updateTurnAttempt(attemptId, { pendingTurnId: pendingTurnId ?? null });
|
|
3630
3779
|
try {
|
|
3631
3780
|
this.assertExecutionGeneration(loaded.record.localSessionId, executionGeneration);
|
|
3781
|
+
if (options.recoveryContinuationTurnId)
|
|
3782
|
+
await this.assertRecoveryContinuation(loaded, attemptId);
|
|
3632
3783
|
if (options.approvedPlanTurnId && (this.planHandoffs.get(loaded.record.localSessionId) !== options.approvedPlanTurnId
|
|
3633
3784
|
|| this.sessionResets.has(loaded.record.localSessionId))) {
|
|
3634
3785
|
throw new Error("方案执行已取消。");
|
|
@@ -4271,6 +4422,19 @@ export class ProxySessionController {
|
|
|
4271
4422
|
&& (saved || this.store.getTurnRuntimeOrigin(event.turnId)))
|
|
4272
4423
|
return;
|
|
4273
4424
|
}
|
|
4425
|
+
const recoveringAttempt = event.type === "turn_completed" || event.type === "turn_cancelled" || event.type === "turn_failed"
|
|
4426
|
+
? this.store.findIncompleteTurnAttemptByTurnId(event.turnId) : undefined;
|
|
4427
|
+
const recoveryResultAttempt = recoveringAttempt && this.recoveryRetryStartedAt.has(recoveringAttempt.attemptId)
|
|
4428
|
+
&& recoveringAttempt.status !== "cancelling"
|
|
4429
|
+
&& recoveringAttempt.executionGeneration === this.store.executionGeneration(event.sessionId)
|
|
4430
|
+
? recoveringAttempt : undefined;
|
|
4431
|
+
const recoverySource = recoveryResultAttempt ? this.store.getSession(event.sessionId) : undefined;
|
|
4432
|
+
const recoveryContinuation = event.type === "turn_cancelled" && recoveryResultAttempt && recoverySource
|
|
4433
|
+
&& this.canContinueAfterRestart(recoveryResultAttempt, recoverySource)
|
|
4434
|
+
? this.store.claimRecoveryContinuation(recoveryResultAttempt.attemptId, RECOVERY_CONTINUATION_PROMPT) : undefined;
|
|
4435
|
+
if (event.type === "turn_cancelled" && recoveryResultAttempt) {
|
|
4436
|
+
event = { ...event, message: recoveryContinuation ? RECOVERY_RESULTS.continuing : RECOVERY_RESULTS.cancelled };
|
|
4437
|
+
}
|
|
4274
4438
|
if ("turnId" in event) {
|
|
4275
4439
|
const recoveryAttempt = this.recoveryRetryStartedAt.size > 0
|
|
4276
4440
|
? this.store.findIncompleteTurnAttemptByTurnId(event.turnId) : undefined;
|
|
@@ -4336,7 +4500,7 @@ export class ProxySessionController {
|
|
|
4336
4500
|
this.store.updateRuntimeSession(event.sessionId, { lastTurnId: event.turnId, lastTurnStatus: "running" });
|
|
4337
4501
|
}
|
|
4338
4502
|
else if (event.type === "turn_completed" || event.type === "turn_cancelled" || event.type === "turn_failed") {
|
|
4339
|
-
const attempt = this.store.findIncompleteTurnAttemptByTurnId(event.turnId);
|
|
4503
|
+
const attempt = this.store.findIncompleteTurnAttemptByTurnId(event.turnId) ?? recoveryResultAttempt;
|
|
4340
4504
|
if (attempt) {
|
|
4341
4505
|
clearTimeout(this.recoveryRetryTimers.get(attempt.attemptId));
|
|
4342
4506
|
this.recoveryRetryTimers.delete(attempt.attemptId);
|
|
@@ -4387,6 +4551,15 @@ export class ProxySessionController {
|
|
|
4387
4551
|
presentationError = error;
|
|
4388
4552
|
}
|
|
4389
4553
|
if (event.type === "turn_completed" || event.type === "turn_cancelled" || event.type === "turn_failed") {
|
|
4554
|
+
if (recoveryContinuation && recoverySource) {
|
|
4555
|
+
await this.submitRecoveryContinuation(recoveryContinuation, recoverySource);
|
|
4556
|
+
if (presentationError)
|
|
4557
|
+
throw presentationError;
|
|
4558
|
+
return;
|
|
4559
|
+
}
|
|
4560
|
+
if (recoveryResultAttempt) {
|
|
4561
|
+
await this.notifyRecoveryResult(recoveryResultAttempt, event.type === "turn_completed" ? "completed" : event.type === "turn_failed" ? "failed" : "cancelled");
|
|
4562
|
+
}
|
|
4390
4563
|
await this.refreshGoalCards(event.sessionId);
|
|
4391
4564
|
const terminalStatus = event.type === "turn_completed"
|
|
4392
4565
|
? "completed"
|
|
@@ -4608,6 +4781,8 @@ export class ProxySessionController {
|
|
|
4608
4781
|
return this.settingsOperations.run(sessionId, () => this.startNextQueuedPromptIfIdleNow(sessionId));
|
|
4609
4782
|
}
|
|
4610
4783
|
async startNextQueuedPromptIfIdleNow(sessionId) {
|
|
4784
|
+
if (this.recoveryClosed)
|
|
4785
|
+
return;
|
|
4611
4786
|
const executionGeneration = this.store.executionGeneration(sessionId);
|
|
4612
4787
|
if (this.planHandoffs.has(sessionId) || this.sessionResets.has(sessionId))
|
|
4613
4788
|
return;
|
|
@@ -8863,6 +9038,10 @@ function isRetryableLlmTurnFailure(message) {
|
|
|
8863
9038
|
function remoteTurnMatchesAttempt(remote, attempt) {
|
|
8864
9039
|
return Boolean(attempt.turnId && remote?.lastTurnId === attempt.turnId);
|
|
8865
9040
|
}
|
|
9041
|
+
function isPendingRecoveryContinuation(attempt) {
|
|
9042
|
+
return attempt.attemptId.startsWith("recovery_continuation:") && Boolean(attempt.recoveredFromTurnId)
|
|
9043
|
+
&& !attempt.turnId && attempt.submissionState === "not_submitted" && isIncompleteTurnAttemptStatus(attempt.status);
|
|
9044
|
+
}
|
|
8866
9045
|
function isRecoveryAttemptRecent(attempt, cutoff) {
|
|
8867
9046
|
const updatedAt = Date.parse(attempt.updatedAt);
|
|
8868
9047
|
return Number.isFinite(updatedAt) && updatedAt >= cutoff;
|