refactor(vscode): extract worktree session selectors

This commit is contained in:
marius-kilocode
2026-08-27 15:03:58 +02:00
parent 13a9673d08
commit 6bf338775a
3 changed files with 45 additions and 21 deletions
@@ -1,5 +1,9 @@
import { describe, expect, it } from "bun:test"
import { rootSessions } from "../../webview-ui/agent-manager/project/session-filter"
import {
rootSessions,
worktreeSessionIds,
worktreeSessions,
} from "../../webview-ui/agent-manager/project/session-filter"
import type { ProjectSessionInfo } from "../../webview-ui/src/types/messages"
const session = (id: string, worktreeId: string | null, parentID: string | null): ProjectSessionInfo => ({
@@ -23,4 +27,19 @@ describe("rootSessions", () => {
expect(rootSessions(sessions, null).map((item) => item.id)).toEqual(["root"])
})
it("preserves managed membership, chronological fallback, and custom tab order", () => {
const rows = [
{ ...session("new", "wt-1", null), createdAt: "2026-01-02T00:00:00.000Z" },
session("old", "wt-1", null),
session("child", "wt-1", "old"),
session("other", "wt-2", null),
]
const managed = rows.map((item) => ({ id: item.id, worktreeId: item.worktreeId, createdAt: item.createdAt }))
expect([...worktreeSessionIds("wt-1", managed)]).toEqual(["new", "old", "child"])
expect(worktreeSessions("wt-1", managed, rows, undefined).map((item) => item.id)).toEqual(["old", "new"])
expect(worktreeSessions("wt-1", managed, rows, ["missing", "new"]).map((item) => item.id)).toEqual(["new", "old"])
expect(rows.map((item) => item.id)).toEqual(["new", "old", "child", "other"])
expect(worktreeSessions("missing", managed, rows, undefined)).toEqual([])
})
})
@@ -85,6 +85,7 @@ import { SidebarBody } from "./SidebarBody"
import { TabBar } from "./TabBar"
import { createProjectLive } from "./project/live"
import { createProjectSessionsLive } from "./project/sessions-live"
import { worktreeSessionIds as worktreeMembership, worktreeSessions } from "./project/session-filter"
import { applyProjectSelection, createTargetRememberer } from "./project/selection"
import { createLocalSessions, persistLocalTabs, projectLocalIds, projectLocalSessions } from "./project/local-tabs"
import { createProjectRegistry, type PersistedProjectTabs } from "./project/registry"
@@ -778,20 +779,8 @@ const AgentManagerContent: Component = () => {
title: () => t("agentManager.session.newSession"),
})
const sessionsForWorktree = (worktreeId: string): SessionInfo[] => {
const ids = new Set(
managedSessions()
.filter((ms) => ms.worktreeId === worktreeId)
.map((ms) => ms.id),
)
return applyTabOrder(
session
.sessions()
.filter((s) => isKnownRootSession(s) && ids.has(s.id))
.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()),
worktreeTabOrder()[worktreeId],
)
}
const sessionsForWorktree = (id: string) =>
worktreeSessions(id, managedSessions(), session.sessions(), worktreeTabOrder()[id])
const activeWorktreeSessions = createMemo((): SessionInfo[] => {
const sel = selection()
@@ -802,11 +791,7 @@ const AgentManagerContent: Component = () => {
const activeWorktreeSessionIds = createMemo<ReadonlySet<string> | undefined>(() => {
const sel = selection()
if (!sel || sel === LOCAL) return undefined
return new Set(
managedSessions()
.filter((item) => item.worktreeId === sel)
.map((item) => item.id),
)
return worktreeMembership(sel, managedSessions())
})
const activeTabs = createMemo((): SessionInfo[] => {
@@ -1,6 +1,26 @@
import type { ProjectSessionInfo } from "../../src/types/messages"
import type { ManagedSessionState, ProjectSessionInfo, SessionInfo } from "../../src/types/messages"
import { isKnownRootSession } from "../navigate"
import { applyTabOrder } from "../tab-order"
export function rootSessions(sessions: ProjectSessionInfo[], worktreeId: string | null): ProjectSessionInfo[] {
return sessions.filter((session) => session.worktreeId === worktreeId && isKnownRootSession(session))
}
export function worktreeSessionIds(id: string, sessions: ManagedSessionState[]) {
return new Set(sessions.filter((session) => session.worktreeId === id).map((session) => session.id))
}
export function worktreeSessions(
id: string,
managed: ManagedSessionState[],
sessions: SessionInfo[],
order: string[] | undefined,
) {
const ids = worktreeSessionIds(id, managed)
return applyTabOrder(
sessions
.filter((session) => isKnownRootSession(session) && ids.has(session.id))
.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()),
order,
)
}