From a12a97bbfd2d017340f016c0e2e05a94b35f2b21 Mon Sep 17 00:00:00 2001 From: mengge237 Date: Wed, 30 Sep 2026 02:45:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(sidebar):=20=E5=90=8E=E5=8F=B0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=BF=80=E6=B4=BB=E4=B8=8D=E5=86=8D=E9=A1=B6=E6=8E=89?= =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E6=A0=8F=E6=AD=A3=E5=9C=A8=E7=9C=8B=E7=9A=84?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 子代理 / 新后台任务的自动激活原先只判断「栏是否收起」,没有判断「栏里现在是谁」: 宿主 openContent 每次打开都 planSetExpanded(true) 并把焦点落到新开的 tab, 所以当右侧栏是展开的、用户正在看一个文件(例如刚产出的 .md)时, 每来一个子代理 / 新任务就把那一页顶掉,读者正在读的东西被换走。 窄屏那半边(#162 的 park)已经承认「栏在用就别动它」,这里把同一条判据补到宽屏: 仅当目标会话在屏、栏已展开、且当前页不是任务页时,后台激活直接返回; 栏收起时不挡(屏幕上看不到任何东西可被顶掉,两个开关的承诺照旧), 已经在任务页上是原地聚焦(单实例语义,无副作用), 拓扑跳回是用户手势,不走这条闸。 --- src/client/sidebar/use-host-feeds.ts | 57 +++++++++++----- tests/sidebar-auto-activation.spec.tsx | 95 +++++++++++++++++++++++--- 2 files changed, 128 insertions(+), 24 deletions(-) diff --git a/src/client/sidebar/use-host-feeds.ts b/src/client/sidebar/use-host-feeds.ts index 3a9d6c75..89282d1e 100644 --- a/src/client/sidebar/use-host-feeds.ts +++ b/src/client/sidebar/use-host-feeds.ts @@ -30,14 +30,17 @@ const FAILURE_LIMIT = 3 const AUTO_OPEN_DEBOUNCE_MS = 500 /** - * The native column's public face, as this module reaches it. Both actions - * act on the session whose surface is MOUNTED (the controller reads its - * binding), which is why the park below is gated on the target session being - * the on-screen one. + * The native column's public face, as this module reaches it. Every reader + * acts on the session whose surface is MOUNTED (the controller reads its + * binding), which is why both gates below are gated on the target session + * being the on-screen one. */ interface NativeColumnFace { isExpanded?: () => boolean toggleExpanded?: () => void + /** The active tab of the active pane (`ISidebarRight.active`): which page the + * reader is looking at. Absent on a host older than the reader gate. */ + active?: () => { kind?: string } | undefined } /** @@ -58,22 +61,42 @@ interface NativeColumnFace { * when the activation FIRES (the debounced subagent trigger included), so a * resize while arming is honoured. * + * A background activation must not replace the page the reader is on either, + * on ANY viewport: the host's open focuses what it opens, so "activate" means + * "take the column over", and doing that while the reader is in a document + * (a file, a diff, a side chat) throws their page out of view on every new + * subagent / background job — the one thing a DELAYED trigger may never do. + * The gate is the wide-viewport twin of the park below and asks the same + * question — is the column in use? — of the same session: a COLLAPSED column + * shows nothing, so its tabs may be re-pointed freely (that is where the two + * switches still land, and where the narrow park keeps them), while an + * EXPANDED one showing a page other than the Tasks page is in use and is left + * alone. A column already on the Tasks page is re-focused in place as before + * (single-instance semantics make it a no-op). + * * @param ctx - the client context (`ctx.sidebarRight` + `ctx.betterSidebar`). * @param sessionId - the session the feed reports the activity for. - * @param options.background - `true` for background activity (parks on narrow - * viewports); `false` for the explicit topology jump-back, which is a user - * gesture and always leaves the column as the host expanded it. + * @param options.background - `true` for background activity (refuses a + * column the reader is using, parks on narrow viewports); `false` for the + * explicit topology jump-back, which is a user gesture and always leaves the + * column as the host expanded it. */ function activateTasksPage(ctx: Context, sessionId: string, options: { background: boolean }): void { const column = ctx.get('sidebarRight') as unknown as NativeColumnFace | undefined + // The face acts on the MOUNTED session: both gates below are only meaningful + // (and only safe) when the activation targets the one on screen. "On screen" + // is the native surface's own mounted seat — the session list has no + // current-session field — and an absent seat (a global panel, or a host + // without the feed) reads as "not this session": the plugin then leaves the + // column alone rather than reading or toggling one it is not drawing. + const onScreen = mountedSessionId(ctx) === sessionId + // Do not take over the page the reader is looking at (see the docblock). + if (options.background && onScreen && column?.isExpanded?.() === true) { + const shown = column.active?.() + if (shown !== undefined && shown.kind !== 'subagent') return + } const park = options.background - // The face acts on the MOUNTED session: parking is only meaningful (and - // only safe) when the activation targets the one on screen. "On screen" - // is the native surface's own mounted seat — the session list has no - // current-session field — and an absent seat (a global panel, or a host - // without the feed) reads as "not this session": the plugin then leaves - // the column alone rather than toggling one it is not drawing. - && mountedSessionId(ctx) === sessionId + && onScreen && isNarrowWidth(window.innerWidth) // Only a column the user had COLLAPSED is put back: an expanded one is in // use, and closing it under the user would be worse than the takeover. @@ -177,7 +200,8 @@ export function useHostFeeds(feeds: { * focus an existing tab in place; a new tab lands in that column and is * never duplicated. Landing it EXPANDS the column on wide viewports, while * a narrow viewport (where the host draws that column fullscreen) parks the - * tab instead of taking the screen over — see {@link activateTasksPage}. + * tab instead of taking the screen over — see {@link activateTasksPage}, + * which also refuses a column the reader has expanded onto another page. * Switching to a session that already has subagents never triggers — its * baseline starts at the current count — so a deliberate layout is never * fought. @@ -227,7 +251,8 @@ export function useHostFeeds(feeds: { * auto-open pref is on, and the Tasks tab type is enabled, activate the Tasks * page that contains the background-jobs section — in DSH's native right * Sidebar, expanded on wide viewports and parked on narrow ones exactly like - * the subagent trigger ({@link activateTasksPage}). Unlike that trigger + * the subagent trigger ({@link activateTasksPage}, which leaves a column the + * reader has expanded onto another page untouched). Unlike that trigger * (0 → N only), ANY new job id triggers: the agent may start several jobs in * one session, and each should surface. * diff --git a/tests/sidebar-auto-activation.spec.tsx b/tests/sidebar-auto-activation.spec.tsx index 658f535f..24a9f0ca 100644 --- a/tests/sidebar-auto-activation.spec.tsx +++ b/tests/sidebar-auto-activation.spec.tsx @@ -123,20 +123,37 @@ function makeMountedStore(initial: string | undefined) { type MountedStore = ReturnType /** `ctx.sidebarRight`, replaced by a stand-in column: `isExpanded` reports the - * current state, `toggleExpanded` flips it and counts the calls, and `mounted` - * is the seat observation the shell binds its per-session state to. */ + * current state, `toggleExpanded` flips it and counts the calls, `active` + * reports the page the reader is looking at, and `mounted` is the seat + * observation the shell binds its per-session state to. */ interface NativeColumnSpy { - face: { isExpanded: () => boolean; toggleExpanded: () => void; mounted: MountedStore } + face: { + isExpanded: () => boolean + toggleExpanded: () => void + active: () => { kind: string } | undefined + mounted: MountedStore + } toggles: number mounted: MountedStore } -function makeNativeColumnSpy(expanded: boolean, mounted: string | undefined): NativeColumnSpy { +/** + * `activeKind` is the kind of the tab on screen: 'editor' stands for anything + * the reader opened themselves (a file, a diff, a side chat). `mountSidebar` + * defaults it to the Tasks page itself, so the lanes that pin the + * activate-and-park promise keep reading a column whose open page the + * activation is allowed to re-focus. + */ +function makeNativeColumnSpy( + expanded: boolean, + mounted: string | undefined, + activeKind: string | undefined, +): NativeColumnSpy { const spy = { toggles: 0, expanded, mounted: makeMountedStore(mounted), - face: {} as { isExpanded: () => boolean; toggleExpanded: () => void; mounted: MountedStore }, + face: {} as NativeColumnSpy['face'], } spy.face = { isExpanded: () => spy.expanded, @@ -144,6 +161,7 @@ function makeNativeColumnSpy(expanded: boolean, mounted: string | undefined): Na spy.toggles += 1 spy.expanded = !spy.expanded }, + active: () => (activeKind === undefined ? undefined : { kind: activeKind }), mounted: spy.mounted, } return spy @@ -228,7 +246,7 @@ function setViewport(width: number): void { function mountSidebar( width: number, bottomOpen = false, - options: { columnExpanded?: boolean; prefs?: Record } = {}, + options: { columnExpanded?: boolean; activeKind?: string; prefs?: Record } = {}, ): MountedSidebar { setViewport(width) vi.stubGlobal('WebSocket', FakeWebSocket) @@ -255,7 +273,11 @@ function mountSidebar( const surface = makeNativeSurfaceSpy() service.setSurface(surface.surface) service.registerTab({ id: 'subagent', title: 'Subagent', component: JumpHarness }) - const column = makeNativeColumnSpy(options.columnExpanded ?? false, sessionId) + const column = makeNativeColumnSpy( + options.columnExpanded ?? false, + sessionId, + options.activeKind ?? 'subagent', + ) const localeSnapshot = { active: 'en' } const ctx = { locale: { subscribe: () => () => {}, getSnapshot: () => localeSnapshot }, @@ -431,12 +453,69 @@ describe('Sidebar background-activity auto-activation (#162)', () => { }) it('a narrow column the user already expanded is not closed under them', async () => { - const sidebar = mountSidebar(390, false, { columnExpanded: true, prefs: { mobileNoAutoOpen: false } }) + const sidebar = mountSidebar(390, false, { + columnExpanded: true, + activeKind: 'subagent', + prefs: { mobileNoAutoOpen: false }, + }) await publishActivity(sidebar, 'subagent') expectNativeTasksOpen(sidebar, sidebar.sessionId) expect(sidebar.column.toggles).toBe(0) }) + it.each(['subagent', 'job'] as const)( + 'a WIDE column open on another page keeps it: %s activation does not take the column over', + async (source) => { + // The takeover gate. The reader is in a document (here: whatever kind the + // plugin opened for a `.md`) and the agent starts background work — the + // host's open would focus the Tasks page and throw that document out of + // view, which is exactly what must not happen. + const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'editor' }) + await publishActivity(sidebar, source) + expect(sidebar.surface.opens).toEqual([]) + // No open, and no park either: the column stays as the reader left it. + expect(sidebar.column.toggles).toBe(0) + }, + ) + + it('a WIDE column already showing the Tasks page is still re-focused in place', async () => { + const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'subagent' }) + await publishActivity(sidebar, 'subagent') + expectNativeTasksOpen(sidebar, sidebar.sessionId) + expect(sidebar.column.toggles).toBe(0) + }) + + it('a COLLAPSED column is not "in use": the page behind it does not block the activation', async () => { + // Nothing of the reader's is on screen while the column is collapsed, so + // the switches keep their promise there (the host expands it on open). + const sidebar = mountSidebar(1024, false, { activeKind: 'editor' }) + await publishActivity(sidebar, 'subagent') + expectNativeTasksOpen(sidebar, sidebar.sessionId) + }) + + it('a host without the active-tab face keeps the plain activation', async () => { + const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'editor' }) + // Nothing to compare against is not "the reader is elsewhere". + delete (sidebar.column.face as { active?: unknown }).active + await publishActivity(sidebar, 'subagent') + expectNativeTasksOpen(sidebar, sidebar.sessionId) + }) + + it('the topology jump-back still takes over a column the reader is using', () => { + // The node click IS the user asking for the Tasks page, so the takeover + // gate applies to background activity only. + const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'editor' }) + act(() => { + sidebar.service.openTab( + { type: 'subagent', title: 'Tasks', target: 'bottom' }, + { sessionId: sidebar.sessionId }, + ) + }) + switchToChild(sidebar) + expectNativeTasksOpen(sidebar, 'child') + expect(sidebar.column.toggles).toBe(0) + }) + it('a page that loads with jobs already running never triggers; the next NEW job does', async () => { const sidebar = mountSidebar(1024) // The conversation is already running work when the sidebar mounts: the