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): 刷新分离侧栏会话上下文

DavidShen72909243805f
diff --git a/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx b/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
index d20df8987e..a83abfb52e 100644
--- a/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
+++ b/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
@@ -74,6 +74,8 @@ export function SidebarWindowLayout() {
const [ctx, setCtx] = useState<SidebarWindowContext | null>(null);
// 预热窗口初始隐藏;由 main 的 visibility push 驱动 Shell 内各子面板暂停/恢复。
const [windowVisible, setWindowVisible] = useState(false);
+ const visibilityRevisionRef = useRef(0);
+ const contextRevisionRef = useRef(0);
const presentationReadySentRef = useRef(false);
// 复用隐藏窗口时,Chromium 可能保留上一次关闭按钮的 focus / :hover 状态。
// 与资源用量窗口一致,隐藏时重挂载 chrome,确保再次显示从干净状态开始。
@@ -89,6 +91,7 @@ export function SidebarWindowLayout() {
})
.catch((err) => log.warn('getContext failed', err));
const offCtx = window.electronAPI.rightSidebarWindow.onContextChanged((next) => {
+ contextRevisionRef.current += 1;
setCtx(next);
});
// renderer-ready:Shell 已挂载,controller 可知 React 组件树就绪。
@@ -120,21 +123,46 @@ export function SidebarWindowLayout() {
}, []);
// 隐藏/显示时刷新 context(主窗 session 可能已切换)并重置瞬时交互态。
+ // 重新显示不能先恢复交互再异步收 context:否则媒体预览等宿主能力会在短暂窗口内
+ // 沿用隐藏前的 session。这里先保持 Shell 不可交互并撤销旧 context,拿到 main 的
+ // 当前快照后才恢复;revision 防止快速 hide/show 时迟到的请求重新激活窗口。
useEffect(() => {
return window.electronAPI.rightSidebarWindow.onVisibilityChanged((payload) => {
- setWindowVisible(payload.visible);
+ const revision = ++visibilityRevisionRef.current;
if (!payload.visible) {
+ setWindowVisible(false);
+ setCtx(null);
if (document.activeElement instanceof HTMLElement) document.activeElement.blur();
setWindowChromeRevision((revision) => revision + 1);
} else {
- // 重新显示时从 main 缓存拉最新 context
- void window.electronAPI.rightSidebarWindow.refreshContext().catch(() => undefined);
+ setWindowVisible(false);
+ setCtx(null);
+ const contextRevision = contextRevisionRef.current;
+ // controller 在发送 visible:true 前已经切换为可见态,getContext 此时返回
+ // main 缓存的最新快照;与单向 refresh push 相比,可明确等待本轮刷新完成。
+ void window.electronAPI.rightSidebarWindow
+ .getContext()
+ .then((next) => {
+ if (visibilityRevisionRef.current !== revision) return;
+ // 请求期间若已有更新的 context-changed 推送,以推送为准,避免迟到的
+ // invoke 快照把新会话覆盖回旧会话。
+ if (contextRevisionRef.current === contextRevision) setCtx(next);
+ setWindowVisible(true);
+ })
+ .catch((err) => {
+ if (visibilityRevisionRef.current !== revision) return;
+ log.warn('refresh context on show failed', err);
+ // 保持 context 为空,允许窗口展示安全占位态;后续 context-changed
+ // 推送仍可正常恢复真实任务。
+ setWindowVisible(true);
+ });
if (document.activeElement instanceof HTMLElement) document.activeElement.blur();
}
});
}, []);
- const sessionId = ctx?.available ? ctx.sessionId : null;
+ const activeCtx = windowVisible ? ctx : null;
+ const sessionId = activeCtx?.available ? activeCtx.sessionId : null;
// agent tab-op 触发的可见性请求(本窗口内 rsbBrowserBridge 派发):
// - 'close'(最后一个 tab 被关)且目标是当前会话 → 收起 = 关本窗口
@@ -243,9 +271,9 @@ export function SidebarWindowLayout() {
<div className="relative flex flex-1 flex-col overflow-hidden">
<RightSidebarShell
sessionId={sessionId}
- workdir={ctx?.workdir ?? ''}
- remoteHostId={ctx?.remoteHostId ?? null}
- deviceLinkDeviceId={ctx?.deviceLinkDeviceId}
+ workdir={activeCtx?.workdir ?? ''}
+ remoteHostId={activeCtx?.remoteHostId ?? null}
+ deviceLinkDeviceId={activeCtx?.deviceLinkDeviceId}
shellVisible={windowVisible}
isMac={isMac}
/>
diff --git a/apps/desktop/src/renderer/components/layout/__tests__/ReusableWindowChrome.test.tsx b/apps/desktop/src/renderer/components/layout/__tests__/ReusableWindowChrome.test.tsx
index 0c20c582e5..6f9a3249ef 100644
--- a/apps/desktop/src/renderer/components/layout/__tests__/ReusableWindowChrome.test.tsx
+++ b/apps/desktop/src/renderer/components/layout/__tests__/ReusableWindowChrome.test.tsx
@@ -272,7 +272,7 @@ describe('reusable auxiliary window chrome', () => {
);
});
- it('passes cached-window visibility through to right-sidebar tab bodies', async () => {
+ it('restores right-sidebar tab bodies only after the visible window has fresh context', async () => {
render(<SidebarWindowLayout />);
expect(mocks.sidebarShellVisible).toBe(false);
@@ -295,7 +295,58 @@ describe('reusable auxiliary window chrome', () => {
render(<SidebarWindowLayout />);
+ await act(async () => mocks.sidebarVisibilityListener?.({ visible: true }));
+ await waitFor(() => expect(mocks.sidebarLightboxSessionId).toBe('session-a'));
+ });
+
+ it('does not expose a stale media session while refreshing context after reopen', async () => {
+ let resolveFreshContext!: (ctx: RsbWindowContext | null) => void;
+ window.electronAPI.rightSidebarWindow.getContext = vi
+ .fn()
+ .mockResolvedValueOnce({
+ sessionId: 'session-a',
+ workdir: '/workdir-a',
+ remoteHostId: null,
+ available: true,
+ })
+ .mockResolvedValueOnce({
+ sessionId: 'session-a',
+ workdir: '/workdir-a',
+ remoteHostId: null,
+ available: true,
+ })
+ .mockImplementationOnce(
+ () =>
+ new Promise<RsbWindowContext | null>((resolve) => {
+ resolveFreshContext = resolve;
+ }),
+ );
+
+ render(<SidebarWindowLayout />);
+ await waitFor(() =>
+ expect(window.electronAPI.rightSidebarWindow.getContext).toHaveBeenCalledTimes(1),
+ );
+ await act(async () => mocks.sidebarVisibilityListener?.({ visible: true }));
await waitFor(() => expect(mocks.sidebarLightboxSessionId).toBe('session-a'));
+
+ await act(async () => mocks.sidebarVisibilityListener?.({ visible: false }));
+ expect(mocks.sidebarLightboxSessionId).toBeUndefined();
+
+ act(() => mocks.sidebarVisibilityListener?.({ visible: true }));
+ expect(mocks.sidebarShellVisible).toBe(false);
+ expect(mocks.sidebarLightboxSessionId).toBeUndefined();
+
+ await act(async () => {
+ resolveFreshContext({
+ sessionId: 'session-b',
+ workdir: '/workdir-b',
+ remoteHostId: null,
+ available: true,
+ });
+ });
+
+ expect(mocks.sidebarShellVisible).toBe(true);
+ expect(mocks.sidebarLightboxSessionId).toBe('session-b');
});
it('respects a plugin manifest that disables minimize in the detached window', async () => {