perf(workspace-cleanup): scope each removal's fresh re-check to its target

The per-removal safety re-check listed and projected every workspace on the
host before filtering to one id, so M removals rebuilt the listing M times.
getWorktreePs now takes an optional id and projects only that workspace's
rows; PTY liveness still refreshes against every worktree.

Refs STA-15293
This commit is contained in:
Kelvin Amoaba
2026-11-14 02:42:22 +00:00
parent a8f6e79631
commit c1fad9858d
4 changed files with 39 additions and 19 deletions
@@ -21,6 +21,8 @@ export function buildRuntimeWorktreePsSummaries(args: {
store: RuntimeStore | null
resolvedWorktrees: readonly ResolvedWorktree[]
platformByRepoId: ReadonlyMap<string, NodeJS.Platform>
/** Only this workspace's rows (every host's copy); PTY liveness still reads every worktree. */
worktreeId?: string
}): Map<string, RuntimeWorktreePsSummary> {
const summaries = new Map<string, RuntimeWorktreePsSummary>()
const metaByHost = new Map<ExecutionHostId, Record<string, WorktreeMeta>>()
@@ -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,
@@ -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<RuntimeWorktreePsResult> {
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))
)
@@ -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])
})
})
@@ -181,21 +181,23 @@ export async function collectWorkspaceCleanupWorkspaces(args: {
}): Promise<WorkspaceCleanupWorkspace[]> {
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<string, ResolvedWorkspaceCleanupPolicy>()
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) => {