diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.test.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.test.tsx new file mode 100644 index 00000000000..e6b273753bf --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.test.tsx @@ -0,0 +1,136 @@ +// @vitest-environment happy-dom +// The delete dialog never contacts an asleep Orca Cloud sandbox: no git-status read and no +// ignored-files scan (that would wake the VM, or wait on it, just to fill in a dialog). +import { renderHook } from '@testing-library/react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { Worktree } from '../../../../../../shared/worktree/types' +import type { Repo } from '../../../../../../shared/repo-types' +import type { CloudWorkspaceStatus } from '../../../../../../shared/hosted-sandbox/cloud-workspace-api' +import { seedCloudWorkspaceSnapshot } from '@/lib/orca-cloud/orca-cloud-workspace-store' +import { useDeleteWorktreeStatusHydration } from './use-delete-worktree-status-hydration' +import { useDeleteWorktreeAtRiskLocalFiles } from './use-delete-worktree-at-risk-local-files' +import { mayContactDeleteTargetHost } from './delete-target-host-contact' +import { + getRuntimeGitStatus, + getRuntimeWorktreeAtRiskLocalFiles +} from '@/runtime/source-control/git/runtime-git-status-client' + +const state = vi.hoisted(() => ({ + repos: [], + settings: null, + activeWorktreeId: null, + activeWorkspaceExecutionHostId: null, + gitStatusByWorktree: {} +})) +vi.mock('@/store', () => ({ + useAppStore: Object.assign((selector: (value: typeof state) => unknown) => selector(state), { + getState: () => state + }) +})) +vi.mock('@/runtime/source-control/git/runtime-git-status-client', () => ({ + getRuntimeGitStatus: vi.fn(async () => ({ entries: [] })), + getRuntimeWorktreeAtRiskLocalFiles: vi.fn(async () => ({ + count: 0, + samplePaths: [], + complete: true + })) +})) +vi.mock('@/runtime/rpc/targets/runtime-client-target', () => ({ + // Every host resolves, so only the cloud guard can stop a read. + ipcOrRuntimeTargetForHostId: (hostId: string) => ({ kind: 'environment', environmentId: hostId }) +})) +vi.mock('@/lib/workspace/ownership/connection-context', () => ({ getConnectionId: () => null })) + +const repoMap = new Map() + +function target(id: string): Worktree { + return { + id, + hostId: 'runtime:env-cloud', + repoId: 'repo', + path: `/workspace/${id}`, + displayName: id, + branch: id, + head: 'abc123', + isBare: false, + isMainWorktree: false, + comment: '', + linkedIssue: null, + linkedPR: null, + linkedLinearIssue: null, + isArchived: false, + isUnread: false, + isPinned: false, + sortOrder: 0, + lastActivityAt: 0 + } +} + +function cloudStatus(state: 'ready' | 'sleeping'): CloudWorkspaceStatus { + return { + state, + metrics: null, + uptimeMs: null, + usage: { totalMs: 0, centsPerHour: 0, costCents: 0 }, + archive: null, + checkedAt: 1 + } +} + +function seedCloud(id: string, state: 'ready' | 'sleeping'): void { + seedCloudWorkspaceSnapshot({ + ids: new Set([id]), + idsLoaded: true, + status: { [id]: { status: cloudStatus(state), loading: false, error: null, fetchedAt: 1 } } + }) +} + +/** Opens the delete dialog's two host reads for one target. */ +function openDialogReads(worktree: Worktree): void { + const targets = [worktree] + renderHook(() => { + useDeleteWorktreeStatusHydration({ + isOpen: true, + deleteTargets: targets, + visibleTargets: targets, + repoMap + }) + useDeleteWorktreeAtRiskLocalFiles({ isOpen: true, deleteTargets: targets, repoMap }) + }) +} + +beforeEach(() => { + vi.mocked(getRuntimeGitStatus).mockClear() + vi.mocked(getRuntimeWorktreeAtRiskLocalFiles).mockClear() +}) +afterEach(() => seedCloudWorkspaceSnapshot({})) + +describe('delete dialog host reads', () => { + it('an asleep cloud workspace: no sandbox RPC at all', () => { + seedCloud('wt-cloud', 'sleeping') + openDialogReads(target('wt-cloud')) + expect(getRuntimeGitStatus).not.toHaveBeenCalled() + expect(getRuntimeWorktreeAtRiskLocalFiles).not.toHaveBeenCalled() + }) + + it('an awake cloud workspace: git status for "N uncommitted changes", never the file scan', () => { + seedCloud('wt-cloud', 'ready') + openDialogReads(target('wt-cloud')) + expect(getRuntimeGitStatus).toHaveBeenCalledOnce() + expect(getRuntimeWorktreeAtRiskLocalFiles).not.toHaveBeenCalled() + }) + + it('any other workspace: both reads, as upstream intends', () => { + seedCloudWorkspaceSnapshot({ ids: new Set(), idsLoaded: true }) + openDialogReads(target('wt-local')) + expect(getRuntimeGitStatus).toHaveBeenCalledOnce() + expect(getRuntimeWorktreeAtRiskLocalFiles).toHaveBeenCalledOnce() + }) + + it('the guard itself', () => { + seedCloud('wt-cloud', 'sleeping') + expect(mayContactDeleteTargetHost({ id: 'wt-cloud' }, 'git-status')).toBe(false) + expect(mayContactDeleteTargetHost({ id: 'wt-cloud' }, 'at-risk-scan')).toBe(false) + expect(mayContactDeleteTargetHost({ id: 'wt-other' }, 'at-risk-scan')).toBe(true) + }) +}) diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.ts b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.ts new file mode 100644 index 00000000000..f7f147daaad --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/delete-target-host-contact.ts @@ -0,0 +1,22 @@ +// What the delete dialog may ask a target's host while it is open. An Orca Cloud workspace's host is +// its sandbox: any read there either wakes it or waits on a VM that is asleep, just to fill in a +// dialog. So a cloud target gets no ignored-files scan (its dialog shows no file list), and its git +// status is read only while it is awake (its dialog shows "N uncommitted changes" only then). +import { getCloudWorkspaceSnapshot } from '@/lib/orca-cloud/orca-cloud-workspace-store' +import type { Worktree } from '../../../../../../shared/worktree/types' + +export type DeleteTargetHostRead = 'git-status' | 'at-risk-scan' + +export function mayContactDeleteTargetHost( + target: Pick, + read: DeleteTargetHostRead +): boolean { + const snapshot = getCloudWorkspaceSnapshot() + if (!snapshot.ids.has(target.id)) { + return true + } + if (read === 'at-risk-scan') { + return false + } + return snapshot.status[target.id]?.status?.state === 'ready' +} diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-at-risk-local-files.ts b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-at-risk-local-files.ts index dffe9badd25..1206250a224 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-at-risk-local-files.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-at-risk-local-files.ts @@ -11,6 +11,7 @@ import { import { getWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' import { isFolderWorkspaceDelete } from './delete-worktree-dialog-copy' import { resolveDeleteTargetGitContext } from './use-delete-worktree-status-hydration' +import { mayContactDeleteTargetHost } from './delete-target-host-contact' /** Ignored files each delete target would lose, by host identity; hosts that can't tell stay absent. */ export function useDeleteWorktreeAtRiskLocalFiles({ @@ -30,7 +31,12 @@ export function useDeleteWorktreeAtRiskLocalFiles({ }>({ generation: '', files: new Map() }) const startScans = useEffectEvent((signal: AbortSignal) => { for (const target of deleteTargets) { - if (target.isMainWorktree || isFolderWorkspaceDelete(repoMap, target)) { + if ( + target.isMainWorktree || + isFolderWorkspaceDelete(repoMap, target) || + // An Orca Cloud sandbox is never contacted for this: its dialog shows no file list. + !mayContactDeleteTargetHost(target, 'at-risk-scan') + ) { continue } const gitContext = resolveDeleteTargetGitContext(target, repos) diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-status-hydration.ts b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-status-hydration.ts index 278f9e55013..16f339f958f 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-status-hydration.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-delete-worktree-status-hydration.ts @@ -12,6 +12,7 @@ import type { GitStatusResult } from '../../../../../../shared/git/status/git-st import { getWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' import { isFolderWorkspaceDelete } from './delete-worktree-dialog-copy' import { orderDeleteWorktreeStatusHydrationTargets } from './delete-worktree-dirty-change-counts' +import { mayContactDeleteTargetHost } from './delete-target-host-contact' /** Routes a delete target's git reads to its owning host; null when no host can serve it. */ export function resolveDeleteTargetGitContext( @@ -83,7 +84,10 @@ export function useDeleteWorktreeStatusHydration({ setStatusByIdentity((current) => new Map(current).set(identity, existingStatus)) continue } - const gitContext = resolveDeleteTargetGitContext(target, repos) + // An asleep Orca Cloud sandbox is not woken (or waited on) to count changes for a dialog. + const gitContext = mayContactDeleteTargetHost(target, 'git-status') + ? resolveDeleteTargetGitContext(target, repos) + : null if (!gitContext) { setStatusByIdentity((current) => new Map(current).set(identity, null)) continue