Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/vs/platform/agentHost/common/sessionDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ export interface ISessionDatabase extends IDisposable {
*/
remapTurnIds(mapping: ReadonlyMap<string, string>): Promise<void>;

/**
* Creates a safe, consistent copy of the database at the given path
* using SQLite's `VACUUM INTO` command.
*/
vacuumInto(targetPath: string): Promise<void>;

/**
* Resolves once all in-flight write operations on this database have
* settled. Used by graceful shutdown to flush fire-and-forget writes
Expand Down
18 changes: 12 additions & 6 deletions src/vs/platform/agentHost/node/copilot/copilotAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,21 @@ export class CopilotAgent extends Disposable implements IAgent {
});
const newSessionId = forkResult.sessionId;

// Copy the source session's database file so the forked session
// inherits turn event IDs and file-edit snapshots.
const sourceDbDir = this._sessionDataService.getSessionDataDir(config.fork!.session);
// Copy the source session's database using VACUUM INTO so the
// forked session inherits turn event IDs and file-edit snapshots.
// VACUUM INTO is safe even while the source DB is open.
const targetDbDir = this._sessionDataService.getSessionDataDirById(newSessionId);
const sourceDbPath = URI.joinPath(sourceDbDir, SESSION_DB_FILENAME);
const targetDbPath = URI.joinPath(targetDbDir, SESSION_DB_FILENAME);
try {
await fs.mkdir(targetDbDir.fsPath, { recursive: true });
await fs.copyFile(sourceDbPath.fsPath, targetDbPath.fsPath);
const sourceDbRef = await this._sessionDataService.tryOpenDatabase(config.fork!.session);
if (sourceDbRef) {
try {
await fs.mkdir(targetDbDir.fsPath, { recursive: true });
await sourceDbRef.object.vacuumInto(targetDbPath.fsPath);
} finally {
sourceDbRef.dispose();
}
}
} catch (err) {
this._logService.warn(`[Copilot] Failed to copy session database for fork: ${err instanceof Error ? err.message : String(err)}`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/vs/platform/agentHost/node/sessionDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ export class SessionDatabase implements ISessionDatabase {
}
}

async vacuumInto(targetPath: string) {
const db = await this._ensureDb();
await dbRun(db, 'VACUUM INTO ?', [targetPath]);
}

/**
* Wrap a mutating operation's promise so {@link whenIdle} can await it.
* Invoke at the **outermost** layer of every public mutating method so
Expand Down
2 changes: 2 additions & 0 deletions src/vs/platform/agentHost/test/common/sessionTestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class TestSessionDatabase implements ISessionDatabase {

async close(): Promise<void> { }

async vacuumInto(_targetPath: string): Promise<void> { }

dispose(): void { }

async setTurnEventId(_turnId: string, _eventId: string): Promise<void> { }
Expand Down
35 changes: 35 additions & 0 deletions src/vs/platform/agentHost/test/node/sessionDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
*--------------------------------------------------------------------------------------------*/

import assert from 'assert';
import { tmpdir } from 'os';
import * as fs from 'fs/promises';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { SessionDatabase, runMigrations, sessionDatabaseMigrations, type ISessionDatabaseMigration } from '../../node/sessionDatabase.js';
import { FileEditKind } from '../../common/state/sessionState.js';
import type { Database } from '@vscode/sqlite3';
import { generateUuid } from '../../../../base/common/uuid.js';
import { join } from '../../../../base/common/path.js';

suite('SessionDatabase', () => {

Expand Down Expand Up @@ -489,4 +493,35 @@ suite('SessionDatabase', () => {
assert.ok(tables.includes('session_metadata'));
});
});

// ---- vacuumInto -----------------------------------------------------

suite('vacuumInto', () => {

let tmpDir: string;

setup(async () => {
tmpDir = await fs.mkdtemp(join(tmpdir(), 'session-db-test-' + generateUuid()));
});

teardown(async () => {
await Promise.all([db?.close(), db2?.close()]);
db = db2 = undefined;
await fs.rm(tmpDir, { recursive: true, force: true });
});

test('produces a copy with the same data', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.setTurnEventId('turn-1', 'evt-1');
await db.setMetadata('key', 'value');

const targetPath = join(tmpDir, 'copy.db');
await db.vacuumInto(targetPath);

db2 = disposables.add(await SessionDatabase.open(targetPath));
assert.strictEqual(await db2.getTurnEventId('turn-1'), 'evt-1');
assert.strictEqual(await db2.getMetadata('key'), 'value');
});
});
});
Loading