mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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:
1 parent
1d31286904
commit
bfbbbf08ef
23 files changed
+932
-81
No files matched your search
+120
-10
@@ -4,6 +4,7 @@ import type { SessionStatusType } from '@/stores/sessionStatusStore'
|
||||
import { getSessionKey } from '@/lib/sessionKey'
|
||||
import { getSessionPath } from '@/lib/navigation'
|
||||
import { findRepoForDirectory, normalizeDirectory } from '@/components/navigation/sidebar-session-tree'
|
||||
import { isWorktreeSibling, type RepoSibling } from '@opencode-manager/shared/utils'
|
||||
|
||||
export const MS_PER_MINUTE = 60_000
|
||||
|
||||
@@ -34,6 +35,8 @@ export interface ActiveWorkItem {
|
||||
state: ActiveWorkState
|
||||
updatedAt: number
|
||||
outcome?: ChildOutcome
|
||||
/** The worktree's branch when the session runs in a worktree of `repo`; null otherwise. */
|
||||
worktreeBranch?: string | null
|
||||
path: string
|
||||
}
|
||||
|
||||
@@ -166,29 +169,126 @@ export function buildRepoActivityIndex(
|
||||
return index
|
||||
}
|
||||
|
||||
/** The repo's activity from a prebuilt index, checking each of its paths. Needs-input outranks running. */
|
||||
/**
|
||||
* The repo's activity from a prebuilt index, checking each of its own paths and the directories of
|
||||
* the worktrees it owns. Needs-input outranks running.
|
||||
*/
|
||||
export function repoActivityFromIndex(
|
||||
repo: Repo,
|
||||
index: ReadonlyMap<string, 'needs-input' | 'running'>,
|
||||
worktrees?: ReadonlyArray<ActiveWorktree>,
|
||||
): 'needs-input' | 'running' | null {
|
||||
let running = false
|
||||
|
||||
for (const path of [repo.fullPath, repo.localPath, repo.sourcePath]) {
|
||||
const normalized = normalizeDirectory(path)
|
||||
if (!normalized) continue
|
||||
const check = (directory?: string | null): boolean => {
|
||||
const normalized = normalizeDirectory(directory)
|
||||
if (!normalized) return false
|
||||
const state = index.get(normalized)
|
||||
if (state === 'needs-input') return 'needs-input'
|
||||
if (state === 'needs-input') return true
|
||||
if (state === 'running') running = true
|
||||
return false
|
||||
}
|
||||
|
||||
for (const path of [repo.fullPath, repo.localPath, repo.sourcePath]) {
|
||||
if (check(path)) return 'needs-input'
|
||||
}
|
||||
|
||||
for (const worktree of worktrees ?? []) {
|
||||
if (worktree.ownerRepo.id !== repo.id) continue
|
||||
if (check(worktree.directory)) return 'needs-input'
|
||||
}
|
||||
|
||||
return running ? 'running' : null
|
||||
}
|
||||
|
||||
/**
|
||||
* True when some indexed active directory matches no repo, so a session is active in a directory
|
||||
* outside the repo list (such as a worktree) and the worktree lookup is worth running.
|
||||
*/
|
||||
export function hasUnmatchedActiveDirectory(
|
||||
index: ReadonlyMap<string, 'needs-input' | 'running'>,
|
||||
repos: Repo[],
|
||||
): boolean {
|
||||
for (const directory of index.keys()) {
|
||||
if (!findRepoForDirectory(repos, directory)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** A worktree directory and the repo that owns it, discovered from that repo's siblings. */
|
||||
export interface ActiveWorktree {
|
||||
directory: string
|
||||
ownerRepo: Repo
|
||||
branch: string | null
|
||||
/** True when the worktree belongs to a schedule rather than an interactive session. */
|
||||
isSchedule: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the worktrees discovered through each repo's siblings, keyed by directory so a session
|
||||
* running in a worktree resolves to its owning repo. A repo's siblings include the worktrees of its
|
||||
* whole OpenCode project, so the same worktree can come back from several repos: the non-worktree
|
||||
* repo wins so a worktree session opens on the main repo, and duplicates are dropped.
|
||||
*/
|
||||
export function buildWorktreeActivity(
|
||||
repos: Repo[],
|
||||
siblingResults: ReadonlyArray<{ data?: RepoSibling[] }>,
|
||||
): ActiveWorktree[] {
|
||||
const ordered = repos
|
||||
.map((repo, index) => ({ repo, siblings: siblingResults[index]?.data }))
|
||||
.sort((a, b) => Number(Boolean(a.repo.isWorktree)) - Number(Boolean(b.repo.isWorktree)))
|
||||
|
||||
const worktrees: ActiveWorktree[] = []
|
||||
const seen = new Set<string>()
|
||||
|
||||
for (const { repo, siblings } of ordered) {
|
||||
for (const sibling of siblings ?? []) {
|
||||
if (!isWorktreeSibling(sibling)) continue
|
||||
const directory = normalizeDirectory(sibling.fullPath)
|
||||
if (!directory || seen.has(directory)) continue
|
||||
seen.add(directory)
|
||||
worktrees.push({
|
||||
directory,
|
||||
ownerRepo: repo,
|
||||
branch: sibling.currentBranch ?? sibling.branch ?? null,
|
||||
isSchedule: sibling.worktreeSource === 'schedule',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return worktrees
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*/
|
||||
export function needsWorktreeLookup(input: {
|
||||
now: number
|
||||
windowMs: number
|
||||
repos: Repo[]
|
||||
sessions: Session[]
|
||||
parents: ReadonlyMap<string, string | null>
|
||||
roots: { needsInput: Set<string>; running: Set<string> }
|
||||
}): boolean {
|
||||
const { now, windowMs, repos, sessions, parents, roots } = input
|
||||
const windowStart = now - windowMs
|
||||
|
||||
return sessions.some((session) => {
|
||||
if (session.time.archived) return false
|
||||
if (findRepoForDirectory(repos, session.location.directory)) return false
|
||||
if (session.time.updated >= windowStart) return true
|
||||
const rootID = rootSessionID(session.id, parents)
|
||||
return roots.needsInput.has(rootID) || roots.running.has(rootID)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups the given sessions into needs-input, running, and recent roots. A session appears in
|
||||
* exactly one group (needs-input over running over recent) and only when it resolves to a repo.
|
||||
* The input may mix roots and children; children never become items, but their latest update time
|
||||
* rolls up into the root's `updatedAt` and decides the recent window and ordering.
|
||||
* A recent session in a schedule worktree is omitted so schedules only surface while they need
|
||||
* attention. The input may mix roots and children; children never become items, but their latest
|
||||
* update time rolls up into the root's `updatedAt` and decides the recent window and ordering.
|
||||
*/
|
||||
export function buildActiveWork(input: {
|
||||
now: number
|
||||
@@ -198,10 +298,16 @@ export function buildActiveWork(input: {
|
||||
parents: ReadonlyMap<string, string | null>
|
||||
roots: { needsInput: Set<string>; running: Set<string> }
|
||||
outcomes: ReadonlyMap<string, ChildOutcome>
|
||||
worktrees?: ReadonlyArray<ActiveWorktree>
|
||||
}): ActiveWorkGroups {
|
||||
const { now, windowMs, repos, sessions, parents, roots, outcomes } = input
|
||||
const { now, windowMs, repos, sessions, parents, roots, outcomes, worktrees } = input
|
||||
const windowStart = now - windowMs
|
||||
|
||||
const worktreeByDirectory = new Map<string, ActiveWorktree>()
|
||||
for (const worktree of worktrees ?? []) {
|
||||
worktreeByDirectory.set(worktree.directory, worktree)
|
||||
}
|
||||
|
||||
const activityByRoot = new Map<string, number>()
|
||||
for (const session of sessions) {
|
||||
if (session.time.archived) continue
|
||||
@@ -232,8 +338,11 @@ export function buildActiveWork(input: {
|
||||
: null
|
||||
if (!state) continue
|
||||
|
||||
const repo = findRepoForDirectory(repos, session.location.directory)
|
||||
const directory = normalizeDirectory(session.location.directory)
|
||||
const worktree = directory ? worktreeByDirectory.get(directory) : undefined
|
||||
const repo = worktree?.ownerRepo ?? findRepoForDirectory(repos, session.location.directory)
|
||||
if (!repo) continue
|
||||
if (worktree?.isSchedule && state === 'recent') continue
|
||||
|
||||
seen.add(session.id)
|
||||
const item: ActiveWorkItem = {
|
||||
@@ -243,7 +352,8 @@ export function buildActiveWork(input: {
|
||||
state,
|
||||
updatedAt,
|
||||
outcome: outcomes.get(session.id),
|
||||
path: getSessionPath(repo.id, session.id),
|
||||
worktreeBranch: worktree ? worktree.branch : null,
|
||||
path: getSessionPath(repo.id, session.id, worktree ? { repoTab: 'workspaces' } : undefined),
|
||||
}
|
||||
|
||||
if (state === 'needs-input') needsInput.push(item)
|
||||
|
||||
Reference in new issue
Block a user