import type { GlobalEvent } from '@mavis/shared/global-events'; import type { AppDb } from './infra/db/client.js'; import { EventBus, type EventBusClient } from './infra/event-bus/index.js'; import { createForkWorktreeAdapter } from './infra/git/fork-worktree-adapter.js'; import { createProcessLocalRuntimeOwnerIdentity, createRuntimeOwnerIdentity, type RuntimeOwnerIdentity, } from './infra/runtime-owner/index.js'; import { Scheduler, type SchedulerClient, type SchedulerOptions } from './infra/scheduler/index.js'; import { pruneTransientStorage } from './infra/storage-retention/index.js'; interface BackgroundRuntimeClients { /** Scheduler is an Electron-owned capability; embedded CLI keeps only the event bus. */ readonly scheduler?: SchedulerClient; readonly eventBus: EventBusClient; readonly runtimeOwnerIdentity: RuntimeOwnerIdentity; readonly forkWorktree: ReturnType; } export interface CreateBackgroundRuntimeOptions { readonly db: AppDb; /** Production hosts persist an exact cross-process Runtime instance lease here. */ readonly dataDir?: string; readonly logger?: { info(fields: Record, message: string): void; warn(fields: Record, message: string): void; }; readonly metrics?: SchedulerOptions['metrics']; readonly nowMs?: () => number; readonly restorePersistedJobExecution?: boolean; /** Disable Electron-only scheduled work for embedded command-line owners. */ readonly enableScheduler?: boolean; } export interface BackgroundRuntime { /** Lifecycle-free capabilities borrowed by business services. */ readonly clients: BackgroundRuntimeClients; /** Starts all shared background infrastructure after services register handlers. */ start(): Promise; /** Registers a bounded best-effort task before post-ready maintenance starts. */ registerMaintenance(name: string, task: () => Promise): void; /** Starts registered retention only after the product Runtime is ready. */ startMaintenance(): void; /** Stops new background triggers before business services drain their accepted work. */ stop(): Promise; /** Releases every owned infrastructure resource after business services drain. */ close(): Promise; } export async function createBackgroundRuntime( options: CreateBackgroundRuntimeOptions, ): Promise { const runtimeOwnerIdentity = options.dataDir ? await createRuntimeOwnerIdentity({ dataDir: options.dataDir }) : createProcessLocalRuntimeOwnerIdentity(); let scheduler: Scheduler | undefined; let eventBus: EventBus; let forkWorktree: ReturnType; try { scheduler = options.enableScheduler === false ? undefined : new Scheduler({ db: options.db, ...(options.metrics ? { metrics: options.metrics } : {}), ...(options.nowMs ? { nowMs: options.nowMs } : {}), }); eventBus = new EventBus(); forkWorktree = createForkWorktreeAdapter({}); } catch (error) { await runtimeOwnerIdentity.close(); throw error; } let startPromise: Promise | undefined; let maintenancePromise: Promise | undefined; let stopPromise: Promise | undefined; let closePromise: Promise | undefined; const maintenanceTasks: Array<{ readonly name: string; readonly run: () => Promise }> = []; if (options.dataDir) { maintenanceTasks.push({ name: 'transient-storage-retention', run: () => prunePostReadyTransientStorage(options), }); } const close = (): Promise => { closePromise ??= closeBackgroundInfrastructure(); return closePromise; }; const stop = (): Promise => { stopPromise ??= stopBackgroundInfrastructure(); return stopPromise; }; const start = (): Promise => { startPromise ??= startInfrastructure(); return startPromise; }; const registerMaintenance = (name: string, task: () => Promise): void => { if (maintenancePromise) throw new Error('Background maintenance already started'); maintenanceTasks.push({ name, run: task }); }; const startMaintenance = (): void => { maintenancePromise ??= runDeferredMaintenance(); }; async function runDeferredMaintenance(): Promise { await deferMaintenance(); await runMaintenanceTasks(); } async function runMaintenanceTasks(): Promise { await Promise.all( maintenanceTasks.map(async ({ name, run }) => { try { await run(); } catch (error) { options.logger?.warn( { error, maintenance: name }, 'local-runtime-v2 background maintenance failed', ); } }), ); } async function stopBackgroundInfrastructure(): Promise { try { if (scheduler) scheduler.stop(); } finally { eventBus.close(); } } async function closeBackgroundInfrastructure(): Promise { let firstError: unknown; try { await stop(); } catch (error) { firstError = error; } try { await maintenancePromise; } catch (error) { firstError ??= error; } try { await runtimeOwnerIdentity.close(); } catch (error) { firstError ??= error; } if (firstError !== undefined) throw firstError; } async function startInfrastructure(): Promise { try { if (scheduler) { scheduler.start({ restorePersistedJobExecution: options.restorePersistedJobExecution !== false, }); } } catch (error) { try { await close(); } catch { // Startup cleanup is best effort; the Scheduler failure remains primary. } throw error; } } return { clients: { ...(scheduler ? { scheduler } : {}), eventBus, runtimeOwnerIdentity, forkWorktree, }, start, registerMaintenance, startMaintenance, stop, close, }; } function deferMaintenance(): Promise { return new Promise((resolve) => setImmediate(resolve)); } async function prunePostReadyTransientStorage( options: CreateBackgroundRuntimeOptions, ): Promise { if (!options.dataDir) return; const retention = await pruneTransientStorage({ dataDir: options.dataDir, ...(options.nowMs ? { nowMs: options.nowMs } : {}), }); if (retention.logFilesDeleted <= 0 && !retention.retiredContextDebugDeleted) { return; } options.logger?.info({ ...retention }, 'local-runtime-v2 transient storage retention completed'); }