diff --git a/src/main/ipc/remote-workspace-cache.test.ts b/src/main/ipc/remote-workspace-cache.test.ts new file mode 100644 index 000000000..3965ba9eb --- /dev/null +++ b/src/main/ipc/remote-workspace-cache.test.ts @@ -0,0 +1,74 @@ +import { beforeEach, describe, expect, it } from 'vitest' +import type { + RemoteWorkspaceSession, + RemoteWorkspaceSnapshot +} from '../../shared/remote-workspace-types' +import { + REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES, + _getRemoteWorkspaceCacheSizesForTests, + _getRemoteWorkspaceSnapshotForTests, + _rememberRemoteWorkspaceSnapshotForTests, + _resetRemoteWorkspaceCachesForTests +} from './remote-workspace' + +function emptyRemoteWorkspaceSession(): RemoteWorkspaceSession { + return { + activeWorktreePath: null, + activeTabId: null, + tabsByWorktreePath: {}, + terminalLayoutsByTabId: {} + } +} + +function snapshot(session: RemoteWorkspaceSession, revision = 7): RemoteWorkspaceSnapshot { + return { + namespace: 'target', + revision, + updatedAt: 123, + schemaVersion: 1, + session + } +} + +describe('remote workspace snapshot cache', () => { + beforeEach(() => { + _resetRemoteWorkspaceCachesForTests() + }) + + it('LRU-evicts old target snapshots', () => { + for (let i = 0; i <= REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES; i++) { + _rememberRemoteWorkspaceSnapshotForTests( + `target-${i}`, + snapshot({ + activeWorktreePath: `/repo-${i}`, + activeTabId: null, + tabsByWorktreePath: {}, + terminalLayoutsByTabId: {} + }) + ) + } + + expect(_getRemoteWorkspaceCacheSizesForTests().snapshots).toBe( + REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES + ) + expect(_getRemoteWorkspaceSnapshotForTests('target-0')).toBeUndefined() + expect(_getRemoteWorkspaceSnapshotForTests('target-1')?.session.activeWorktreePath).toBe( + '/repo-1' + ) + }) + + it('refreshes snapshot recency on cache reads', () => { + for (let i = 0; i < REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES; i++) { + _rememberRemoteWorkspaceSnapshotForTests( + `target-${i}`, + snapshot(emptyRemoteWorkspaceSession()) + ) + } + + expect(_getRemoteWorkspaceSnapshotForTests('target-0')).toBeDefined() + _rememberRemoteWorkspaceSnapshotForTests('target-new', snapshot(emptyRemoteWorkspaceSession())) + + expect(_getRemoteWorkspaceSnapshotForTests('target-0')).toBeDefined() + expect(_getRemoteWorkspaceSnapshotForTests('target-1')).toBeUndefined() + }) +}) diff --git a/src/main/ipc/remote-workspace.ts b/src/main/ipc/remote-workspace.ts index 119dad066..cd7318db7 100644 --- a/src/main/ipc/remote-workspace.ts +++ b/src/main/ipc/remote-workspace.ts @@ -25,12 +25,71 @@ import { registerRemoteWorkspaceNotificationHandler } from './remote-workspace-e const CLIENT_ID = randomUUID() const CLIENT_NAME = hostname() || 'This device' const SNAPSHOT_SCHEMA_VERSION = 1 +export const REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES = 64 let mainWindowGetter: (() => BrowserWindow | null) | null = null const latestSnapshotByTargetId = new Map() const remoteWorkspacePatchTailByTargetId = new Map>() let unregisterRemoteWorkspaceNotifications: (() => void) | null = null +function rememberRemoteWorkspaceSnapshot( + targetId: string, + snapshot: RemoteWorkspaceSnapshot +): void { + if (latestSnapshotByTargetId.has(targetId)) { + latestSnapshotByTargetId.delete(targetId) + } + latestSnapshotByTargetId.set(targetId, snapshot) + while (latestSnapshotByTargetId.size > REMOTE_WORKSPACE_SNAPSHOT_CACHE_MAX_ENTRIES) { + const oldest = latestSnapshotByTargetId.keys().next() + if (oldest.done) { + break + } + latestSnapshotByTargetId.delete(oldest.value) + } +} + +function getCachedRemoteWorkspaceSnapshot(targetId: string): RemoteWorkspaceSnapshot | undefined { + const snapshot = latestSnapshotByTargetId.get(targetId) + if (!snapshot) { + return undefined + } + // Why: remote workspace snapshots can contain the whole tab/layout session + // for a target. Touch cache hits so deleted or rarely used targets age out. + rememberRemoteWorkspaceSnapshot(targetId, snapshot) + return snapshot +} + +export function _resetRemoteWorkspaceCachesForTests(): void { + latestSnapshotByTargetId.clear() + remoteWorkspacePatchTailByTargetId.clear() +} + +export function _getRemoteWorkspaceCacheSizesForTests(): { + snapshots: number + patchTails: number +} { + return { + snapshots: latestSnapshotByTargetId.size, + patchTails: remoteWorkspacePatchTailByTargetId.size + } +} + +/** @internal - exposed for cache-bound tests only. */ +export function _rememberRemoteWorkspaceSnapshotForTests( + targetId: string, + snapshot: RemoteWorkspaceSnapshot +): void { + rememberRemoteWorkspaceSnapshot(targetId, snapshot) +} + +/** @internal - exposed for cache-bound tests only. */ +export function _getRemoteWorkspaceSnapshotForTests( + targetId: string +): RemoteWorkspaceSnapshot | undefined { + return getCachedRemoteWorkspaceSnapshot(targetId) +} + function emptyRemoteSession(): RemoteWorkspaceSession { return { activeWorktreePath: null, @@ -211,7 +270,7 @@ async function getRemoteSnapshot(target: SshTarget): Promise