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(history): 保留思考快照直到持久化完成

Dash9bd98f6d3fdf
diff --git a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts
index 948f63bab9..3f4d7f8dbe 100644
--- a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts
+++ b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts
@@ -87,6 +87,7 @@ import {
onInteractionResolved,
onThinkingEvent,
getSessionThinkingSnapshots,
+ clearSessionThinkingSnapshots,
flushAssistantBlock,
sealAssistantBlockForLateFinal,
flushOrphanToolResults,
@@ -1684,6 +1685,62 @@ describe('done orphan:残留 buffer 在 turn 末 flush', () => {
});
describe('thinking persistence', () => {
+ it('keeps final thinking across terminal resets until the queued write succeeds', async () => {
+ let finishWrite!: () => void;
+ vi.mocked(createMessage).mockImplementationOnce(() => new Promise((resolve) => {
+ finishWrite = () => resolve({} as Awaited<ReturnType<typeof createMessage>>);
+ }));
+ onThinkingEvent(SESSION, { stage: 'final', blockId: 'pending-final', text: 'complete thought' }, null);
+ resetTurnPersistState(SESSION);
+ await flushWrites();
+ try {
+ resetTurnPersistState(SESSION);
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([expect.objectContaining({
+ clientId: 'pending-final', content: expect.objectContaining({ text: 'complete thought' }),
+ })]);
+ } finally {
+ finishWrite();
+ await flushWrites();
+ }
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([]);
+ });
+
+ it.each(['clear', 'session', 'tree', 'owner'] as const)(
+ 'keeps failed final thinking until explicit %s cleanup', async (boundary) => {
+ vi.mocked(createMessage).mockRejectedValueOnce(new Error('write failed'));
+ onThinkingEvent(SESSION, { stage: 'final', blockId: 'failed-final', text: 'recoverable' }, null);
+ resetTurnPersistState(SESSION);
+ await flushWrites();
+ expect(getSessionThinkingSnapshots(SESSION)).toHaveLength(1);
+ if (boundary === 'clear') noteSessionClearBoundary(SESSION, Date.now());
+ if (boundary === 'session') clearSessionPersistState(SESSION);
+ if (boundary === 'tree') clearSessionThinkingSnapshots(SESSION);
+ if (boundary === 'owner') ownerScopeState.current = false;
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([]);
+ ownerScopeState.current = true;
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([]);
+ },
+ );
+
+ it('does not let an old write release a replacement snapshot after history cleanup', async () => {
+ let finishWrite!: () => void;
+ vi.mocked(createMessage).mockImplementationOnce(() => new Promise((resolve) => {
+ finishWrite = () => resolve({} as Awaited<ReturnType<typeof createMessage>>);
+ }));
+ onThinkingEvent(SESSION, { stage: 'final', blockId: 'reused', text: 'old' }, null);
+ await flushWrites();
+ clearSessionThinkingSnapshots(SESSION);
+ onThinkingEvent(SESSION, { stage: 'delta', blockId: 'reused', text: 'new' }, null);
+ finishWrite();
+ await flushWrites();
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([expect.objectContaining({
+ clientId: 'reused', content: expect.objectContaining({ text: 'new' }),
+ })]);
+ onThinkingEvent(SESSION, { stage: 'redacted', blockId: 'reused' }, null);
+ expect(getSessionThinkingSnapshots(SESSION)).toEqual([]);
+ await flushWrites();
+ });
+
it('recovers thinking accumulated while folded, then rejects it after an owner change', () => {
onThinkingEvent(SESSION, { stage: 'start', blockId: 'live-thought' }, null);
onThinkingEvent(SESSION, { stage: 'delta', blockId: 'live-thought', text: 'first ' }, null);
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 82edf990e4..056f9e3e4d 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -477,6 +477,7 @@ import {
import { libraryExtraDirSyncTargets } from './libraryExtraDirSyncTargets.js';
import {
clearSessionPersistState,
+ clearSessionThinkingSnapshots,
consumeLastAssistantPersistId,
consumeLastTopLevelAssistantPersistId,
drainPersistQueue,
@@ -16524,6 +16525,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
});
return { result, hiddenClientIds, now };
});
+ clearSessionThinkingSnapshots(sessionId);
resetTurnPersistState(sessionId);
// 多窗口与 device-link 控制端先清旧投影,再按 DB 真相补当前活动路径。
diff --git a/apps/desktop/src/main/messagePersistBroadcaster.ts b/apps/desktop/src/main/messagePersistBroadcaster.ts
index 5709443d8d..8b4acf0d01 100644
--- a/apps/desktop/src/main/messagePersistBroadcaster.ts
+++ b/apps/desktop/src/main/messagePersistBroadcaster.ts
@@ -86,10 +86,13 @@ const assistantBlocks = new Map<string, AssistantBlock>();
// so expanding midway does not depend on deltas a collapsed controller never received.
const historyThinkingBlocks = new Map<string, Map<string, Message>>();
const historyThinkingOwners = new Map<string, OwnerScope>();
+export function clearSessionThinkingSnapshots(sessionId: string): void {
+ historyThinkingBlocks.delete(sessionId);
+ historyThinkingOwners.delete(sessionId);
+}
export function getSessionThinkingSnapshots(sessionId: string): Message[] {
if (!isOwnerScopeCurrent(historyThinkingOwners.get(sessionId) ?? null)) {
- historyThinkingBlocks.delete(sessionId);
- historyThinkingOwners.delete(sessionId);
+ clearSessionThinkingSnapshots(sessionId);
return [];
}
return [...(historyThinkingBlocks.get(sessionId)?.values() ?? [])];
@@ -161,8 +164,7 @@ export function noteSessionClearBoundary(sessionId: string, clearedAt: string |
if (!Number.isFinite(parsed)) return;
const current = clearBoundaryBySession.get(sessionId);
if (current === undefined || parsed > current) {
- historyThinkingBlocks.delete(sessionId);
- historyThinkingOwners.delete(sessionId);
+ clearSessionThinkingSnapshots(sessionId);
clearBoundaryBySession.set(sessionId, parsed);
sealedAssistantLateFinalBySession.delete(sessionId);
// A cleared transcript must not be revived by a late terminal update from an
@@ -1838,8 +1840,8 @@ export function flushOrphanToolResults(sessionId: string, agentMeta: AgentMeta |
* + knownToolUseIds + lastAgentMeta。必须在 flushOrphanToolResults 之后调用。
*/
export function resetTurnPersistState(sessionId: string): void {
- historyThinkingBlocks.delete(sessionId);
- historyThinkingOwners.delete(sessionId);
+ // Event-stream completion is not a persistence barrier. Thinking snapshots
+ // survive until their write succeeds or an explicit history/owner cleanup.
toolResultIdByToolUseId.delete(sessionId);
pendingFullTextByToolUseId.delete(sessionId);
toolResultContentByClientId.delete(sessionId);
@@ -2448,8 +2450,7 @@ export function clearCodexPlanRowsForSession(sessionId: string): void {
}
export function clearSessionPersistState(sessionId: string): void {
- historyThinkingBlocks.delete(sessionId);
- historyThinkingOwners.delete(sessionId);
+ clearSessionThinkingSnapshots(sessionId);
clearCodexPlanRowsForSession(sessionId);
assistantBlocks.delete(sessionId);
sealedAssistantLateFinalBySession.delete(sessionId);