import { FrontierRepository, SnapshotRepository } from '@projectos/storage-fabric/sqlite/repositories'; import type { DatabaseSync } from 'node:sqlite'; export type LogicPromotionResult = boolean | { valid: boolean; issues?: string[] }; export type GovernancePromotionResult = boolean | { approved: boolean; decision?: string; reason?: string; approvalId?: string; reviewId?: string }; export type PromotionDependencies = { logic: { validatePromotion(input: { frontierItem: Record; projectId: string }): Promise | LogicPromotionResult }; governance: { review(input: { action: 'frontier.promote'; frontierItem: Record; projectId: string; requestedBy?: string }): Promise | GovernancePromotionResult }; }; export class PromotionService { private readonly frontier: FrontierRepository; private readonly snapshots: SnapshotRepository; constructor(private readonly db: DatabaseSync, private readonly dependencies: PromotionDependencies) { this.frontier = new FrontierRepository(db); this.snapshots = new SnapshotRepository(db); } async promote(frontierId: string, options: { requestedBy?: string; changeSummary?: string } = {}) { const frontierItem = this.frontier.get(frontierId) as unknown as Record | null; if (!frontierItem) throw new Error(`Frontier item "${frontierId}" does not exist.`); const projectId = String(frontierItem.projectId ?? ''); if (!projectId) throw new Error('Frontier promotion requires a project-owned item.'); const logic = await this.dependencies.logic.validatePromotion({ frontierItem, projectId }); const logicValid = typeof logic === 'boolean' ? logic : logic.valid; if (!logicValid) { const issues = typeof logic === 'boolean' ? [] : logic.issues ?? []; throw new Error(`Logic validation rejected promotion${issues.length ? `: ${issues.join('; ')}` : '.'}`); } const governance = await this.dependencies.governance.review({ action: 'frontier.promote', frontierItem, projectId, requestedBy: options.requestedBy }); const approved = typeof governance === 'boolean' ? governance : governance.approved; if (!approved) { const reason = typeof governance === 'boolean' ? '' : governance.reason ?? governance.decision ?? ''; const reviewId = typeof governance === 'boolean' ? '' : governance.reviewId ?? ''; throw new Error(`Governance did not approve promotion${reason ? `: ${reason}` : '.'}${reviewId ? ` Review ID: ${reviewId}.` : ''}`); } this.db.exec('BEGIN IMMEDIATE'); try { const changeSummary = options.changeSummary ?? 'Validated and approved frontier promotion'; const canonEntry = this.frontier.promote(frontierId, { changeSummary, createdBy: options.requestedBy, withinTransaction: true }); const snapshot = this.snapshots.create(projectId, { type: 'canon', label: `After canon promotion: ${String(frontierItem.title)}`, createdBy: options.requestedBy, metadata: { sourceFrontierId: frontierId, governanceApprovalId: typeof governance === 'boolean' ? null : governance.approvalId ?? null, changeSummary }, withinTransaction: true }); this.db.exec('COMMIT'); return { frontierId, canonEntry, snapshot }; } catch (error) { this.db.exec('ROLLBACK'); throw error; } } }