fix(activity): surface worktree sessions in active work, the sidebar and repo badges

- Resolve sessions in repo worktrees to the owning repo in the activity list; show the worktree branch and open the Workspaces tab
- Look up worktrees only for unmatched sessions in the window or under an active root, and hold the loading state until it settles
- Show schedule-worktree sessions only while running or needing input
- Light repo badges for worktree sessions through one app-wide worktree lookup
- List worktree sessions under the expanded repo in the sidebar
- Subscribe the event stream to active session directories so worktree permissions and forms arrive
- Keep the worktree tab when navigating from the desktop Activity panel
- Remove the braille spinner's always-dim bottom row
This commit is contained in:
Chris Scott committed 2026-10-16 20:36:56 +00:00
1 parent 1d31286904
commit bfbbbf08ef
23 files changed
+932 -81

No files matched your search

+64 -2
View File
@@ -14,6 +14,7 @@ const WINDOW_MS = 60 * MS_PER_MINUTE
const mocks = vi.hoisted(() => ({
listSessionPage: vi.fn(),
getSession: vi.fn(),
getRepoSiblings: vi.fn(),
useNavigableRepos: vi.fn(),
pendingSessionIDs: [] as string[],
}))
@@ -27,6 +28,14 @@ vi.mock('@/api/opencode', async (importOriginal) => {
}
})
vi.mock('@/api/repos', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/api/repos')>()
return {
...actual,
getRepoSiblings: mocks.getRepoSiblings,
}
})
vi.mock('@/hooks/useSidebarRepoGroups', () => ({
useNavigableRepos: mocks.useNavigableRepos,
}))
@@ -48,12 +57,17 @@ vi.mock('@/lib/toast', () => ({
},
}))
const session = (id: string, updated: number, parentID?: string | null): SessionInfo => ({
const session = (
id: string,
updated: number,
parentID?: string | null,
directory = REPO_PATH,
): SessionInfo => ({
id,
projectID: 'proj_1',
title: `Session ${id}`,
time: { created: updated, updated },
location: { directory: REPO_PATH },
location: { directory },
cost: 0,
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
...(parentID === undefined ? {} : { parentID }),
@@ -83,6 +97,8 @@ describe('useActiveWork', () => {
beforeEach(() => {
mocks.listSessionPage.mockReset()
mocks.getSession.mockReset()
mocks.getRepoSiblings.mockReset()
mocks.getRepoSiblings.mockResolvedValue([])
mocks.useNavigableRepos.mockReset()
mocks.useNavigableRepos.mockReturnValue({ repos: [repo], isLoading: false, refetch: vi.fn() })
useSessionStatus.setState({
@@ -197,4 +213,50 @@ describe('useActiveWork', () => {
expect(recentIDs(result.current.groups)).not.toContain('root')
})
it('resolves a session in a worktree to its owning repo and opens the workspaces tab', async () => {
const now = Date.now()
mocks.listSessionPage.mockResolvedValue({
items: [session('wt', now - 500, undefined, '/repo/.worktrees/feature')],
})
mocks.getRepoSiblings.mockResolvedValue([
{
...repo,
id: -1,
fullPath: '/repo/.worktrees/feature',
localPath: 'feature',
sourcePath: '/repo/.worktrees/feature',
currentBranch: 'feature',
worktreeSource: 'opencode',
},
])
const { result } = renderHook(() => useActiveWork(WINDOW_MS), {
wrapper: createWrapper(createQueryClient()),
})
await waitFor(() => expect(recentIDs(result.current.groups)).toContain('wt'))
expect(result.current.groups.recent[0].repo.id).toBe(1)
expect(result.current.groups.recent[0].path).toBe('/repos/1/sessions/wt?repoTab=workspaces')
})
it('never fetches siblings for an orphan session updated outside the window', async () => {
const now = Date.now()
mocks.listSessionPage.mockResolvedValue({
items: [session('orphan', now - 2 * WINDOW_MS, undefined, '/outside/repo')],
})
const { result } = renderHook(() => useActiveWork(WINDOW_MS), {
wrapper: createWrapper(createQueryClient()),
})
await waitFor(() => expect(result.current.isLoading).toBe(false))
await act(async () => {
await Promise.resolve()
})
expect(mocks.getRepoSiblings).not.toHaveBeenCalled()
})
})
+24 -18
View File
@@ -3,23 +3,22 @@ import { useQueries, useQueryClient, type QueryClient } from '@tanstack/react-qu
import type { SessionInfo } from '@opencode-manager/shared/opencode'
import { sessionQueryOptions, useSessionsAcrossDirectories } from '@/hooks/useOpenCode'
import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups'
import { useForms, usePermissions } from '@/contexts/EventContext'
import { usePendingInputSessionIDs, useRepoWorktreeList } from '@/contexts/RepoWorktreesContext'
import { useSessionStatus } from '@/stores/sessionStatusStore'
import { useSettings } from '@/hooks/useSettings'
import { DEFAULT_ACTIVITY_WINDOW_MINUTES } from '@/api/types/settings'
import { useRepoWorktrees } from '@/hooks/useRepoWorktrees'
import {
MS_PER_MINUTE,
buildActiveWork,
buildRepoActivityIndex,
collectActiveRoots,
needsWorktreeLookup,
repoActivityFromIndex,
rootSessionID,
type ActiveWorkGroups,
} from '@/lib/activeWork'
import {
combineQuerySessions,
mergeExtraSessions,
} from '@/components/navigation/sidebar-session-tree'
import { combineQuerySessions, mergeExtraSessions } from '@/components/navigation/sidebar-session-tree'
import type { Repo } from '@/api/types'
const ACTIVE_WORK_SESSION_LIMIT = 50
@@ -33,17 +32,6 @@ export function useActivityWindowMinutes(): number {
return preferences?.activityWindowMinutes ?? DEFAULT_ACTIVITY_WINDOW_MINUTES
}
/** Pending permission and form session ids, merged once per snapshot into a referentially stable list. */
function usePendingInputSessionIDs(): string[] {
const pendingPermissionSessionIDs = usePermissions().pendingSessionIDs
const pendingFormSessionIDs = useForms().pendingSessionIDs
return useMemo(
() => [...pendingPermissionSessionIDs, ...pendingFormSessionIDs],
[pendingPermissionSessionIDs, pendingFormSessionIDs],
)
}
/**
* Distinct active roots for a badge: needs-input roots and running roots that are not also
* pending input. Reads only the session-status store and the pending queues, with no network.
@@ -72,6 +60,7 @@ export function useActiveWorkCounts(): { needsInput: number; running: number } {
*/
export function useRepoActivityState(repo: Repo): 'needs-input' | 'running' | null {
const pendingInputSessionIDs = usePendingInputSessionIDs()
const worktrees = useRepoWorktreeList()
return useSessionStatus((state) =>
repoActivityFromIndex(
@@ -83,6 +72,7 @@ export function useRepoActivityState(repo: Repo): 'needs-input' | 'running' | nu
directories: state.directories,
pendingInputSessionIDs,
}),
worktrees,
),
)
}
@@ -256,6 +246,21 @@ export function useActiveWork(windowMs: number): {
[sessions, fetchedSessions],
)
const needsLookup = useMemo(
() =>
needsWorktreeLookup({
now,
windowMs,
repos,
sessions: mergedSessions,
parents: resolvedParents,
roots: resolvedRoots,
}),
[now, windowMs, repos, mergedSessions, resolvedParents, resolvedRoots],
)
const { worktrees, isLoading: isWorktreesLoading } = useRepoWorktrees(repos, needsLookup)
const groups = useMemo(
() =>
buildActiveWork({
@@ -266,13 +271,14 @@ export function useActiveWork(windowMs: number): {
parents: resolvedParents,
roots: resolvedRoots,
outcomes,
worktrees,
}),
[now, windowMs, repos, mergedSessions, resolvedParents, resolvedRoots, outcomes],
[now, windowMs, repos, mergedSessions, resolvedParents, resolvedRoots, outcomes, worktrees],
)
return {
groups,
isLoading: isReposLoading || isSessionsLoading,
isLoading: isReposLoading || isSessionsLoading || isWorktreesLoading,
isError: isSessionsError,
}
}
+28
View File
@@ -0,0 +1,28 @@
import { useQueries } from '@tanstack/react-query'
import { getRepoSiblings } from '@/api/repos'
import { repoSiblingsQueryKey } from '@/hooks/useRepoSiblings'
import { buildWorktreeActivity, type ActiveWorktree } from '@/lib/activeWork'
import type { Repo } from '@/api/types'
/**
* Discovers each repo's worktrees from its siblings while enabled, keyed by directory so a session
* running in a worktree resolves to the repo that owns it. Each query is skipped for repos without
* a positive id. `isLoading` is true while any enabled siblings query is still loading.
*/
export function useRepoWorktrees(
repos: Repo[],
enabled: boolean,
): { worktrees: ActiveWorktree[]; isLoading: boolean } {
return useQueries({
queries: repos.map((repo) => ({
queryKey: repoSiblingsQueryKey(repo.id),
queryFn: () => getRepoSiblings(repo.id),
enabled: enabled && repo.id > 0,
staleTime: 30_000,
})),
combine: (results) => ({
worktrees: buildWorktreeActivity(repos, results),
isLoading: results.some((result) => result.isLoading),
}),
})
}
+19 -4
View File
@@ -5,6 +5,7 @@ import { sessionQueryOptions, useSessionsAcrossDirectories } from '@/hooks/useOp
import { useSessionPins } from '@/hooks/useSessionPins'
import { buildPinnedSessionKeys, buildSessionKey, getSessionKey } from '@/lib/sessionKey'
import type { Repo } from '@/api/types'
import type { ActiveWorktree } from '@/lib/activeWork'
import {
SIDEBAR_SESSIONS_PER_REPO,
buildSidebarRepoGroups,
@@ -17,10 +18,17 @@ import {
export function useSidebarRepoGroups(input: {
repos: Repo[]
search?: string
worktrees?: ReadonlyArray<ActiveWorktree>
}): { groups: SidebarRepoGroup[]; isLoading: boolean; isError: boolean; hasMore: boolean } {
const { repos, search } = input
const { repos, search, worktrees } = input
const directories = useMemo(() => repos.map((repo) => repo.fullPath), [repos])
const directories = useMemo(
() => [
...repos.map((repo) => repo.fullPath),
...(worktrees ?? []).map((worktree) => worktree.directory),
],
[repos, worktrees],
)
const { data: sessions, isLoading, isError, hasNextPage } = useSessionsAcrossDirectories(directories, {
search,
@@ -52,8 +60,15 @@ export function useSidebarRepoGroups(input: {
)
const groups = useMemo(
() => buildSidebarRepoGroups({ repos, sessions: mergedSessions, pinnedKeys, now: Date.now() }),
[repos, mergedSessions, pinnedKeys],
() =>
buildSidebarRepoGroups({
repos,
sessions: mergedSessions,
pinnedKeys,
now: Date.now(),
worktrees,
}),
[repos, mergedSessions, pinnedKeys, worktrees],
)
return { groups, isLoading, isError, hasMore: Boolean(hasNextPage) }