diff --git a/backend/src/services/opencode-import.ts b/backend/src/services/opencode-import.ts index 2cb7e44d..4231dd9e 100644 --- a/backend/src/services/opencode-import.ts +++ b/backend/src/services/opencode-import.ts @@ -23,6 +23,18 @@ import type { SettingsService } from './settings' const OPENCODE_STATE_DB_FILENAMES = new Set(['opencode.db', 'opencode.db-shm', 'opencode.db-wal']) +export const OPENCODE_STATE_IMPORT_EXCLUDED_ENTRIES: ReadonlySet = new Set([ + 'worktree', + 'snapshot', + 'repos', + 'bin', + 'log', + 'tool-output', + 'shell', +]) + +const OPENCODE_STATE_IMPORT_EXCLUDED_SUFFIXES = ['.log'] + export interface OpenCodeImportStatus { configSourcePath: string | null configSourcePaths: string[] @@ -93,7 +105,29 @@ function escapeSqliteValue(value: string): string { return value.replace(/'/g, "''") } -async function copyOpenCodeStateFiles(sourcePath: string, targetPath: string): Promise { +function getTopLevelEntryContainingPath(sourcePath: string, targetPath: string): string | null { + const relativeTargetPath = path.relative(path.resolve(sourcePath), path.resolve(targetPath)) + + if (relativeTargetPath === '' || relativeTargetPath.startsWith('..') || path.isAbsolute(relativeTargetPath)) { + return null + } + + return relativeTargetPath.split(path.sep)[0] ?? null +} + +export function shouldImportOpenCodeStateEntry(entryName: string, sourcePath: string, targetPath: string): boolean { + if (OPENCODE_STATE_IMPORT_EXCLUDED_ENTRIES.has(entryName)) { + return false + } + + if (OPENCODE_STATE_IMPORT_EXCLUDED_SUFFIXES.some(suffix => entryName.endsWith(suffix))) { + return false + } + + return entryName !== getTopLevelEntryContainingPath(sourcePath, targetPath) +} + +async function copyOpenCodeStateFiles(sourcePath: string, targetPath: string, destinationPath: string): Promise { const entries = await readdir(sourcePath, { withFileTypes: true }) for (const entry of entries) { @@ -101,6 +135,10 @@ async function copyOpenCodeStateFiles(sourcePath: string, targetPath: string): P continue } + if (!shouldImportOpenCodeStateEntry(entry.name, sourcePath, destinationPath)) { + continue + } + await cp(path.join(sourcePath, entry.name), path.join(targetPath, entry.name), { recursive: true, force: true, @@ -139,7 +177,7 @@ export async function importOpenCodeStateDirectory(sourcePath: string, targetPat const stagedTargetPath = await mkdtemp(path.join(targetParentPath, `${targetDirectoryName}-import-`)) try { - await copyOpenCodeStateFiles(resolvedSourcePath, stagedTargetPath) + await copyOpenCodeStateFiles(resolvedSourcePath, stagedTargetPath, resolvedTargetPath) snapshotOpenCodeDatabase(sourceDbPath, path.join(stagedTargetPath, 'opencode.db')) await rm(resolvedTargetPath, { recursive: true, force: true }) diff --git a/backend/test/services/opencode-import.test.ts b/backend/test/services/opencode-import.test.ts index a36e9ff8..756d805d 100644 --- a/backend/test/services/opencode-import.test.ts +++ b/backend/test/services/opencode-import.test.ts @@ -455,6 +455,72 @@ describe('opencode-import service', () => { expect(mockRename).toHaveBeenCalledWith(stagedPath, targetPath) }) + it('imports only durable host state entries and keeps nested directories named like excluded caches', async () => { + const { importOpenCodeStateDirectory } = await import('../../src/services/opencode-import') + const actualFs = await vi.importActual('fs/promises') + const rootPath = await actualFs.mkdtemp(path.join(os.tmpdir(), 'opencode-import-test-')) + const sourcePath = path.join(rootPath, 'source') + const targetPath = path.join(rootPath, 'workspace', '.opencode', 'state', 'opencode') + + const writeSourceFile = async (relativePath: string) => { + const filePath = path.join(sourcePath, relativePath) + await actualFs.mkdir(path.dirname(filePath), { recursive: true }) + await actualFs.writeFile(filePath, relativePath) + } + const mockRm = vi.mocked(rm) + + try { + await actualFs.mkdir(path.dirname(targetPath), { recursive: true }) + for (const relativePath of [ + 'opencode.db', + 'auth.json', + 'storage/x', + 'storage/log/y', + 'project/project.json', + 'plans/plan.json', + 'forge/forge.json', + 'forge.db', + 'worktree/checkout/file', + 'snapshot/snapshot', + 'repos/repo', + 'bin/binary', + 'log/server', + 'tool-output/output', + 'shell/command', + 'debug.log', + ]) { + await writeSourceFile(relativePath) + } + + mockFileExists.mockImplementation(async (candidate: string) => candidate === path.join(sourcePath, 'opencode.db')) + mockEnsureDirectoryExists.mockResolvedValue(undefined) + mockReaddir.mockImplementation(actualFs.readdir) + mockMkdtemp.mockImplementation(actualFs.mkdtemp) + mockRename.mockImplementation(actualFs.rename) + mockRm.mockImplementation(actualFs.rm) + vi.mocked(cp).mockImplementation(actualFs.cp) + + const result = await importOpenCodeStateDirectory(sourcePath, targetPath) + + expect(result).toBe(true) + expect((await actualFs.readdir(targetPath)).sort()).toEqual( + ['auth.json', 'forge', 'forge.db', 'plans', 'project', 'storage'].sort() + ) + expect(await actualFs.readFile(path.join(targetPath, 'storage', 'log', 'y'), 'utf8')).toBe('storage/log/y') + expect(MockSQLiteDatabase).toHaveBeenCalledWith(path.join(sourcePath, 'opencode.db')) + } finally { + await actualFs.rm(rootPath, { recursive: true, force: true }) + } + }) + + it('skips the top-level entry that contains the import destination', async () => { + const { shouldImportOpenCodeStateEntry } = await import('../../src/services/opencode-import') + + expect(shouldImportOpenCodeStateEntry('.opencode', '/host/opencode', '/host/opencode/.opencode/state/opencode')).toBe(false) + expect(shouldImportOpenCodeStateEntry('storage', '/host/opencode', '/host/opencode/.opencode/state/opencode')).toBe(true) + expect(shouldImportOpenCodeStateEntry('storage', '/host/opencode', '/elsewhere/.opencode/state/opencode')).toBe(true) + }) + it('cleans up the staged import directory when snapshotting fails', async () => { const { importOpenCodeStateDirectory } = await import('../../src/services/opencode-import')