File size: 3,458 Bytes
a8f4b72 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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<string, unknown>; projectId: string }): Promise<LogicPromotionResult> | LogicPromotionResult };
governance: { review(input: { action: 'frontier.promote'; frontierItem: Record<string, unknown>; projectId: string; requestedBy?: string }): Promise<GovernancePromotionResult> | 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<string, unknown> | 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;
}
}
}
|