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(desktop): 修复流式消息偶发停止自动跟随 (#3604)

DavidShenXD257597b8c996
diff --git a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
index 8a5a0fa396..d5b78b487c 100644
--- a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
+++ b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts
@@ -26,6 +26,7 @@ import {
resolveNearBottomOnScroll,
resolveRenderPinDecision,
resolveSendWindowHandoff,
+ resolveWindowCoverageLossAction,
selectTailUserMessageId,
isVerticalScrollbarPress,
shouldRepinOnDownIntent,
@@ -711,6 +712,30 @@ describe('resolveSendWindowHandoff', () => {
});
});
+describe('resolveWindowCoverageLossAction', () => {
+ it('hands an automatically expanded window back to the tail when append breaks coverage', () => {
+ expect(
+ resolveWindowCoverageLossAction({
+ hasWindowAnchor: true,
+ wasCoveringEnd: true,
+ windowCoversEnd: false,
+ wasFollowingTail: true,
+ }),
+ ).toBe('handoff-to-tail');
+ });
+
+ it('preserves an active history anchor when append breaks coverage away from the tail', () => {
+ expect(
+ resolveWindowCoverageLossAction({
+ hasWindowAnchor: true,
+ wasCoveringEnd: true,
+ windowCoversEnd: false,
+ wasFollowingTail: false,
+ }),
+ ).toBe('preserve-anchor');
+ });
+});
+
describe('resolveLastUserMessageObservation', () => {
it('seeds a restored user tail hydrated after mount without treating it as a send', () => {
expect(
@@ -780,6 +805,9 @@ describe('MessageStream send-window handoff wiring', () => {
it('clears any anchored window on a local send and only defers pin for stale slices', () => {
const source = readFileSync(resolve(__dirname, '../components/chat/MessageStream.tsx'), 'utf8');
expect(source).toContain('resolveSendWindowHandoff({');
+ expect(source).toContain('resolveWindowCoverageLossAction({');
+ expect(source).toContain("if (coverageLossAction === 'handoff-to-tail')");
+ expect(source).toContain("if (coverageLossAction === 'preserve-anchor')");
expect(source).toContain('if (windowHandoff.clearWindowAnchor)');
expect(source).toContain('if (decision.pinToBottom && !windowHandoff.deferPinToNextRender)');
expect(source).not.toContain('realTailUserSendOutsideWindow');
diff --git a/apps/desktop/src/renderer/__tests__/focusScrollLifecycle.test.ts b/apps/desktop/src/renderer/__tests__/focusScrollLifecycle.test.ts
index dff7853bac..b9efef605c 100644
--- a/apps/desktop/src/renderer/__tests__/focusScrollLifecycle.test.ts
+++ b/apps/desktop/src/renderer/__tests__/focusScrollLifecycle.test.ts
@@ -317,6 +317,19 @@ describe('MessageStream focus cancellation wiring', () => {
expect(railJumpEffect).toContain('topOffset: NAV_RAIL_JUMP_TOP_OFFSET_PX');
});
+ it('records navigation intent before requesting either a current-window or off-window target', () => {
+ const railJump = sourceBetween(
+ 'const handleNavRailJump = useCallback(',
+ 'useLayoutEffect(() => {\n if (!railJumpRequest) return;',
+ );
+ const unpinIndex = railJump.indexOf('isNearBottomRef.current = false;');
+ const requestIndex = railJump.indexOf(
+ 'setRailJumpRequest({ id: clientId, seq: railJumpSeqRef.current });',
+ );
+ expect(unpinIndex).toBeGreaterThanOrEqual(0);
+ expect(requestIndex).toBeGreaterThan(unpinIndex);
+ });
+
it('re-resolves the saved target before consuming an earlier deferred deletion', () => {
const settlement = sourceBetween(
'const settleChipJump = useCallback(',
diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsx
index 0e4dadbd50..022a842725 100644
--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx
+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx
@@ -314,6 +314,7 @@ import {
resolveLastUserMessageObservation,
resolveRenderPinDecision,
resolveSendWindowHandoff,
+ resolveWindowCoverageLossAction,
selectTailUserMessageId,
readFollowLatestRequestKey,
shouldBumpSendFollowCancelOnScroll,
@@ -4063,14 +4064,27 @@ export function MessageStream({
const anchoredForwardItemsRef = useRef(anchoredForwardItems);
anchoredForwardItemsRef.current = anchoredForwardItems;
- // render-window-bidirectional P1 fix: 新消息导致锚定窗口不再覆盖末尾时,
- // 重置 near-bottom 以触发未读提示(覆盖末尾→清锚回默认窗的逻辑在 handleScroll 里)。
+ // 新 item 导致锚定窗口不再覆盖末尾时,先保留此前的跟随意图:自动补历史建立的
+ // 锚点只是实现细节,用户仍在跟随就清锚回默认尾窗;用户已经离底才保留历史窗口。
const prevWindowCoversEndRef = useRef(windowCoversEnd);
useLayoutEffect(() => {
const wasCovering = prevWindowCoversEndRef.current;
prevWindowCoversEndRef.current = windowCoversEnd;
- if (firstVisibleItemKey !== null && wasCovering && !windowCoversEnd) {
+ const coverageLossAction = resolveWindowCoverageLossAction({
+ hasWindowAnchor: firstVisibleItemKey !== null,
+ wasCoveringEnd: wasCovering,
+ windowCoversEnd,
+ wasFollowingTail: isNearBottomRef.current,
+ });
+ if (coverageLossAction === 'handoff-to-tail') {
+ isNearBottomRef.current = true;
+ setIsNearBottom(true);
+ setUnreadCount(0);
+ setFirstVisibleItemKey(null);
+ return;
+ }
+ if (coverageLossAction === 'preserve-anchor') {
isNearBottomRef.current = false;
setIsNearBottom(false);
return;
@@ -5687,6 +5701,12 @@ export function MessageStream({
// 导航条目标要到 layout effect 才能确认仍存在且 DOM 已就绪,因此这里不能提前消费
// focus 期间延期的删除补偿:先重放补偿,目标有效时后续导航再覆盖最终落点。
cancelFocusJump();
+ // 点击本身就是离开尾部的明确意图,必须在 request 进入下一次 render 前同步写入。
+ // 否则同一批流式 append 的 coverage-loss layout effect 会先按旧跟随态清掉锚点,
+ // 当前窗口内、但默认尾窗外的目标会在后续 rail effect 滚动时被卸载。
+ restoringRef.current = false;
+ isNearBottomRef.current = false;
+ setIsNearBottom(false);
railJumpSeqRef.current += 1;
setRailJumpRequest({ id: clientId, seq: railJumpSeqRef.current });
},
@@ -5716,11 +5736,8 @@ export function MessageStream({
) as HTMLElement | null;
if (!el) return;
lastAppliedRailJumpRef.current = railJumpRequest.seq;
- // 从贴底态往上跳必须先解除 auto-follow 钉底,否则流式期间 pin effect 会把
- // 视口拽回底部(focus-jump 同款处理;chip 不需要是因为它只在已上滚时出现)。
- restoringRef.current = false;
- isNearBottomRef.current = false;
- setIsNearBottom(false);
+ // auto-follow 已由点击处理器在 request 进入 render 前解除,避免本轮更早的
+ // coverage-loss effect 清掉当前窗口内、默认尾窗外的导航目标。
// smooth scroll 途经顶部区域时抑制 expandWindow/onLoadMore(chip-jump 协议,
// 解抑靠用户 wheel/touch/keydown + 安全兜底 timer)。
beginChipJump({
diff --git a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
index 0124b07e63..abdd147390 100644
--- a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
+++ b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts
@@ -362,10 +362,11 @@ export interface ResolveSendWindowHandoff {
* After a local send, leave any historical render window so later assistant /
* tool items keep auto-following the real tail.
*
- * An anchored window that still covers the end at send time would otherwise
- * stay anchored; the next appended items flip `windowCoversEnd` to false and
- * the stream treats that as "left the bottom", stopping follow. External
- * injections must not take this handoff (#2194).
+ * An anchored window that still covers the end at send time used to stop
+ * following as soon as the next item moved the real tail past its bound. Local
+ * sends hand off eagerly so the optimistic row is shown at the real tail; the
+ * generic coverage-loss handoff below protects anchors created by auto-fill.
+ * External injections must not take this eager handoff (#2194).
*/
export function resolveSendWindowHandoff({
isNewUserSend,
@@ -380,6 +381,38 @@ export function resolveSendWindowHandoff({
};
}
+export type WindowCoverageLossAction = 'none' | 'handoff-to-tail' | 'preserve-anchor';
+
+export interface ResolveWindowCoverageLossArgs {
+ /** The stream is currently showing an anchored (non-default-tail) window. */
+ hasWindowAnchor: boolean;
+ /** The anchored window covered the real session tail before this render. */
+ wasCoveringEnd: boolean;
+ /** The anchored window still covers the real session tail after this render. */
+ windowCoversEnd: boolean;
+ /** Auto-follow intent before the tail item was appended. */
+ wasFollowingTail: boolean;
+}
+
+/**
+ * Decide how an anchored render window should react when an appended item moves
+ * the real session tail beyond its bounded end.
+ *
+ * An automatically expanded window can still be following the tail. In that
+ * case the bounded anchor is only an implementation detail, so hand it back to
+ * the default tail window instead of interpreting the append as user scroll
+ * intent. A user who already left the tail keeps the anchored reading window.
+ */
+export function resolveWindowCoverageLossAction({
+ hasWindowAnchor,
+ wasCoveringEnd,
+ windowCoversEnd,
+ wasFollowingTail,
+}: ResolveWindowCoverageLossArgs): WindowCoverageLossAction {
+ if (!hasWindowAnchor || !wasCoveringEnd || windowCoversEnd) return 'none';
+ return wasFollowingTail ? 'handoff-to-tail' : 'preserve-anchor';
+}
+
export interface ResolveNearBottomArgs {
/** scroll 事件前的跟随态(isNearBottomRef) */
wasNearBottom: boolean;