Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(provider-group): 防止崩溃恢复误报自动继续

Dashbf014352a554
diff --git a/apps/desktop/src/main/__tests__/agentInputQueue.test.ts b/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
index 86133b5e4e..789a445bab 100644
--- a/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
+++ b/apps/desktop/src/main/__tests__/agentInputQueue.test.ts
@@ -9,6 +9,7 @@ import {
getAgentFacingText,
reconcileSessionRefsForText,
sanitizeQueuedMessageForPersistence,
+ USAGE_LIMIT_RESET_AUTO_RESUME_REASON,
updateQueuedMessageContent,
updateQueuedMessageText,
} from '../../shared/agentInputQueue.js';
@@ -511,6 +512,23 @@ describe('agentInputQueue', () => {
.toHaveProperty('text', 'process-local referenced body');
});
+ // Unit contract (#6611, usage-limit-auto-continue.md): sanitizing either snapshot
+ // boundary must copy out a first-input marker without mutating the live send.
+ // Existing reference sanitization did not cover this metadata or hidden continuations.
+ it.each([false, true])('sanitizes quota activity while preserving hidden continuation metadata (hidden=%s)', hidden => {
+ const entry = queuedMessage(undefined);
+ entry.autoResume = hidden;
+ entry.autoResumeInfo = {
+ reason: USAGE_LIMIT_RESET_AUTO_RESUME_REASON,
+ attempt: 1,
+ maxAttempts: 3,
+ sessionTotal: 0,
+ };
+ const persisted = sanitizeQueuedMessageForPersistence(entry);
+ expect(persisted.autoResumeInfo).toEqual(hidden ? entry.autoResumeInfo : undefined);
+ expect(entry.autoResumeInfo.reason).toBe(USAGE_LIMIT_RESET_AUTO_RESUME_REASON);
+ });
+
it('strips transient Bot host state from crash-recovery snapshots', () => {
const entry = queuedMessage(undefined);
const href = 'cindy://bot/bot-b';
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
index cab40292c5..dfa42e3490 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
@@ -14249,6 +14249,49 @@ describe('usage-limit wait (ordinary tasks)', () => {
}
});
+ // Unit regression for #6611 / usage-limit-auto-continue.md: the old preparation-failure
+ // case did not cover a crash before durable acceptance. Exercise snapshot writes plus
+ // legacy snapshot reads through the coordinator, then both explicit release entrances.
+ it.each(['resume', 'input'] as const)('drops process-local quota activity across a crash before %s', async release => {
+ const h = createHarness();
+ const sid = 'quota-retry-crash';
+ let snapshot: AgentInputQueuedMessage[] = [];
+ h.persistQueueSnapshot.mockImplementation((_sessionId, items) => {
+ // Capture bytes at the persistence boundary, not mutable mock call arguments.
+ snapshot = JSON.parse(JSON.stringify(items));
+ });
+ await h.coordinator.ensureQueueRestored(sid);
+ h.sendToAgent.mockRejectedValueOnce(new ProviderGroupUsageLimitError({ resetAtMs: 5_000_000 }));
+ h.coordinator.enqueue(sid, makeItem('original', 'original task'));
+ await flush();
+ const candidate = h.onUsageLimitedTurnError.mock.calls.at(-1)?.[3] as number;
+ h.coordinator.armUsageLimitWait(sid, candidate, 9_000_000);
+ const acceptance = deferred<AgentInputSendResult>();
+ h.sendToAgent.mockImplementationOnce(() => acceptance.promise);
+ await h.coordinator.continueAfterUsageLimitReset(sid, candidate, INFO);
+ await flush();
+ expect(h.sendToAgent.mock.calls[1][3].persistUserMessage?.autoResumeInfo).toMatchObject(INFO);
+ expect(snapshot[0]).toMatchObject({ clientId: 'original' });
+ expect(snapshot[0].autoResumeInfo).toBeUndefined();
+
+ // Also accept a snapshot produced by the previous version, which leaked this marker.
+ snapshot[0].autoResumeInfo = INFO;
+ const restarted = createHarness();
+ restarted.setLoadQueueSnapshot(async () => snapshot);
+ await restarted.coordinator.ensureQueueRestored(sid);
+ await flush();
+ expect(restarted.sendToAgent).not.toHaveBeenCalled();
+ if (release === 'resume') restarted.coordinator.resume(sid);
+ else restarted.coordinator.enqueue(sid, makeItem('next', 'next task'), { resumeRestorePausedQueue: true });
+ await flush();
+ expect(restarted.sendToAgent).toHaveBeenCalledOnce();
+ const persisted = restarted.sendToAgent.mock.calls[0][3].persistUserMessage;
+ expect(persisted).toMatchObject({ clientId: 'original' });
+ expect(persisted?.autoResumeInfo).toBeUndefined();
+ acceptance.resolve(sendSuccess());
+ await flush();
+ });
+
it('does not keep automatic activity on a manual retry after preparation fails', async () => {
const h = createHarness();
const sid = 'quota-retry-preparation-failed';
diff --git a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
index 31f30695cf..d9363f7510 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -1481,6 +1481,7 @@ export class AgentInputCoordinator {
let items: AgentInputQueuedMessage[];
try {
items = (await this.deps.loadQueueSnapshot!(sessionId))
+ .map(sanitizeQueuedMessageForPersistence)
.map(normalizeRestoredSyntheticTrigger)
.map(restoreTrustedDesktopQueuedOrigin);
} catch (err) {
diff --git a/apps/desktop/src/shared/agentInputQueue.ts b/apps/desktop/src/shared/agentInputQueue.ts
index b06a43476f..9da14c5368 100644
--- a/apps/desktop/src/shared/agentInputQueue.ts
+++ b/apps/desktop/src/shared/agentInputQueue.ts
@@ -575,14 +575,16 @@ export function queuedMessageRetryToken(queued: AgentInputQueuedMessage): string
}
/**
- * 队列崩溃恢复快照不能持久化跨设备引用正文或瞬时 Bot 状态。两者只在当前
- * 进程内存中存活;恢复后正文保留 fail-closed 标记,Bot 状态必须重新查询,
- * 不能把崩溃前的 working/idle 当成当前事实。
+ * 队列崩溃恢复快照不能持久化跨设备引用正文、瞬时 Bot 状态或尚未落库的
+ * 原用户消息自动继续标记。它们只在当前进程内存中存活;恢复后正文保留
+ * fail-closed 标记,Bot 状态必须重新查询,手动恢复不能冒充自动继续。
*/
export function sanitizeQueuedMessageForPersistence(
item: AgentInputQueuedMessage,
): AgentInputQueuedMessage {
- let changed = false;
+ const transientUsageResume = !item.autoResume
+ && item.autoResumeInfo?.reason === USAGE_LIMIT_RESET_AUTO_RESUME_REASON;
+ let changed = transientUsageResume;
let persistedContent = item.persistedContent;
let agentReferences = item.agentReferences;
let chatMessage = item.chatMessage;
@@ -660,6 +662,7 @@ export function sanitizeQueuedMessageForPersistence(
: {}),
};
delete sanitized[HOST_ONLY_AGENT_PREFIX];
+ if (transientUsageResume) delete sanitized.autoResumeInfo;
if (!item.agentReferences) delete sanitized.agentReferences;
if (item.trustedSessionReferenceContexts) delete sanitized.trustedSessionReferenceContexts;
return sanitized;