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(chat): 发送时立即跟随最新消息 (#3453)

Dash699452ea4c39
diff --git a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
index 150e3a7a4a..8a5a0fa396 100644
--- a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
+++ b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
@@ -594,7 +594,7 @@ describe('send follow cancel generation', () => {
expect(readSendFollowCancelGeneration('session-b')).toBe(bStart + 1);
});
- it('bumps on scrollbar unpin and continued user up-scroll, not on content growth while following', () => {
+ it('bumps only when leaving the tail, not on leftover up-scroll while already away', () => {
expect(
shouldBumpSendFollowCancelOnScroll({
wasNearBottom: true,
@@ -610,7 +610,7 @@ describe('send follow cancel generation', () => {
scrollDelta: -40,
directionDeadZonePx: 2,
}),
- ).toBe(true);
+ ).toBe(false);
expect(
shouldBumpSendFollowCancelOnScroll({
wasNearBottom: true,
@@ -787,7 +787,9 @@ describe('MessageStream send-window handoff wiring', () => {
expect(source).toContain('subscribeFollowLatestRequests');
expect(source).toContain('readFollowLatestRequestKey(sessionId)');
expect(source).toContain('pinToBottom();');
- expect(source).toContain('bumpSendFollowCancelGeneration(sessionId)');
+ expect(source).toMatch(
+ /const unpinAutoFollowForUserUpIntent = useCallback\(\(\) => \{\s*if \(!isNearBottomRef\.current\) return;\s*bumpSendFollowCancelGeneration\(sessionId\);/,
+ );
expect(source).toContain('shouldBumpSendFollowCancelOnScroll({');
expect(source).toContain('knownUserMessageIds: knownUserMessageIdsRef.current');
expect(source).toContain('collectKnownUserMessageIds(messages,');
@@ -799,19 +801,28 @@ describe('MessageStream send-window handoff wiring', () => {
expect(source).toContain('cancelFocusJump({ consumeDeferredDelete: true });');
});
- it('session view commits follow-latest only after accept and unchanged scroll generation', () => {
+ it('session view requests follow-latest at send dispatch, not after accept', () => {
const source = readFileSync(
resolve(__dirname, '../features/cc-agent/CCAgentSessionView.tsx'),
'utf8',
);
expect(source).toContain('tryRequestFollowLatest({');
- expect(source).toContain('requestFollowLatest(sessionId, followStartGeneration)');
expect(source).toContain(
'const followStartGeneration = readSendFollowCancelGeneration(sessionId);',
);
+ expect(source).toContain('if (sessionId) requestFollowLatest(sessionId, followStartGeneration);');
+ expect(source).toMatch(
+ /if \(sessionId\) requestFollowLatest\(sessionId, followStartGeneration\);\s*const accepted = await sendMessage\(/,
+ );
+ expect(source).toMatch(
+ /if \(sessionId\) requestFollowLatest\(sessionId, followStartGeneration\);\s*const accepted = await steerMessage\(/,
+ );
+ expect(source).not.toMatch(
+ /const accepted = await sendMessage\([\s\S]{0,400}requestFollowLatest\(sessionId, followStartGeneration\)/,
+ );
});
- it('edit-resend and blocked resend request follow-latest through the same owner', () => {
+ it('edit-resend and blocked resend request follow-latest at dispatch', () => {
const source = readFileSync(
resolve(__dirname, '../components/chat/UserMessageEditBox.tsx'),
'utf8',
@@ -820,6 +831,8 @@ describe('MessageStream send-window handoff wiring', () => {
expect(source).toContain(
'const followStartGeneration = readSendFollowCancelGeneration(sessionId);',
);
- expect(source).toContain('if (accepted)');
+ expect(source).toMatch(
+ /tryRequestFollowLatest\(\{[\s\S]*?\}\);\s*if \(onCommitOverride\)/,
+ );
});
});
diff --git a/apps/desktop/src/renderer/__tests__/userMessageEditBox.test.ts b/apps/desktop/src/renderer/__tests__/userMessageEditBox.test.ts
index 0c6f5e6293..1eb557f2a7 100644
--- a/apps/desktop/src/renderer/__tests__/userMessageEditBox.test.ts
+++ b/apps/desktop/src/renderer/__tests__/userMessageEditBox.test.ts
@@ -101,7 +101,7 @@ describe('UserMessageEditBox — idle 发送', () => {
expect(props.onRequestStop).not.toHaveBeenCalled();
});
- it('编辑重发受理后请求跟底;入队失败或等待期间上翻则不跟底', async () => {
+ it('编辑重发在派发时就请求跟底,不等受理,等待期间上翻也不撤票', async () => {
const acceptedId = `edit-follow-ok-${Date.now()}`;
commitMock.mockResolvedValueOnce(true);
const accepted = renderBox({ sessionId: acceptedId });
@@ -116,22 +116,23 @@ describe('UserMessageEditBox — idle 发送', () => {
const failed = renderBox({ sessionId: failedId });
fireEvent.click(failed.sendBtn);
await waitFor(() => expect(failed.props.onSent).toHaveBeenCalledTimes(1));
- expect(readFollowLatestRequestKey(failedId)).toBe(0);
+ expect(readFollowLatestRequestKey(failedId)).toBe(1);
failed.unmount();
vi.clearAllMocks();
- const cancelledId = `edit-follow-cancel-${Date.now()}`;
+ const dispatchedId = `edit-follow-dispatch-${Date.now()}`;
let release: (value: boolean) => void = () => {};
commitMock.mockImplementationOnce(
() => new Promise<boolean>((resolve) => { release = resolve; }),
);
- const cancelled = renderBox({ sessionId: cancelledId });
- fireEvent.click(cancelled.sendBtn);
+ const dispatched = renderBox({ sessionId: dispatchedId });
+ fireEvent.click(dispatched.sendBtn);
await waitFor(() => expect(commitMock).toHaveBeenCalledTimes(1));
- bumpSendFollowCancelGeneration(cancelledId);
+ expect(readFollowLatestRequestKey(dispatchedId)).toBe(1);
+ bumpSendFollowCancelGeneration(dispatchedId);
await act(async () => { release(true); });
- await waitFor(() => expect(cancelled.props.onSent).toHaveBeenCalledTimes(1));
- expect(readFollowLatestRequestKey(cancelledId)).toBe(0);
+ await waitFor(() => expect(dispatched.props.onSent).toHaveBeenCalledTimes(1));
+ expect(readFollowLatestRequestKey(dispatchedId)).toBe(1);
});
it('被拦消息重发成功后请求跟底', async () => {
diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsx
index ccc2ea267c..8bf2c03be5 100644
--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx
+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx
@@ -4194,8 +4194,8 @@ export function MessageStream({
// shouldUnpinOnUpIntent),本回调只负责翻转:ref 与 state 同步更新(F2 不
// 变量);unreadCount 不动 — 它只在回底时清零。
const unpinAutoFollowForUserUpIntent = useCallback(() => {
- bumpSendFollowCancelGeneration(sessionId);
if (!isNearBottomRef.current) return;
+ bumpSendFollowCancelGeneration(sessionId);
isNearBottomRef.current = false;
setIsNearBottom(false);
}, [sessionId]);
diff --git a/apps/desktop/src/renderer/components/chat/UserMessageEditBox.tsx b/apps/desktop/src/renderer/components/chat/UserMessageEditBox.tsx
index 65888b948b..c3eef81831 100644
--- a/apps/desktop/src/renderer/components/chat/UserMessageEditBox.tsx
+++ b/apps/desktop/src/renderer/components/chat/UserMessageEditBox.tsx
@@ -209,7 +209,12 @@ export function UserMessageEditBox({
? [rebuiltSlashRange]
: undefined;
const followStartGeneration = readSendFollowCancelGeneration(sessionId);
- let accepted = true;
+ // 点发送就跟底。等受理会让阅读历史时的残留上翻把票作废。
+ tryRequestFollowLatest({
+ sourceSessionId: sessionId,
+ currentSessionId: sessionId,
+ startGeneration: followStartGeneration,
+ });
if (onCommitOverride) {
// 被拦消息:普通重发(不 rewind)。失败抛错落入下方 catch 保留编辑态。
await onCommitOverride({
@@ -220,7 +225,7 @@ export function UserMessageEditBox({
...(preservedSlashCommandRanges !== undefined ? { slashCommandRanges: preservedSlashCommandRanges } : {}),
});
} else {
- accepted = await commitEditAndResendWithRunningRetry({
+ await commitEditAndResendWithRunningRetry({
sessionId,
clientId: messageClientId,
text: submitText,
@@ -233,18 +238,6 @@ export function UserMessageEditBox({
...(preservedSlashCommandRanges !== undefined ? { slashCommandRanges: preservedSlashCommandRanges } : {}),
});
}
- // 先归零守卫再 onSent:onSent 让父组件立刻卸载本组件,晚于它的 setState
- // 在已卸载组件上是无效 no-op(bot review 指出的死代码顺序问题)。
- // 编辑框挂在 MessageStream(key=sessionId) 里,切走即卸载;跟底 store 按
- // session 隔离,这里 bump 不会钉到别的会话。CCAgentSessionView 会在
- // `/cc-agent/:id` 切换后存活,所以那边才比对 sessionIdRef.current。
- if (accepted) {
- tryRequestFollowLatest({
- sourceSessionId: sessionId,
- currentSessionId: sessionId,
- startGeneration: followStartGeneration,
- });
- }
submittingRef.current = false;
setSubmitting(false);
onSent();
diff --git a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
index c85007f2b5..0124b07e63 100644
--- a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
+++ b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
@@ -486,16 +486,15 @@ export function bumpSendFollowCancelGeneration(sessionId: string | null | undefi
export function shouldBumpSendFollowCancelOnScroll({
wasNearBottom,
effectiveNearBottom,
- scrollDelta,
- directionDeadZonePx,
}: {
wasNearBottom: boolean;
effectiveNearBottom: boolean;
scrollDelta: number;
directionDeadZonePx: number;
}): boolean {
- if (wasNearBottom && !effectiveNearBottom) return true;
- return !effectiveNearBottom && scrollDelta < -directionDeadZonePx;
+ // Only leaving the tail cancels a pending follow. Continued up-scroll while
+ // already away is leftover reading inertia and must not void the next send.
+ return wasNearBottom && !effectiveNearBottom;
}
export function shouldCommitFollowLatestRequest({
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
index 595afdb77a..cd2a1b1331 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
@@ -2956,6 +2956,10 @@ export function CCAgentSessionView({
pending.onDeferredAccepted?.();
const resumedSessionId = sessionId;
if (resumedSessionId) {
+ requestFollowLatest(
+ resumedSessionId,
+ readSendFollowCancelGeneration(resumedSessionId),
+ );
void dispatchDeferredUiAssignment(resumedSessionId, undefined, {
waitForLeadHistory: false,
}).catch((err) => {
@@ -2991,6 +2995,7 @@ export function CCAgentSessionView({
: undefined;
const dispatch = pending.deliveryMode === 'steer' ? steerMessage : sendMessage;
const followStartGeneration = readSendFollowCancelGeneration(sessionId);
+ requestFollowLatest(sessionId, followStartGeneration);
const accepted = await dispatch(
slashDispatch.message,
pending.model,
@@ -3029,7 +3034,6 @@ export function CCAgentSessionView({
);
if (accepted) {
pending.onDeferredAccepted?.();
- requestFollowLatest(sessionId, followStartGeneration);
const resumedSessionId = sessionId;
if (resumedSessionId) {
void dispatchDeferredUiAssignment(resumedSessionId, undefined).catch((err) => {
@@ -3175,6 +3179,7 @@ export function CCAgentSessionView({
});
if (slashDispatch.handled) {
if (slashDispatch.accepted && sessionId) {
+ requestFollowLatest(sessionId, readSendFollowCancelGeneration(sessionId));
void dispatchDeferredUiAssignment(sessionId, undefined, {
waitForLeadHistory: false,
}).catch((err) => {
@@ -3308,6 +3313,7 @@ export function CCAgentSessionView({
};
if (deliveryMode === 'steer') {
const followStartGeneration = readSendFollowCancelGeneration(sessionId);
+ if (sessionId) requestFollowLatest(sessionId, followStartGeneration);
const accepted = await steerMessage(
message,
model,
@@ -3319,7 +3325,6 @@ export function CCAgentSessionView({
sendOptions,
);
if (accepted && sessionId) {
- requestFollowLatest(sessionId, followStartGeneration);
void dispatchDeferredUiAssignment(sessionId, undefined).catch((err) => {
log.error('recover deferred Worker assignment after user message failed', err);
toast.error(t('newChat.collaboration.assignmentFailed'));
@@ -3328,6 +3333,7 @@ export function CCAgentSessionView({
return accepted;
}
const followStartGeneration = readSendFollowCancelGeneration(sessionId);
+ if (sessionId) requestFollowLatest(sessionId, followStartGeneration);
const accepted = await sendMessage(
message,
model,
@@ -3339,7 +3345,6 @@ export function CCAgentSessionView({
sendOptions,
);
if (accepted && sessionId) {
- requestFollowLatest(sessionId, followStartGeneration);
void dispatchDeferredUiAssignment(sessionId, undefined).catch((err) => {
log.error('recover deferred Worker assignment after user message failed', err);
toast.error(t('newChat.collaboration.assignmentFailed'));
@@ -3832,6 +3837,7 @@ export function CCAgentSessionView({
// 必须 await:sendMessage 在设备离线 / 访问被撤销 / 远端 enqueue 拒绝时不抛错,
// 而是 resolve false —— 不等它就丢副本,正文会从界面和磁盘上一起消失(codex P1)。
const followStartGeneration = readSendFollowCancelGeneration(sessionId);
+ requestFollowLatest(sessionId, followStartGeneration);
const delivered = await deliverRecoverableHandoff(sessionId, () =>
sendMessage(
pendingText,
@@ -3863,7 +3869,6 @@ export function CCAgentSessionView({
),
);
if (delivered) {
- requestFollowLatest(sessionId, followStartGeneration);
void dispatchDeferredUiAssignment(sessionId, deferredUiAssignment).catch((err) => {
log.error('deferred Worker assignment after first message failed', err);
toast.error(t('newChat.collaboration.assignmentFailed'));