feat(session): show the workspace branch in the session detail header

Resolve the branch from the repo's siblings so a session running in a worktree shows that worktree's branch, falling back to the repo branch and staying empty when no branch is resolvable. Desktop renders a branch chip in the header; mobile appends it to the repo name in the subtitle.
This commit is contained in:
Chris Scott committed 2026-10-16 17:29:31 -04:00
1 parent 4e8051b75a
commit f5d073d7ee
8 files changed
+105 -2

No files matched your search

+39
View File
@@ -13,6 +13,7 @@ import {
hasUnmatchedActiveDirectory,
needsWorktreeLookup,
repoActivityFromIndex,
resolveSessionBranch,
rootSessionID,
type ActiveWorktree,
} from './activeWork'
@@ -611,3 +612,41 @@ describe('buildWorktreeActivity', () => {
expect(buildWorktreeActivity([main], [{ data: [main] }])).toEqual([])
})
})
describe('resolveSessionBranch', () => {
it('returns the matching worktree sibling branch, tolerating a trailing slash', () => {
const main = createRepo({ id: 1, fullPath: '/repos/a', currentBranch: 'main' })
const worktree = createWorktreeSibling(-1, '/repos/a/.worktrees/feature', 'feature', 'opencode')
expect(resolveSessionBranch(main, '/repos/a/.worktrees/feature', [main, worktree])).toBe('feature')
expect(resolveSessionBranch(main, '/repos/a/.worktrees/feature/', [main, worktree])).toBe('feature')
})
it('returns the branch of a registered worktree repo row without a worktree source', () => {
const main = createRepo({ id: 1, fullPath: '/repos/a', currentBranch: 'main' })
const registered = createRepo({ id: 2, fullPath: '/repos/a-feature', isWorktree: true, currentBranch: 'feature' })
expect(resolveSessionBranch(main, '/repos/a-feature', [main, registered])).toBe('feature')
})
it('returns null for a matching sibling with no branch instead of the repo branch', () => {
const main = createRepo({ id: 1, fullPath: '/repos/a', currentBranch: 'main' })
const detached = createRepo({ id: 2, fullPath: '/repos/a-detached', isWorktree: true })
expect(resolveSessionBranch(main, '/repos/a-detached', [main, detached])).toBeNull()
})
it('falls back to the repo branch when no sibling matches', () => {
const current = createRepo({ id: 1, fullPath: '/repos/a', currentBranch: 'main' })
const legacy = createRepo({ id: 1, fullPath: '/repos/a', branch: 'develop' })
expect(resolveSessionBranch(current, '/repos/other', [current])).toBe('main')
expect(resolveSessionBranch(legacy, '/repos/other', [legacy])).toBe('develop')
})
it('returns the repo branch when siblings are undefined', () => {
const main = createRepo({ id: 1, fullPath: '/repos/a', currentBranch: 'main' })
expect(resolveSessionBranch(main, '/repos/a', undefined)).toBe('main')
})
})
+18
View File
@@ -3,6 +3,7 @@ import type { ChildOutcome } from '@/lib/backgroundWork'
import type { SessionStatusType } from '@/stores/sessionStatusStore'
import { getSessionKey } from '@/lib/sessionKey'
import { getSessionPath } from '@/lib/navigation'
import { getRepoBranchLabel } from '@/lib/utils'
import { findRepoForDirectory, normalizeDirectory } from '@/components/navigation/sidebar-session-tree'
import { isWorktreeSibling, type RepoSibling } from '@opencode-manager/shared/utils'
@@ -259,6 +260,23 @@ export function buildWorktreeActivity(
return worktrees
}
/**
* The branch of the workspace a session runs in: the matching sibling's branch when the session
* directory is one of the repo's siblings (a worktree), otherwise the repo's own branch.
*/
export function resolveSessionBranch(
repo: Pick<Repo, 'currentBranch' | 'branch'>,
directory: string | null | undefined,
siblings?: ReadonlyArray<RepoSibling>,
): string | null {
const normalized = normalizeDirectory(directory)
const sibling = normalized
? siblings?.find((entry) => normalizeDirectory(entry.fullPath) === normalized)
: undefined
if (!sibling) return getRepoBranchLabel(repo)
return sibling.currentBranch ?? sibling.branch ?? null
}
/**
* Whether the worktree lookup is worth running: true when some non-archived session that matches no
* repo is either updated inside the window or has its root active (needs-input or running).
+28 -2
View File
@@ -5,9 +5,10 @@ import { getRepo } from "@/api/repos";
import { MessageThread } from "@/components/message/MessageThread";
import { PromptInput, type PromptInputHandle } from "@/components/message/PromptInput";
import { FloatingTTSButton } from '@/components/message/FloatingTTSButton'
import { X, CornerUpLeft } from "lucide-react";
import { X, CornerUpLeft, GitBranch } from "lucide-react";
import { SquareFill } from "@/components/ui/square-fill";
import { Header } from "@/components/ui/header";
import { Badge } from "@/components/ui/badge";
import { SessionPickerDialog } from "@/components/session/SessionPickerDialog";
import { getRepoPath, getSessionListPath, getSessionPath } from '@/lib/navigation'
import { FetchError } from '@/api/fetchWrapper'
@@ -19,6 +20,7 @@ import { useSession, useInterruptSession, useUpdateSession, useCreateSession } f
import { useSessionTranscript } from "@/hooks/useSessionTranscript";
import { useAutoOpenWalkthrough } from "@/hooks/useAutoOpenWalkthrough";
import { useRepoActivity } from "@/hooks/useRepoActivity";
import { useRepoSiblings } from "@/hooks/useRepoSiblings";
import { useSSE } from "@/hooks/useSSE";
import { useUIState } from "@/stores/uiStateStore";
import { useSettings } from "@/hooks/useSettings";
@@ -39,6 +41,7 @@ import { useSpeakVisibleMessages } from "@/hooks/useSpeakVisibleMessages";
import { showToast } from "@/lib/toast";
import { getWorkspaceFilePath } from "@/lib/markdownLinks";
import { getRepoDisplayName } from "@/lib/utils";
import { resolveSessionBranch } from "@/lib/activeWork";
import { RepoMcpDialog } from "@/components/repo/RepoMcpDialog";
import { ProjectActionsMenu } from "@/components/repo/ProjectActionsMenu";
import { RepoActionsDialog } from "@/components/repo/RepoActionsDialog";
@@ -190,6 +193,13 @@ export function SessionDetail() {
: undefined
) ?? repoDirectory;
const { data: repoSiblings } = useRepoSiblings(!isAssistantSession && repo && !repo.isWorktree ? repoId : undefined);
const sessionBranch = useMemo(
() => (repo && !isAssistantSession ? resolveSessionBranch(repo, sessionDirectory, repoSiblings) : null),
[repo, isAssistantSession, sessionDirectory, repoSiblings],
);
const { data: session, isLoading: sessionLoading, error: sessionQueryError } = useSession(
sessionId,
sessionDirectory,
@@ -719,8 +729,24 @@ export function SessionDetail() {
<Header.EditableTitle
value={session?.title || "Untitled Session"}
onChange={handleRenameSession}
subtitle={<span className="text-highlight">{workspaceDisplayName}</span>}
subtitle={
<span className="text-highlight">
{workspaceDisplayName}
{sessionBranch && <span className="font-mono sm:hidden"> · {sessionBranch}</span>}
</span>
}
/>
{sessionBranch && (
<Badge
variant="outline"
data-testid="session-branch"
title={`Branch: ${sessionBranch}`}
className="hidden sm:inline-flex max-w-44 shrink-0 items-center gap-1 border-border bg-card px-2 font-mono text-[11px] font-normal text-muted-foreground"
>
<GitBranch className="h-3 w-3 shrink-0" />
<span className="truncate">{sessionBranch}</span>
</Badge>
)}
</div>
<Header.Actions className="gap-2 sm:gap-4">
<div className="flex items-center gap-1">
@@ -35,6 +35,10 @@ vi.mock('@/hooks/useSessionTranscript', () => ({
useSessionTranscript: mocks.useSessionTranscript,
}))
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: vi.fn(() => ({ data: [] })),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({ model: null, modelString: null, modelRef: null })),
}))
@@ -98,6 +98,10 @@ vi.mock('@/hooks/useSessionTranscript', () => ({
useSessionTranscript: mocks.useSessionTranscript,
}))
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: vi.fn(() => ({ data: [] })),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({ model: null, modelString: null, modelRef: null })),
}))
@@ -58,6 +58,10 @@ vi.mock('@/hooks/useSessionTranscript', () => ({
useSessionTranscript: mocks.useSessionTranscript,
}))
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: vi.fn(() => ({ data: [] })),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({ model: null, modelString: null, modelRef: null })),
}))
@@ -37,6 +37,10 @@ vi.mock('@/hooks/useSessionTranscript', () => ({
useSessionTranscript: mocks.useSessionTranscript,
}))
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: vi.fn(() => ({ data: [] })),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({ model: null, modelString: null, modelRef: null })),
}))
@@ -53,6 +53,10 @@ vi.mock('@/hooks/useSessionTranscript', () => ({
useSessionTranscript: mocks.useSessionTranscript,
}))
vi.mock('@/hooks/useRepoSiblings', () => ({
useRepoSiblings: vi.fn(() => ({ data: [] })),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({ model: null, modelString: null, modelRef: null })),
}))