mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(runtime): keep worktree names through scan timeouts (#37768)
Fixes #16724
This commit is contained in:
@@ -65,6 +65,7 @@ export type RepoWorktreeRowDeps = {
|
||||
repo: Repo,
|
||||
projectRuntimeByRepoId: ReadonlyMap<string, ProjectExecutionRuntimeResolution>
|
||||
) => Promise<RuntimeWorktreeScanResult>
|
||||
getCachedSuccessfulScan?: (repo: Repo) => RuntimeWorktreeScanResult | undefined
|
||||
/** Folder workspaces are stamped from runtime-owned identity helpers, so the caller supplies them. */
|
||||
listFolderWorkspaces: (repo: Repo, repoOwnerCount: number) => Worktree[]
|
||||
}
|
||||
@@ -109,7 +110,7 @@ export type RepoWorktreeRowsScan = { ok: boolean; worktrees: GitWorktreeInfo[] }
|
||||
|
||||
/** The scan half of `resolveRepoWorktreeRows`: slow, and the only part that awaits. */
|
||||
export async function scanRepoWorktreeRows(
|
||||
deps: Pick<RepoWorktreeRowDeps, 'store' | 'scanRepo'>,
|
||||
deps: Pick<RepoWorktreeRowDeps, 'store' | 'scanRepo' | 'getCachedSuccessfulScan'>,
|
||||
repo: Repo,
|
||||
projectRuntimeByRepoId: ReadonlyMap<string, ProjectExecutionRuntimeResolution>,
|
||||
repoOwnerCount = deps.store.getRepos().filter((candidate) => candidate.id === repo.id).length
|
||||
@@ -122,7 +123,12 @@ export async function scanRepoWorktreeRows(
|
||||
.catch(() => ({ ok: false, worktrees: [] }) satisfies RuntimeWorktreeScanResult),
|
||||
RESOLVED_WORKTREE_REPO_TIMEOUT_MS,
|
||||
null
|
||||
)) ?? { ok: false, worktrees: listStoredWorktreeRowsForRepo(store, repo, repoOwnerCount) }
|
||||
)) ?? {
|
||||
ok: false,
|
||||
worktrees:
|
||||
deps.getCachedSuccessfulScan?.(repo)?.worktrees ??
|
||||
listStoredWorktreeRowsForRepo(store, repo, repoOwnerCount)
|
||||
}
|
||||
const gitWorktrees = preserveFolderUpgradeWorktreePath(repo, scan.worktrees)
|
||||
if (scan.ok) {
|
||||
pruneLineageForMissingRepoWorktrees(store, repo, gitWorktrees)
|
||||
|
||||
@@ -1,10 +1,36 @@
|
||||
import type { Repo } from '../../shared/repo-types'
|
||||
import { isAgentScratchRepoRootPath } from '../../shared/agent-scratch-worktrees'
|
||||
import { getRepoRoutingConnectionId } from '../../shared/execution-host'
|
||||
import { getRepoRoutingConnectionId, getRepoSshConnectionId } from '../../shared/execution-host'
|
||||
import type { ProjectExecutionRuntimeResolution } from '../../shared/project-execution-runtime'
|
||||
import {
|
||||
resolveLocalProjectRuntimeForRepo,
|
||||
type ProjectRuntimeResolutionStore
|
||||
} from '../workspaces/execution-host/local-project-runtime-resolution'
|
||||
|
||||
const WORKTREE_SCAN_CACHE_TTL_MS = 30_000
|
||||
const WORKTREE_SCAN_AGENT_SCRATCH_TTL_MS = 5 * 60_000
|
||||
|
||||
export function resolveWorktreeScanRuntime(
|
||||
store: ProjectRuntimeResolutionStore,
|
||||
repo: Repo,
|
||||
projectRuntimeByRepoId?: ReadonlyMap<string, ProjectExecutionRuntimeResolution>
|
||||
) {
|
||||
const sshConnectionId = getRepoSshConnectionId(repo)
|
||||
const projectRuntime = projectRuntimeByRepoId
|
||||
? projectRuntimeByRepoId.get(repo.id)
|
||||
: !sshConnectionId
|
||||
? resolveLocalProjectRuntimeForRepo(store, repo)
|
||||
: undefined
|
||||
const runtimeKey = projectRuntime
|
||||
? projectRuntime.status === 'resolved'
|
||||
? projectRuntime.runtime.cacheKey
|
||||
: projectRuntime.repair.cacheKey
|
||||
: sshConnectionId
|
||||
? `ssh:${sshConnectionId}`
|
||||
: 'local:default'
|
||||
return { projectRuntime, runtimeKey, sshConnectionId }
|
||||
}
|
||||
|
||||
export function resolveWorktreeScanCacheTtlMs(
|
||||
repo: Pick<Repo, 'path' | 'connectionId' | 'executionHostId'>
|
||||
): number {
|
||||
|
||||
@@ -32,7 +32,7 @@ export type ResolvedWorktreeRepoScans = Map<
|
||||
export type ResolvedWorktreeScanJoin = ResolvedWorktreeSnapshot & {
|
||||
/** Git repos the scans predate, which list no rows until scanned. */
|
||||
unscannedRepoKeys: ReadonlySet<HostScopedRepoKey>
|
||||
/** Rows a scan that reached a verdict listed, by their git run; a stalled scan's carry no branch. */
|
||||
/** Rows a scan that reached a verdict listed, by their git run; stalled scans remain degraded. */
|
||||
gitRunById: ReadonlyMap<HostScopedWorktreeKey, object | null>
|
||||
/** Repos whose scan reached a verdict, so a row they no longer list is gone. */
|
||||
scannedRepoKeys: ReadonlySet<HostScopedRepoKey>
|
||||
|
||||
@@ -6,10 +6,7 @@ import { isPathInsideOrEqual } from '../../../shared/cross-platform-path'
|
||||
import type { ResolvedWorktreeSnapshot } from '../runtime-resolved-worktree-cache'
|
||||
import { RESOLVED_WORKTREE_CACHE_TTL_MS } from '../orca-runtime-postlude'
|
||||
import { getWorktreeScanMutationRevision } from '../../workspaces/catalog/local-worktree-scan-generation'
|
||||
import {
|
||||
resolveLocalProjectRuntimeForRepo,
|
||||
resolveLocalProjectRuntimesForRepos
|
||||
} from '../../workspaces/execution-host/local-project-runtime-resolution'
|
||||
import { resolveLocalProjectRuntimesForRepos } from '../../workspaces/execution-host/local-project-runtime-resolution'
|
||||
import {
|
||||
resolveRepoWorktreeRows,
|
||||
resolveScopedWorktreeIdRow,
|
||||
@@ -31,17 +28,16 @@ import type { ExecutionHostId } from '../../../shared/execution-host'
|
||||
import type { Repo } from '../../../shared/repo-types'
|
||||
import type { ProjectExecutionRuntimeResolution } from '../../../shared/project-execution-runtime'
|
||||
import type { RuntimeWorktreeScanResult } from '../repo-worktree-resolution-scan'
|
||||
import {
|
||||
getRepoExecutionHostId,
|
||||
getRepoSshConnectionId,
|
||||
LOCAL_EXECUTION_HOST_ID
|
||||
} from '../../../shared/execution-host'
|
||||
import { getRepoExecutionHostId, LOCAL_EXECUTION_HOST_ID } from '../../../shared/execution-host'
|
||||
import { withUnregisteredRemovalCheckouts } from '../../workspaces/removal/worktree-removal-listing'
|
||||
import { withoutInFlightWorktreeCreates } from '../../workspaces/creation/worktree-create-in-flight'
|
||||
import { preserveFolderUpgradeWorktreePath } from '../../workspaces/catalog/folder-upgrade-worktree-path'
|
||||
import type { RuntimeWorktreeScanCache } from '../orca-runtime-core'
|
||||
import { getRepoHostIdentityForParts } from '../../../shared/repo-host-identity'
|
||||
import { resolveWorktreeScanCacheTtlMs } from '../runtime-worktree-scan-cache'
|
||||
import {
|
||||
resolveWorktreeScanCacheTtlMs,
|
||||
resolveWorktreeScanRuntime
|
||||
} from '../runtime-worktree-scan-cache'
|
||||
|
||||
export abstract class RuntimeWorkspaceCatalog extends OrcaRuntimeWithReadKittyFile {
|
||||
protected abstract refreshRepoWorktreeScan(
|
||||
@@ -191,6 +187,18 @@ export abstract class RuntimeWorkspaceCatalog extends OrcaRuntimeWithReadKittyFi
|
||||
store,
|
||||
scanRepo: (repo, projectRuntimeByRepoId) =>
|
||||
this.listRepoWorktreesForListing(repo, projectRuntimeByRepoId),
|
||||
getCachedSuccessfulScan: (repo) => {
|
||||
const key = `${repo.id}\0${getRepoExecutionHostId(repo)}`
|
||||
const cached = this.worktreeScanCache.get(key)
|
||||
if (
|
||||
cached?.generation === (this.worktreeScanGenerations.get(key) ?? 0) &&
|
||||
cached.runtimeKey === resolveWorktreeScanRuntime(store, repo).runtimeKey &&
|
||||
cached.listing?.ok
|
||||
) {
|
||||
return cached.listing
|
||||
}
|
||||
return undefined
|
||||
},
|
||||
listFolderWorkspaces: (repo, repoOwnerCount) =>
|
||||
listRuntimeFolderWorkspaces(store, repo, repoOwnerCount)
|
||||
}
|
||||
@@ -235,20 +243,11 @@ export abstract class RuntimeWorkspaceCatalog extends OrcaRuntimeWithReadKittyFi
|
||||
repo: Repo,
|
||||
projectRuntimeByRepoId?: ReadonlyMap<string, ProjectExecutionRuntimeResolution>
|
||||
): Promise<RuntimeWorktreeScanResult> {
|
||||
// Resolve executionHostId-only SSH rows too; local routing would miskey caches and miss provider replacements.
|
||||
const sshConnectionId = getRepoSshConnectionId(repo)
|
||||
const projectRuntime = projectRuntimeByRepoId
|
||||
? projectRuntimeByRepoId.get(repo.id)
|
||||
: !sshConnectionId
|
||||
? resolveLocalProjectRuntimeForRepo(this.requireStore(), repo)
|
||||
: undefined
|
||||
const runtimeKey = projectRuntime
|
||||
? projectRuntime.status === 'resolved'
|
||||
? projectRuntime.runtime.cacheKey
|
||||
: projectRuntime.repair.cacheKey
|
||||
: sshConnectionId
|
||||
? `ssh:${sshConnectionId}`
|
||||
: 'local:default'
|
||||
const { projectRuntime, runtimeKey, sshConnectionId } = resolveWorktreeScanRuntime(
|
||||
this.requireStore(),
|
||||
repo,
|
||||
projectRuntimeByRepoId
|
||||
)
|
||||
const now = Date.now()
|
||||
const scanScopeKey = `${repo.id}\0${getRepoExecutionHostId(repo)}`
|
||||
const retryCurrent = (): Promise<RuntimeWorktreeScanResult> => {
|
||||
|
||||
@@ -22,6 +22,9 @@ vi.mock('../git/worktree-listing', async (importOriginal) => {
|
||||
})
|
||||
|
||||
vi.mock('electron', () => electronMocks)
|
||||
vi.mock('./repo-worktree-admin-fingerprint', () => ({
|
||||
readRepoWorktreeAdminFingerprint: vi.fn(async () => null)
|
||||
}))
|
||||
|
||||
const listWorktreesStrictMock = vi.hoisted(() => vi.fn())
|
||||
vi.mock('../git/worktree', async (importOriginal) => ({
|
||||
@@ -34,6 +37,11 @@ import type { WorktreeMeta } from '../../shared/worktree/meta-types'
|
||||
import type { WorktreeLineage } from '../../shared/worktree/lineage-types'
|
||||
import { worktreeWorkspaceKey } from '../../shared/workspace-scope'
|
||||
import { OrcaRuntimeService } from './orca-runtime'
|
||||
import { projectRepoWorktreeRows, scanRepoWorktreeRows } from './repo-worktree-row-resolution'
|
||||
import type { Repo } from '../../shared/repo-types'
|
||||
import type { ProjectExecutionRuntimeResolution } from '../../shared/project-execution-runtime'
|
||||
import type { RuntimeWorktreeScanResult } from './repo-worktree-resolution-scan'
|
||||
import * as removalTable from '../workspaces/removal/worktree-removal-table'
|
||||
import {
|
||||
hostWorktreeTarget,
|
||||
type HostWorktreeTarget
|
||||
@@ -145,6 +153,218 @@ async function advancePastRepoScanBudget<T>(pending: Promise<T>): Promise<T> {
|
||||
return pending
|
||||
}
|
||||
|
||||
class ScanCacheRuntime extends OrcaRuntimeService {
|
||||
protected override listRepoWorktreesForResolutionUncached =
|
||||
vi.fn<(repo: Repo) => Promise<RuntimeWorktreeScanResult>>()
|
||||
|
||||
readonly scanGit = this.listRepoWorktreesForResolutionUncached
|
||||
|
||||
async scanRows(
|
||||
repo: Repo,
|
||||
runtimes: ReadonlyMap<string, ProjectExecutionRuntimeResolution> = new Map()
|
||||
) {
|
||||
const deps = this.repoWorktreeRowDeps()
|
||||
const scan = await scanRepoWorktreeRows(deps, repo, runtimes)
|
||||
return {
|
||||
...scan,
|
||||
rows: projectRepoWorktreeRows(deps, repo, scan, deps.store.getAllWorktreeMeta())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cachedScanFixture(connectionId: string | null = SSH_CONNECTION_ID) {
|
||||
const hostId = connectionId ? toSshExecutionHostId(connectionId) : LOCAL_EXECUTION_HOST_ID
|
||||
const store = makeStore({
|
||||
connectionId: connectionId ?? undefined,
|
||||
metaById: { [WORKTREE_ID]: makeMeta({ hostId, displayNameIsPinned: false }) }
|
||||
})
|
||||
const runtime = new ScanCacheRuntime(partialRuntimeStore(store))
|
||||
const repo = store.getRepos()[0]!
|
||||
const successfulScan: RuntimeWorktreeScanResult = {
|
||||
ok: true,
|
||||
worktrees: [
|
||||
{
|
||||
path: WORKTREE_PATH,
|
||||
head: 'abc',
|
||||
branch: 'refs/heads/feature',
|
||||
isBare: false,
|
||||
isMainWorktree: false
|
||||
}
|
||||
]
|
||||
}
|
||||
return { runtime, repo, store, successfulScan }
|
||||
}
|
||||
|
||||
describe('successful scan cache on timeout', () => {
|
||||
it.each([SSH_CONNECTION_ID, null])(
|
||||
'keeps automatic names and Git metadata from the last successful scan on %s',
|
||||
async (connectionId) => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const { runtime, repo, successfulScan } = cachedScanFixture(connectionId)
|
||||
runtime.scanGit.mockResolvedValueOnce(successfulScan).mockImplementation(neverSettles)
|
||||
await runtime.scanRows(repo)
|
||||
await vi.advanceTimersByTimeAsync(31_000)
|
||||
|
||||
const result = await advancePastRepoScanBudget(runtime.scanRows(repo))
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.rows).toEqual([
|
||||
expect.objectContaining({
|
||||
displayName: 'feature',
|
||||
branch: 'refs/heads/feature',
|
||||
head: 'abc',
|
||||
hostId: connectionId ? toSshExecutionHostId(connectionId) : LOCAL_EXECUTION_HOST_ID
|
||||
})
|
||||
])
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
it.each(['empty', 'failed', 'rejected'] as const)(
|
||||
'keeps completed %s scan semantics after a warm scan',
|
||||
async (verdict) => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const { runtime, repo, successfulScan } = cachedScanFixture()
|
||||
runtime.scanGit.mockResolvedValueOnce(successfulScan)
|
||||
await runtime.scanRows(repo)
|
||||
await vi.advanceTimersByTimeAsync(31_000)
|
||||
if (verdict === 'rejected') {
|
||||
runtime.scanGit.mockRejectedValueOnce(new Error('offline'))
|
||||
} else {
|
||||
runtime.scanGit.mockResolvedValueOnce({ ok: verdict === 'empty', worktrees: [] })
|
||||
}
|
||||
|
||||
const result = await runtime.scanRows(repo)
|
||||
|
||||
expect(result.ok).toBe(verdict === 'empty')
|
||||
expect(result.rows).toEqual([])
|
||||
if (verdict === 'empty') {
|
||||
await vi.advanceTimersByTimeAsync(31_000)
|
||||
runtime.scanGit.mockImplementation(neverSettles)
|
||||
expect((await advancePastRepoScanBudget(runtime.scanRows(repo))).rows).toEqual([])
|
||||
}
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
it.each(['generation', 'host', 'runtime'] as const)(
|
||||
'does not reuse a successful scan from another %s',
|
||||
async (owner) => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const { runtime, repo, successfulScan } = cachedScanFixture(
|
||||
owner === 'runtime' ? null : SSH_CONNECTION_ID
|
||||
)
|
||||
// An explicit runtime snapshot can predate a settings change while Git is running.
|
||||
const runtimes = new Map<string, ProjectExecutionRuntimeResolution>(
|
||||
owner === 'runtime'
|
||||
? [
|
||||
[
|
||||
repo.id,
|
||||
{
|
||||
status: 'resolved',
|
||||
runtime: {
|
||||
kind: 'local-host',
|
||||
hostPlatform: 'darwin',
|
||||
projectId: 'old',
|
||||
reason: 'non-windows',
|
||||
cacheKey: 'old-runtime'
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
: []
|
||||
)
|
||||
runtime.scanGit.mockResolvedValueOnce(successfulScan).mockImplementation(neverSettles)
|
||||
await runtime.scanRows(repo, runtimes)
|
||||
await vi.advanceTimersByTimeAsync(31_000)
|
||||
if (owner === 'generation') {
|
||||
runtime.invalidateWorktreeScanCacheForRepo(repo.id)
|
||||
}
|
||||
const current =
|
||||
owner === 'host'
|
||||
? {
|
||||
...repo,
|
||||
connectionId: 'another-host',
|
||||
executionHostId: toSshExecutionHostId('another-host')
|
||||
}
|
||||
: repo
|
||||
|
||||
const result = await advancePastRepoScanBudget(runtime.scanRows(current))
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.rows).toHaveLength(owner === 'host' ? 0 : 1)
|
||||
expect(result.rows.every((row) => row.branch === '' && row.head === '')).toBe(true)
|
||||
if (owner !== 'host') {
|
||||
expect(result.rows[0]?.displayName).toBe('app')
|
||||
}
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
it('does not reuse failed cached local rows on timeout', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const { runtime, repo, successfulScan } = cachedScanFixture(null)
|
||||
runtime.scanGit
|
||||
.mockResolvedValueOnce({ ...successfulScan, ok: false })
|
||||
.mockImplementation(neverSettles)
|
||||
await runtime.scanRows(repo)
|
||||
await vi.advanceTimersByTimeAsync(31_000)
|
||||
|
||||
const result = await advancePastRepoScanBudget(runtime.scanRows(repo))
|
||||
|
||||
expect(result.rows[0]).toMatchObject({ branch: '', head: '', displayName: 'app' })
|
||||
expect(result.ok).toBe(false)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps a failed-removal checkout while the successful Git result awaits its listing overlay', async () => {
|
||||
vi.useFakeTimers()
|
||||
const checkoutExists = vi
|
||||
.spyOn(removalTable, 'worktreeCheckoutExists')
|
||||
.mockImplementation(neverSettles)
|
||||
removalTable.failedWorktreeRemovals.set(WORKTREE_ID, {
|
||||
worktreeId: WORKTREE_ID,
|
||||
repoId: REPO_ID,
|
||||
repoPath: REPO_PATH,
|
||||
worktreePath: WORKTREE_PATH,
|
||||
branch: 'feature',
|
||||
head: 'abc',
|
||||
deleteBranch: false,
|
||||
force: false,
|
||||
requestedAt: 1,
|
||||
failure: { message: 'checkout cleanup failed', failedAt: 2 }
|
||||
})
|
||||
try {
|
||||
const { runtime, repo } = cachedScanFixture(null)
|
||||
runtime.scanGit.mockResolvedValue({ ok: true, worktrees: [] })
|
||||
|
||||
const result = await advancePastRepoScanBudget(runtime.scanRows(repo))
|
||||
|
||||
expect(checkoutExists).toHaveBeenCalledWith(WORKTREE_PATH)
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.rows).toEqual([
|
||||
expect.objectContaining({ id: WORKTREE_ID, branch: '', head: '' })
|
||||
])
|
||||
} finally {
|
||||
removalTable.failedWorktreeRemovals.delete(WORKTREE_ID)
|
||||
checkoutExists.mockRestore()
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('worktree.ps on a degraded repo scan', () => {
|
||||
beforeEach(() => {
|
||||
listWorktreesStrictMock.mockReset()
|
||||
|
||||
Reference in New Issue
Block a user