Add Effect persistence runtime modules and wire service bootstrap

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 10:11:39 +00:00
1 parent 40d49f1035
commit 2cf729e36c
10 files changed
+80 -21

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
import path from "node:path";
export interface PersistenceConfig {
dbPath: string;
legacyProjectsJsonPath?: string;
}
export function resolvePersistenceConfig(input: PersistenceConfig): PersistenceConfig {
const resolved: PersistenceConfig = {
dbPath: path.resolve(input.dbPath),
};
if (input.legacyProjectsJsonPath) {
resolved.legacyProjectsJsonPath = path.resolve(input.legacyProjectsJsonPath);
}
return resolved;
}
+6
View File
@@ -0,0 +1,6 @@
export class PersistenceInitializationError extends Error {
constructor(message: string, options?: { cause?: unknown }) {
super(message, options);
this.name = "PersistenceInitializationError";
}
}
+6
View File
@@ -0,0 +1,6 @@
import type { SqliteDatabase } from "../sqliteAdapter";
import { runStateMigrations } from "../stateMigrations";
export function runPersistenceMigrations(db: SqliteDatabase): void {
runStateMigrations(db);
}
@@ -0,0 +1 @@
export * from "../documentsRepo";
@@ -0,0 +1 @@
export * from "../metadataRepo";
@@ -0,0 +1 @@
export * from "../providerEventsRepo";
@@ -0,0 +1 @@
export * from "../stateEventsRepo";
+19
View File
@@ -0,0 +1,19 @@
import type * as Effect from "effect/Effect";
import type * as SqlClient from "effect/unstable/sql/SqlClient";
import type { EffectSqliteDatabaseAdapter, SqliteDatabase } from "../sqliteAdapter";
function isEffectSqliteDatabase(db: SqliteDatabase): db is EffectSqliteDatabaseAdapter {
return "runWithSqlClient" in db && typeof db.runWithSqlClient === "function";
}
export function runWithSqlClient<A>(
db: SqliteDatabase,
effect: Effect.Effect<A, unknown, SqlClient.SqlClient>,
fallback: () => A,
): A {
if (isEffectSqliteDatabase(db)) {
return db.runWithSqlClient(effect);
}
return fallback();
}
@@ -0,0 +1,10 @@
import fs from "node:fs";
import path from "node:path";
import { openSqliteDatabase, type SqliteDatabase } from "../sqliteAdapter";
export function openPersistenceSqliteDatabase(dbPath: string): SqliteDatabase {
const resolvedPath = path.resolve(dbPath);
fs.mkdirSync(path.dirname(resolvedPath), { recursive: true });
return openSqliteDatabase(resolvedPath);
}
+19 -21
View File
@@ -77,23 +77,23 @@ import {
readNextSortKey as readNextSortKeyEffect,
upsertDocument as upsertDocumentEffect,
type DocumentRow,
} from "./persistence/documentsRepo";
} from "./persistence/repos/documentsRepo";
import {
insertProviderEvent as insertProviderEventEffect,
listCompletedItemEventsBySessionTurn as listCompletedItemEventsBySessionTurnEffect,
listCompletedItemEventsByThreadTurn as listCompletedItemEventsByThreadTurnEffect,
listCompletedItemEventsByTurn as listCompletedItemEventsByTurnEffect,
type CompletedProviderItemRow,
} from "./persistence/providerEventsRepo";
} from "./persistence/repos/providerEventsRepo";
import {
appendStateEvent as appendStateEventEffect,
listStateEventsAfterSeq as listStateEventsAfterSeqEffect,
readLastStateSeq as readLastStateSeqEffect,
} from "./persistence/stateEventsRepo";
} from "./persistence/repos/stateEventsRepo";
import {
readMetadataValue as readMetadataValueEffect,
writeMetadataValue as writeMetadataValueEffect,
} from "./persistence/metadataRepo";
} from "./persistence/repos/metadataRepo";
import {
buildUpdatedAppSettings,
resolveAppSettings,
@@ -119,8 +119,12 @@ import {
} from "./persistence/domain/stateSync";
import { fallbackGroupId, normalizeTerminalIds, normalizeThread } from "./persistence/domain/threads";
import { mergeTurnSummaryFiles, summarizeUnifiedDiff } from "./persistence/domain/turnSummaries";
import { openSqliteDatabase, type EffectSqliteDatabaseAdapter, type SqliteDatabase } from "./sqliteAdapter";
import { runStateMigrations } from "./stateMigrations";
import { resolvePersistenceConfig } from "./persistence/config";
import { PersistenceInitializationError } from "./persistence/errors";
import { runPersistenceMigrations } from "./persistence/migrator";
import { runWithSqlClient } from "./persistence/runtime";
import { openPersistenceSqliteDatabase } from "./persistence/sqliteLayer";
import type { SqliteDatabase } from "./sqliteAdapter";
const METADATA_KEY_PROJECTS_JSON_IMPORTED = "migration.projects_json_imported";
const METADATA_KEY_APP_SETTINGS = "app.settings.v1";
@@ -197,21 +201,22 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
constructor(options: PersistenceServiceOptions) {
super();
const dbPath = path.resolve(options.dbPath);
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
this.db = openSqliteDatabase(dbPath);
const config = resolvePersistenceConfig(options);
this.db = openPersistenceSqliteDatabase(config.dbPath);
try {
runStateMigrations(this.db);
runPersistenceMigrations(this.db);
} catch (error) {
try {
this.db.close();
} catch {
// Best effort close on failed initialization.
}
throw error;
throw new PersistenceInitializationError("Failed to initialize persistence database", {
cause: error,
});
}
if (options.legacyProjectsJsonPath) {
this.importProjectsJsonIfNeeded(options.legacyProjectsJsonPath);
if (config.legacyProjectsJsonPath) {
this.importProjectsJsonIfNeeded(config.legacyProjectsJsonPath);
}
}
@@ -1716,13 +1721,6 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
effect: Effect.Effect<A, unknown, SqlClient.SqlClient>,
fallback: () => A,
): A {
if (isEffectSqliteDatabase(this.db)) {
return this.db.runWithSqlClient(effect);
}
return fallback();
return runWithSqlClient(this.db, effect, fallback);
}
}
function isEffectSqliteDatabase(db: SqliteDatabase): db is EffectSqliteDatabaseAdapter {
return "runWithSqlClient" in db && typeof db.runWithSqlClient === "function";
}