feat(servers): harden pairing and run linked-server sessions in the UI

Replace the plaintext pairing exchange with a nonce and HMAC proof plus
AES-256-GCM encrypted credentials, bumping links to v2. Track session
ownership per linked server so activity, session picker, transcript, MCP
and skills views work against remote servers. Share encrypted secret
access, event routing and URL helpers across backend, shared and CLI.
This commit is contained in:
Chris Scott committed 2026-10-18 20:27:34 -04:00
1 parent c591c15743
commit f566556fd6
77 files changed
+3170 -702

No files matched your search

@@ -48,6 +48,7 @@ const NEAR_BOTTOM_PX = 240
type SessionPickerTab = 'active' | 'sessions' | 'projects'
const SESSION_PICKER_TABS: SessionPickerTab[] = ['active', 'sessions', 'projects']
const REMOTE_SESSION_PICKER_TABS: SessionPickerTab[] = ['active', 'sessions']
const SESSION_PICKER_TAB_LABELS: Record<SessionPickerTab, string> = {
active: 'Active',
@@ -55,9 +56,15 @@ const SESSION_PICKER_TAB_LABELS: Record<SessionPickerTab, string> = {
projects: 'Projects',
}
function nextSessionPickerTab(tab: SessionPickerTab): SessionPickerTab {
const index = SESSION_PICKER_TABS.indexOf(tab)
return SESSION_PICKER_TABS[(index + 1) % SESSION_PICKER_TABS.length]
function nextSessionPickerTab(tab: SessionPickerTab, tabs: SessionPickerTab[]): SessionPickerTab {
const index = tabs.indexOf(tab)
return tabs[(index + 1) % tabs.length]
}
interface RemotePickerScope {
serverId: string
serverName?: string
projectDirectory?: string
}
interface SessionPickerDialogProps {
@@ -66,14 +73,19 @@ interface SessionPickerDialogProps {
currentRepo: Repo | undefined
activeSessionID?: string
onSelectSession: (session: Session, repo: Repo) => void
onSelectRemoteSession?: (session: Session, serverId: string) => void
onDeleteActiveSession: () => void
onSelectRepo: (repo: Repo) => void
/** Set on a linked-server route to list that server's sessions instead of Manager repos. */
remote?: RemotePickerScope
}
interface SessionPickerRow {
key: string
session: Session
repo: Repo
repo?: Repo
serverId?: string
serverName?: string
updatedAt?: number
}
@@ -87,14 +99,11 @@ function buildSections(
sessions: Session[],
pinnedKeys: ReadonlySet<string>,
keyFn: (session: Session) => string,
resolveRepo: (session: Session) => Repo | undefined,
resolveRepo?: (session: Session) => Repo | undefined,
): SessionPickerSection[] {
const { pinned, today, older } = partitionSessions(sessions, pinnedKeys, keyFn)
const toRows = (group: Session[]): SessionPickerRow[] =>
group.flatMap((session) => {
const repo = resolveRepo(session)
return repo ? [{ key: keyFn(session), session, repo }] : []
})
group.map((session) => ({ key: keyFn(session), session, repo: resolveRepo?.(session) }))
const sections: SessionPickerSection[] = []
const pinnedRows = toRows(pinned)
@@ -189,6 +198,7 @@ interface SessionPickerRowItemProps {
isPinned: boolean
isPendingDelete: boolean
showRepo: boolean
showActions: boolean
label?: string
onOpen: (row: SessionPickerRow) => void
onHover: (index: number) => void
@@ -206,6 +216,7 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({
isPinned,
isPendingDelete,
showRepo,
showActions,
label,
onOpen,
onHover,
@@ -263,13 +274,18 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({
: 'text-muted-foreground',
)}
>
{showRepo && <span className="max-w-32 truncate">{getRepoDisplayName(row.repo)}</span>}
{showRepo && row.repo ? (
<span className="max-w-32 truncate">{getRepoDisplayName(row.repo)}</span>
) : null}
{showRepo && !row.repo && (row.serverName || row.serverId) ? (
<span className="max-w-32 truncate">{row.serverName ?? row.serverId}</span>
) : null}
{!showRepo && label && <span className="max-w-32 truncate">{label}</span>}
<span>{formatShortRelativeTime(new Date(row.updatedAt ?? row.session.time.updated))}</span>
</span>
</div>
<div className="flex shrink-0 items-center pr-1">
<DropdownMenu>
{row.serverId || !showActions ? null : <DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
type="button"
@@ -291,7 +307,7 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({
</DropdownMenuItem>
<DropdownMenuItem onClick={() => onRequestDelete(row.session)}>Delete</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</DropdownMenu>}
</div>
</div>
)
@@ -301,6 +317,7 @@ interface SessionPickerRowActionsOptions {
directories: string[]
activeSessionID?: string
onSelectSession: (session: Session, repo: Repo) => void
onSelectRemoteSession?: (session: Session, serverId: string) => void
onDeleteActiveSession: () => void
}
@@ -325,6 +342,7 @@ function useSessionPickerRowActions({
directories,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
}: SessionPickerRowActionsOptions): SessionPickerRowActions {
const { data: sessionPins } = useSessionPins()
@@ -341,9 +359,13 @@ function useSessionPickerRowActions({
const openRow = useCallback(
(row: SessionPickerRow | undefined) => {
if (!row) return
onSelectSession(row.session, row.repo)
if (row.serverId) {
onSelectRemoteSession?.(row.session, row.serverId)
return
}
if (row.repo) onSelectSession(row.session, row.repo)
},
[onSelectSession],
[onSelectSession, onSelectRemoteSession],
)
const performDelete = useCallback(
@@ -358,7 +380,7 @@ function useSessionPickerRowActions({
const deleteRow = useCallback(
(row: SessionPickerRow | undefined) => {
if (!row) return
if (!row || row.serverId) return
if (pendingDeleteKey !== row.key) {
setPendingDeleteKey(row.key)
return
@@ -372,6 +394,7 @@ function useSessionPickerRowActions({
const handleTogglePin = useCallback(
(row: SessionPickerRow) => {
if (row.serverId) return
togglePin.mutate({
sessionId: row.session.id,
directory: row.session.location.directory,
@@ -431,6 +454,7 @@ interface SessionPickerListProps {
pinnedKeys: ReadonlySet<string>
pendingDeleteKey: string | null
showRepo: boolean
showRowActions?: boolean
labelFor?: (row: SessionPickerRow) => string | undefined
emptyState: React.ReactNode
onOpenRow: (row: SessionPickerRow) => void
@@ -455,6 +479,7 @@ function SessionPickerList({
pinnedKeys,
pendingDeleteKey,
showRepo,
showRowActions = true,
labelFor,
emptyState,
onOpenRow,
@@ -504,6 +529,7 @@ function SessionPickerList({
isPinned={pinnedKeys.has(row.key)}
isPendingDelete={pendingDeleteKey === row.key}
showRepo={showRepo}
showActions={showRowActions}
label={labelFor?.(row)}
onOpen={onOpenRow}
onHover={onHoverRow}
@@ -527,6 +553,7 @@ interface SessionPickerContentProps {
currentRepo: Repo | undefined
activeSessionID?: string
onSelectSession: (session: Session, repo: Repo) => void
onSelectRemoteSession?: (session: Session, serverId: string) => void
onDeleteActiveSession: () => void
switchHint: string
searchInputRef: React.RefObject<HTMLInputElement | null>
@@ -538,6 +565,7 @@ function SessionPickerContent({
currentRepo,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
switchHint,
searchInputRef,
@@ -616,11 +644,18 @@ function SessionPickerContent({
directories,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
})
const sections = useMemo(
() => buildSections(filteredSessions, pinnedKeys, getSessionKey, resolveRepo),
() =>
buildSections(
filteredSessions.filter((session) => Boolean(resolveRepo(session))),
pinnedKeys,
getSessionKey,
resolveRepo,
),
[filteredSessions, pinnedKeys, resolveRepo],
)
const rows = useMemo(() => sections.flatMap((section) => section.rows), [sections])
@@ -787,6 +822,7 @@ interface ActivePickerContentProps {
open: boolean
activeSessionID?: string
onSelectSession: (session: Session, repo: Repo) => void
onSelectRemoteSession?: (session: Session, serverId: string) => void
onDeleteActiveSession: () => void
switchHint: string
searchInputRef: React.RefObject<HTMLInputElement | null>
@@ -796,6 +832,7 @@ function ActivePickerContent({
open,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
switchHint,
searchInputRef,
@@ -815,7 +852,14 @@ function ActivePickerContent({
const rows: SessionPickerRow[] = []
for (const item of groups[definition.key]) {
if (needle && !(item.session.title ?? '').toLowerCase().includes(needle)) continue
rows.push({ key: item.key, session: item.session, repo: item.repo, updatedAt: item.updatedAt })
rows.push({
key: item.key,
session: item.session,
repo: item.repo ?? undefined,
serverId: item.serverId ?? undefined,
serverName: item.serverName ?? undefined,
updatedAt: item.updatedAt,
})
}
return rows.length > 0 ? [{ id: definition.key, heading: definition.heading, rows }] : []
})
@@ -838,7 +882,10 @@ function ActivePickerContent({
} = usePickerCursor(rows, defaultCursorKey)
const directories = useMemo(
() => Array.from(new Set(rows.map((row) => row.session.location.directory))),
() =>
Array.from(
new Set(rows.filter((row) => !row.serverId).map((row) => row.session.location.directory)),
),
[rows],
)
@@ -855,6 +902,7 @@ function ActivePickerContent({
directories,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
})
@@ -934,14 +982,179 @@ function ActivePickerContent({
)
}
const EMPTY_PINNED_KEYS: ReadonlySet<string> = new Set()
interface RemoteSessionPickerContentProps {
open: boolean
serverName?: string
projectDirectory?: string
activeSessionID?: string
onSelectSession: (session: Session) => void
switchHint: string
searchInputRef: React.RefObject<HTMLInputElement | null>
}
function RemoteSessionPickerContent({
open,
serverName,
projectDirectory,
activeSessionID,
onSelectSession,
switchHint,
searchInputRef,
}: RemoteSessionPickerContentProps) {
const listRef = useRef<HTMLDivElement>(null)
const listId = useId()
const optionBaseId = useId()
const {
query,
setQuery,
trimmedQuery,
filteredSessions,
isSearchPending,
isLoading,
fetchNextPage,
isFetchingNextPage,
isFetchNextPageError,
canFetchNextPage,
} = useSessionSearch(projectDirectory ? [projectDirectory] : [])
const sections = useMemo(
() => buildSections(filteredSessions, EMPTY_PINNED_KEYS, getSessionKey),
[filteredSessions],
)
const rows = useMemo(() => sections.flatMap((section) => section.rows), [sections])
const activeRow = useMemo(
() => rows.find((row) => row.session.id === activeSessionID),
[rows, activeSessionID],
)
const defaultCursorKey = !trimmedQuery && activeRow ? activeRow.key : rows[0]?.key ?? null
const {
rowIndexByKey,
cursorIndex,
cursorRow,
registerRef,
moveCursor,
resetCursor,
} = usePickerCursor(rows, defaultCursorKey)
const openRow = useCallback(
(row: SessionPickerRow | undefined) => {
if (row) onSelectSession(row.session)
},
[onSelectSession],
)
useEffect(() => {
resetCursor()
}, [trimmedQuery, resetCursor])
const handleSearchKeyDown = usePickerKeyNav({
rows,
cursorIndex,
cursorRow,
moveCursor,
onOpenRow: openRow,
})
const handleListScroll = useCallback(
(event: React.UIEvent<HTMLDivElement>) => {
const { scrollTop, scrollHeight, clientHeight } = event.currentTarget
if (scrollHeight - scrollTop - clientHeight <= NEAR_BOTTOM_PX && canFetchNextPage) {
void fetchNextPage()
}
},
[canFetchNextPage, fetchNextPage],
)
const labelFor = useCallback(() => serverName, [serverName])
return (
<div className="mt-4 flex min-h-0 flex-1 flex-col">
<PickerSearchBar
inputRef={searchInputRef}
listId={listId}
open={open}
activeDescendantId={cursorIndex >= 0 ? optionId(optionBaseId, cursorIndex) : undefined}
value={query}
onChange={setQuery}
onKeyDown={handleSearchKeyDown}
placeholder="Search sessions..."
name="remote-session-picker-search"
/>
<SessionPickerList
listRef={listRef}
listId={listId}
optionBaseId={optionBaseId}
ariaLabel="Sessions"
sections={sections}
rows={rows}
rowIndexByKey={rowIndexByKey}
cursorIndex={cursorIndex}
activeSessionID={activeSessionID}
pinnedKeys={EMPTY_PINNED_KEYS}
pendingDeleteKey={null}
showRepo={false}
showRowActions={false}
labelFor={labelFor}
emptyState={
isLoading
? 'Loading sessions...'
: isSearchPending
? 'Searching sessions...'
: trimmedQuery
? 'No sessions found'
: 'No sessions yet'
}
onOpenRow={openRow}
onHoverRow={moveCursor}
onTogglePin={() => undefined}
onRequestDelete={() => undefined}
registerRef={registerRef}
onScroll={handleListScroll}
>
{isFetchNextPageError && (
<div className="flex flex-col items-center gap-2 py-4">
<p className="text-sm text-muted-foreground">Failed to load more sessions</p>
<Button
variant="outline"
size="sm"
onClick={() => {
void fetchNextPage()
}}
disabled={isFetchingNextPage}
>
Retry
</Button>
</div>
)}
{isFetchingNextPage && rows.length > 0 && (
<div className="py-4 text-center text-sm text-muted-foreground">Loading more sessions...</div>
)}
</SessionPickerList>
<div className="mt-2 shrink-0 px-4 text-xs text-muted-foreground pointer-coarse:hidden sm:px-0">
↑↓ move · Enter open
{switchHint ? ` · ${switchHint} sessions` : ''}
</div>
</div>
)
}
export function SessionPickerDialog({
open,
onOpenChange,
currentRepo,
activeSessionID,
onSelectSession,
onSelectRemoteSession,
onDeleteActiveSession,
onSelectRepo,
remote,
}: SessionPickerDialogProps) {
const searchInputRef = useRef<HTMLInputElement>(null)
const activeSearchInputRef = useRef<HTMLInputElement>(null)
@@ -960,6 +1173,8 @@ export function SessionPickerDialog({
},
[preferences],
)
const isRemote = remote !== undefined
const pickerTabs = isRemote ? REMOTE_SESSION_PICKER_TABS : SESSION_PICKER_TABS
const [tab, setTab] = useState<SessionPickerTab>('sessions')
const [prevOpen, setPrevOpen] = useState(open)
@@ -968,10 +1183,12 @@ export function SessionPickerDialog({
if (open) setTab('sessions')
}
const activeTab: SessionPickerTab = isRemote && tab === 'projects' ? 'sessions' : tab
const toggleTab = useCallback(() => {
focusSearchAfterSwitchRef.current = true
setTab((current) => nextSessionPickerTab(current))
}, [])
setTab((current) => nextSessionPickerTab(current, pickerTabs))
}, [pickerTabs])
useShortcutActions({ switchPickerTab: open ? toggleTab : undefined })
@@ -983,12 +1200,12 @@ export function SessionPickerDialog({
if (!hasFinePointer && !focusSearchAfterSwitchRef.current) return
const frame = requestAnimationFrame(() => {
focusSearchAfterSwitchRef.current = false
if (tab === 'projects') projectSearchInputRef.current?.focus()
else if (tab === 'active') activeSearchInputRef.current?.focus()
if (activeTab === 'projects') projectSearchInputRef.current?.focus()
else if (activeTab === 'active') activeSearchInputRef.current?.focus()
else searchInputRef.current?.focus()
})
return () => cancelAnimationFrame(frame)
}, [open, tab, hasFinePointer])
}, [open, activeTab, hasFinePointer])
return (
<Dialog open={open} onOpenChange={onOpenChange}>
@@ -1006,16 +1223,16 @@ export function SessionPickerDialog({
}}
>
<Tabs
value={tab}
value={activeTab}
onValueChange={(value) => setTab(value as SessionPickerTab)}
className="flex min-h-0 w-full min-w-0 flex-1 flex-col gap-0"
>
<div className="shrink-0 px-4 pt-4 sm:px-0 sm:pt-6">
<DialogTitle className="sr-only">{SESSION_PICKER_TAB_LABELS[tab]}</DialogTitle>
<DialogTitle className="sr-only">{SESSION_PICKER_TAB_LABELS[activeTab]}</DialogTitle>
<TabsList>
<TabsTrigger value="active">Active</TabsTrigger>
<TabsTrigger value="sessions">Sessions</TabsTrigger>
<TabsTrigger value="projects">Projects</TabsTrigger>
{isRemote ? null : <TabsTrigger value="projects">Projects</TabsTrigger>}
</TabsList>
</div>
<TabsContent
@@ -1026,6 +1243,7 @@ export function SessionPickerDialog({
open={open}
activeSessionID={activeSessionID}
onSelectSession={onSelectSession}
onSelectRemoteSession={onSelectRemoteSession}
onDeleteActiveSession={onDeleteActiveSession}
switchHint={switchHint}
searchInputRef={activeSearchInputRef}
@@ -1035,29 +1253,44 @@ export function SessionPickerDialog({
value="sessions"
className="mt-0 flex min-h-0 w-full min-w-0 flex-1 flex-col px-0"
>
<SessionPickerContent
open={open}
currentRepo={currentRepo}
activeSessionID={activeSessionID}
onSelectSession={onSelectSession}
onDeleteActiveSession={onDeleteActiveSession}
switchHint={switchHint}
searchInputRef={searchInputRef}
hasFinePointer={hasFinePointer}
/>
</TabsContent>
<TabsContent
value="projects"
className="mt-0 flex min-h-0 w-full min-w-0 flex-1 flex-col px-0"
>
<ProjectPickerContent
open={open}
currentRepo={currentRepo}
onSelectRepo={onSelectRepo}
switchHint={switchHint}
searchInputRef={projectSearchInputRef}
/>
{remote ? (
<RemoteSessionPickerContent
open={open}
serverName={remote.serverName}
projectDirectory={remote.projectDirectory}
activeSessionID={activeSessionID}
onSelectSession={(session) => onSelectRemoteSession?.(session, remote.serverId)}
switchHint={switchHint}
searchInputRef={searchInputRef}
/>
) : (
<SessionPickerContent
open={open}
currentRepo={currentRepo}
activeSessionID={activeSessionID}
onSelectSession={onSelectSession}
onSelectRemoteSession={onSelectRemoteSession}
onDeleteActiveSession={onDeleteActiveSession}
switchHint={switchHint}
searchInputRef={searchInputRef}
hasFinePointer={hasFinePointer}
/>
)}
</TabsContent>
{isRemote ? null : (
<TabsContent
value="projects"
className="mt-0 flex min-h-0 w-full min-w-0 flex-1 flex-col px-0"
>
<ProjectPickerContent
open={open}
currentRepo={currentRepo}
onSelectRepo={onSelectRepo}
switchHint={switchHint}
searchInputRef={projectSearchInputRef}
/>
</TabsContent>
)}
</Tabs>
</DialogContent>
</Dialog>