perf(core): opt-in in-memory SQLite for *-store tests

Adds an opt-in `inMemory` flag to `Database`/`ArchiveDatabase` (and
`{ inMemoryDb }` to TaskStore, AgentStore, RoutineStore,
AutomationStore, PluginStore) that swaps the on-disk fusion.db /
archive.db for SQLite's `:memory:` connection. Production callers
never set the flag, so behavior is unchanged.

Test files for each store now flip the flag in `beforeEach`. The
handful of tests that exercise cross-instance persistence (open store
A, close, open store B on same dir, expect data) construct disk-backed
stores explicitly inside the test body, marked with a comment at each
site.

Wall-clock impact:
- core:      69.4s → 18.5s  (3.7× faster, 3038 tests)
- dashboard: 156.6s → 30.0s (5.2× faster — improvement ripples through
                              any test that constructs a TaskStore)

The refactor eliminates the per-test SQLite open + WAL fsync + tmp
dir cleanup loop that dominated setup cost: ~50ms/test → ~5ms/test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-26 18:45:14 -07:00
parent e2bc644fd5
commit 6e4797ff14
17 changed files with 179 additions and 34 deletions

View File

@@ -395,12 +395,24 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
/** Cached TodoStore instance */
private todoStore: TodoStore | null = null;
constructor(private rootDir: string, globalSettingsDir?: string) {
// Test-only: when true, both fusion.db and archive.db open as `:memory:`
// SQLite connections instead of disk-backed files. Production code never
// sets this; it's gated through an opt-in TaskStoreOptions field below.
// Tests that need cross-instance persistence (open store A, close,
// open store B on the same dir, expect data) must leave this false.
private readonly inMemoryDb: boolean;
constructor(
private rootDir: string,
globalSettingsDir?: string,
options?: { inMemoryDb?: boolean },
) {
super();
this.setMaxListeners(100);
this.fusionDir = join(rootDir, ".fusion");
this.tasksDir = join(this.fusionDir, "tasks");
this.configPath = join(this.fusionDir, "config.json");
this.inMemoryDb = options?.inMemoryDb === true;
const resolvedGlobalSettingsDir = globalSettingsDir
?? (process.env.VITEST === "true" ? join(rootDir, ".fusion-global-settings") : undefined);
this.globalSettingsStore = new GlobalSettingsStore(resolvedGlobalSettingsDir);
@@ -412,7 +424,7 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
*/
private get db(): Database {
if (!this._db) {
this._db = new Database(this.fusionDir);
this._db = new Database(this.fusionDir, { inMemory: this.inMemoryDb });
this._db.init();
// Auto-migrate legacy data if needed
if (detectLegacyData(this.fusionDir)) {
@@ -426,7 +438,7 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
private get archiveDb(): ArchiveDatabase {
if (!this._archiveDb) {
this._archiveDb = new ArchiveDatabase(this.fusionDir);
this._archiveDb = new ArchiveDatabase(this.fusionDir, { inMemory: this.inMemoryDb });
this._archiveDb.init();
this.migrateLegacyArchiveEntriesToArchiveDb();
}
@@ -438,7 +450,7 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
// Initialize SQLite database
if (!this._db) {
this._db = new Database(this.fusionDir);
this._db = new Database(this.fusionDir, { inMemory: this.inMemoryDb });
this._db.init();
}