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(interactions): 统一手机回答收尾并保护草稿免于缓存回收

Dash84b5a44926e6
diff --git a/apps/desktop/src/renderer/__tests__/askUserDoneRace.test.ts b/apps/desktop/src/renderer/__tests__/askUserDoneRace.test.ts
index 0a603a83fc..add1150227 100644
--- a/apps/desktop/src/renderer/__tests__/askUserDoneRace.test.ts
+++ b/apps/desktop/src/renderer/__tests__/askUserDoneRace.test.ts
@@ -262,6 +262,23 @@ describe('ask_user 与 done 的时序', () => {
expect(makerChatStore.getLightSnapshot(SESSION_ID).pendingAskUser?.requestId).toBe('new');
});
+ it('keeps an edited question when more than 20 other tasks enter the cache', () => {
+ emitAskUserRequest('async-1', [{ question: 'Original?' }], 'async');
+ const draft = { requestId: 'async-1', currentIndex: 0, answers: {}, started: true, customInput: 'Still typing' };
+ makerChatStore.setAskUserDraft(SESSION_ID, draft);
+ onInteractionDismissed?.({ sessionId: SESSION_ID, requestId: 'async-1', reason: 'turn_completed' });
+ const others = Array.from({ length: 25 }, (_, i) => `draft-lru-${i}`);
+ try {
+ for (const id of others) makerChatStore.insertSystemCard(id, 'status', { label: id });
+ expect(makerChatStore.getLightSnapshot(SESSION_ID).askUserDraft).toBe(draft);
+ expect(makerChatStore.getLightSnapshot(SESSION_ID).pendingAskUser?.requestId).toBe('async-1');
+ // Ordinary inactive tasks still get evicted.
+ expect(makerChatStore.getSnapshot(others[0]).messages).toHaveLength(0);
+ } finally {
+ for (const id of others) makerChatStore.purgeSession(id);
+ }
+ });
+
it('does not protect untouched cards or synchronous drafts', () => {
emitAskUserRequest('untouched', [{ question: 'Q?' }], 'async');
makerChatStore.setAskUserDraft(SESSION_ID, { requestId: 'untouched', currentIndex: 0, answers: {} });
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 6457955b3e..0440f8bcdc 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -3659,14 +3659,7 @@ function _evictLruIfNeeded(): void {
// 驱逐会丢 taskUpdates 让 sidebar spinner 熄灭。远程会话豁免(同折算口径)。
return (
s &&
- !s.isStreaming &&
- !s.agentStatus.isRunning &&
- !hasBackgroundAgentWork(id, s) &&
- !remoteOptimisticSends.has(id) &&
- ![...remoteOptimisticMaterializationRecoveries.values()].some(
- (recovery) =>
- recovery.sessionId === id && isRemoteOptimisticMaterializationRecoveryActive(recovery),
- ) &&
+ !_isSessionBusy(id, s) &&
!_activeViewSessions.has(id)
);
});
diff --git a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
index 464f432b17..c86a88bf8d 100644
--- a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
+++ b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
@@ -100,6 +100,41 @@ async function type(id: string, value: string) {
});
}
+it.each(['push-first', 'receipt-first', 'lost-receipt', 'other-client'] as const)(
+ 'finalizes retained answers without stale snapshot resurrection: %s',
+ async (order) => {
+ const item = { persistId: 'original-row', request: {
+ kind: 'ask_user_question', requestId, delivery: 'async', questions: [{ question: 'Original?' }],
+ } };
+ remoteSessionStore.setPendingInteractions('s1', [item]);
+ await act(async () => root.render(<Harness />));
+ await type('interaction.ask.textInput', 'Finished');
+ let accept!: (value: { accepted: boolean }) => void;
+ let reject!: (error: Error) => void;
+ resolveInteraction.mockImplementationOnce(() => new Promise((resolve, fail) => {
+ accept = resolve; reject = fail;
+ }));
+ if (order !== 'other-client') await click('interaction.ask.submitButton');
+ if (order === 'receipt-first') await act(async () => accept({ accepted: true }));
+ await act(async () => {
+ remoteSessionStore.applyRemotePush('d1', 'maker:interaction-dismissed', {
+ sessionId: 's1', requestId, reason: 'resolved',
+ decision: { kind: 'ask_user_question', answers: { 'Original?': 'Finished' } },
+ });
+ // A snapshot requested before the answer may arrive after its push.
+ remoteSessionStore.setPendingInteractions('s1', [item]);
+ });
+ expect(remoteSessionStore.getVisiblePendingInteractions('s1')).toEqual([]);
+ expect(readAskUserDraft(requestId)).toBeNull();
+ expect(host.querySelector('[data-testid="interaction.ask.card"]')).toBeNull();
+ if (order === 'push-first') await act(async () => accept({ accepted: true }));
+ if (order === 'lost-receipt') await act(async () => reject(new Error('receipt lost')));
+ expect(asyncReply.input.enqueue).not.toHaveBeenCalled();
+ expect(readAskUserDraft(requestId)).toBeNull();
+ expect(onError.mock.calls.every(([error]) => error === null)).toBe(true);
+ },
+);
+
it.each(['send', 'close', 'stopped', 'stopped-empty', 'already-answered', 'unconfirmed', 'unknown-steer'])('retains edited async answers across expiry and new questions: %s', async (action) => {
remoteSessionStore.setPendingInteractions('s1', [{
persistId: 'original-row',
diff --git a/apps/mobile/src/session/InteractionPanel.tsx b/apps/mobile/src/session/InteractionPanel.tsx
index dad8f92bad..8b5bafac34 100644
--- a/apps/mobile/src/session/InteractionPanel.tsx
+++ b/apps/mobile/src/session/InteractionPanel.tsx
@@ -606,8 +606,7 @@ function InteractionItem({
await resolveInteractionResilient(maker, sessionId, currentRequestId, decision);
}
if (item.retainedDraft) {
- remoteSessionStore.dismissInteraction(sessionId, currentRequestId);
- remoteSessionStore.settleOptimisticInteractionDismiss(sessionId, currentRequestId, { kind: 'confirmed' });
+ remoteSessionStore.confirmProtectedQuestion(sessionId, currentRequestId);
}
remoteSessionStore.releaseProtectedQuestion(sessionId, currentRequestId);
if (kind === 'ask_user_question') clearAskUserDraft(currentRequestId);
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 435d8f579e..13ed5976ee 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -43,6 +43,7 @@ import { isDefaultDraftSessionTitle } from '@cindy/maker-shared/session-title';
import { CINDY_MAKE_ENABLED, isCindyMakeSource } from '@cindy/maker-shared/cindy-make-feature';
import { EMPTY_INPUT_PROJECTION, normalizeInputProjection } from '@/session/inputProjection';
import { sortPendingInteractions } from '@/session/interactionModel';
+import { clearAskUserDraft } from '@/session/interactionDraftStore';
import { applySessionModelPrefPush } from '@/session/sessionModelMirror';
import {
createLatestWriteGuard,
@@ -4786,7 +4787,7 @@ export const remoteSessionStore = {
const requestId = readString(payload, 'requestId');
if (sessionId && requestId) {
if (payload.reason === 'resolved' && !(isRecord(payload.decision) && payload.decision.dismissed === true)) {
- this.releaseProtectedQuestion(sessionId, requestId);
+ this.confirmProtectedQuestion(sessionId, requestId);
}
// 注意:这里**不**提前回收延长抑制条目——push 到达只说明被控端已确认,
// 不排除仍有「决定提交前发出、此刻还在途」的旧 getPendingInteractions
@@ -5588,6 +5589,16 @@ export const remoteSessionStore = {
emit();
},
+ /** Both the resolve receipt and a resolved push complete the same draft. */
+ confirmProtectedQuestion(sessionId: string, requestId: string): void {
+ if (protectedQuestions.get(sessionId)?.request.requestId !== requestId) return;
+ // Register the existing stale-snapshot suppression before publishing removal.
+ this.settleOptimisticInteractionDismiss(sessionId, requestId, { kind: 'confirmed' });
+ clearAskUserDraft(requestId);
+ this.dismissInteraction(sessionId, requestId);
+ this.releaseProtectedQuestion(sessionId, requestId);
+ },
+
getVisiblePendingInteractions(sessionId: string): PendingInteraction[] {
const raw = this.getPendingInteractions(sessionId);
const retained = protectedQuestions.get(sessionId);