diff --git a/src/main/runtime/workspaces/process-listing/runtime-worktree-ps-summaries.ts b/src/main/runtime/workspaces/process-listing/runtime-worktree-ps-summaries.ts index ab145225128..2a1d2e48e74 100644 --- a/src/main/runtime/workspaces/process-listing/runtime-worktree-ps-summaries.ts +++ b/src/main/runtime/workspaces/process-listing/runtime-worktree-ps-summaries.ts @@ -21,6 +21,8 @@ export function buildRuntimeWorktreePsSummaries(args: { store: RuntimeStore | null resolvedWorktrees: readonly ResolvedWorktree[] platformByRepoId: ReadonlyMap + /** Only this workspace's rows (every host's copy); PTY liveness still reads every worktree. */ + worktreeId?: string }): Map { const summaries = new Map() const metaByHost = new Map>() @@ -42,8 +44,12 @@ export function buildRuntimeWorktreePsSummaries(args: { const ghCache = args.store?.getGitHubCache?.() const crossStoreLineage = args.store?.getAllCrossStoreWorkspaceLineage?.() ?? {} const workspaceLineage = args.store?.getAllWorkspaceLineage?.() + const resolvedWorktrees = + args.worktreeId === undefined + ? args.resolvedWorktrees + : args.resolvedWorktrees.filter((worktree) => worktree.id === args.worktreeId) for (const { worktree, repo } of withOwnerRepos( - args.resolvedWorktrees, + resolvedWorktrees, args.store?.getRepos() ?? [] )) { const meta = readMeta(worktree) @@ -144,6 +150,9 @@ export function buildRuntimeWorktreePsSummaries(args: { continue } const worktree = folderWorkspaceToWorktree(folderWorkspace) + if (args.worktreeId !== undefined && worktree.id !== args.worktreeId) { + continue + } setPsSummary(summaries, { workspaceKind: 'folder-workspace', worktreeId: worktree.id, diff --git a/src/main/runtime/workspaces/process-listing/workspace-process-listing.ts b/src/main/runtime/workspaces/process-listing/workspace-process-listing.ts index bef13877ace..7cabcfaa5a1 100644 --- a/src/main/runtime/workspaces/process-listing/workspace-process-listing.ts +++ b/src/main/runtime/workspaces/process-listing/workspace-process-listing.ts @@ -75,7 +75,8 @@ export abstract class OrcaRuntimeWithGetWorktreePs extends RuntimeChatWorkerEven async getWorktreePs( limit = DEFAULT_WORKTREE_PS_LIMIT, sourceDefaultsSupported = true, - includeAgentWait = false + includeAgentWait = false, + worktreeId?: string ): Promise { if (!Number.isInteger(limit) || limit <= 0) { throw new Error('invalid_limit') @@ -85,13 +86,12 @@ export abstract class OrcaRuntimeWithGetWorktreePs extends RuntimeChatWorkerEven sourceDefaultsSupported ) const freshPtyLiveness = await this.refreshPtyWorktreeRecordsFromController(resolvedWorktrees) - const repoById = new Map((this.store?.getRepos() ?? []).map((repo) => [repo.id, repo])) const summaries = buildRuntimeWorktreePsSummaries({ store: this.store, resolvedWorktrees, - platformByRepoId + platformByRepoId, + worktreeId }) - const getSummary = createRuntimeWorktreePsSummaryLookup( summaries, buildRuntimeWorktreeSummaryPathIndex(summaries, platformByRepoId) @@ -121,7 +121,7 @@ export abstract class OrcaRuntimeWithGetWorktreePs extends RuntimeChatWorkerEven applyRuntimeWorktreePsSessionActivity({ store: this.store, summaries, - repoById, + repoById: new Map((this.store?.getRepos() ?? []).map((repo) => [repo.id, repo])), ptysById: this.ptysById, tabs: this.tabs, paneOf: (pty) => this.paneOfPty(pty), @@ -144,10 +144,9 @@ export abstract class OrcaRuntimeWithGetWorktreePs extends RuntimeChatWorkerEven agentList: this.readAgentListPublication() }) - const sorted = [...summaries.values()].sort(compareWorktreePs) // Apply per-host pagination so hosts sorting last cannot disappear behind a global cap (#18104). const page = buildWorktreeListingPage( - sorted, + [...summaries.values()].sort(compareWorktreePs), limit, withoutHiddenHosts(this.listKnownExecutionHostIds(), runtimeHiddenHostIds(this.store)) ) diff --git a/src/main/runtime/workspaces/process-listing/worktree-ps-host-scope.test.ts b/src/main/runtime/workspaces/process-listing/worktree-ps-host-scope.test.ts index 20820a20d24..9f051e12b76 100644 --- a/src/main/runtime/workspaces/process-listing/worktree-ps-host-scope.test.ts +++ b/src/main/runtime/workspaces/process-listing/worktree-ps-host-scope.test.ts @@ -117,4 +117,14 @@ describe('worktree.ps host coverage', () => { ) expect(result.hostScope?.hostIds).toEqual(['local', `ssh:${SSH_CONNECTION_ID}`]) }) + + it('lists only the named workspace when scoped to one id', async () => { + const runtime = new OrcaRuntimeService(makeStore() as never) + const target = `${LOCAL_REPO_ID}::${LOCAL_REPO_PATH}-b` + + const result = await runtime.getWorktreePs(100, true, false, target) + + expect(result.truncated).toBe(false) + expect(result.worktrees.map((worktree) => worktree.worktreeId)).toEqual([target]) + }) }) diff --git a/src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts b/src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts index bdcc8d4fd8c..49f432068d7 100644 --- a/src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts +++ b/src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts @@ -181,21 +181,23 @@ export async function collectWorkspaceCleanupWorkspaces(args: { }): Promise { const { store, runtime, globalRules, now, worktreeId, signal } = args const repos = listWorkspaceCleanupOwnedRepos(store) - const ps = await runtime.getWorktreePs(WORKTREE_PS_LIMIT) + // A re-check projects only its target, so M removals don't rebuild the whole host's listing M times. + const ps = await runtime.getWorktreePs(WORKTREE_PS_LIMIT, true, false, worktreeId) if (ps.truncated) { throw new Error('Could not list every workspace on this host.') } - const summaries = ps.worktrees.filter( - (summary) => - isOwnedSummary(summary, repos) && - (worktreeId === undefined || summary.worktreeId === worktreeId) - ) + const summaries = ps.worktrees.filter((summary) => isOwnedSummary(summary, repos)) const policies = new Map() - for (const repo of repos.values()) { - policies.set( - repo.id, - resolveWorkspaceCleanupPolicy({ workspaceCleanupPolicy: globalRules }, repo) - ) + for (const summary of summaries) { + if (!policies.has(summary.repoId)) { + policies.set( + summary.repoId, + resolveWorkspaceCleanupPolicy( + { workspaceCleanupPolicy: globalRules }, + repos.get(summary.repoId)! + ) + ) + } } const gitTargets = summaries .filter((summary) => {