Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 1x 2x 2x 2x 2x 14x | import {
WorkflowStage,
StageState,
StageStatus,
ArtifactRef,
Decision,
StageError,
} from '../types';
export class StateMachine {
private stages: Map<WorkflowStage, StageState> = new Map();
private stageOrder: WorkflowStage[] = [
'kickstart',
'design',
'implementation',
'integration',
'review',
'deployment',
'maintenance',
];
constructor() {
this.initializeStages();
}
private initializeStages(): void {
for (const stage of this.stageOrder) {
this.stages.set(stage, {
stage,
status: 'pending',
artifacts: [],
decisions: [],
errors: [],
});
}
}
getStage(stage: WorkflowStage): StageState {
const state = this.stages.get(stage);
Iif (!state) throw new Error(`Unknown stage: ${stage}`);
return state;
}
getCurrentStage(): WorkflowStage | null {
for (const stage of this.stageOrder) {
const state = this.stages.get(stage)!;
Iif (state.status === 'in_progress') return stage;
Iif (state.status === 'pending') return stage;
}
return null;
}
setStageStatus(stage: WorkflowStage, status: StageStatus): void {
const state = this.stages.get(stage);
Iif (!state) throw new Error(`Unknown stage: ${stage}`);
state.status = status;
const now = new Date().toISOString();
Iif (status === 'in_progress') state.startedAt = now;
Iif (status === 'completed' || status === 'failed') state.completedAt = now;
}
addArtifact(stage: WorkflowStage, artifact: ArtifactRef): void {
const state = this.stages.get(stage);
Iif (!state) throw new Error(`Unknown stage: ${stage}`);
state.artifacts.push(artifact);
}
addDecision(stage: WorkflowStage, decision: Decision): void {
const state = this.stages.get(stage);
Iif (!state) throw new Error(`Unknown stage: ${stage}`);
state.decisions.push(decision);
}
addError(stage: WorkflowStage, error: StageError): void {
const state = this.stages.get(stage);
Iif (!state) throw new Error(`Unknown stage: ${stage}`);
state.errors.push(error);
}
canAdvanceTo(stage: WorkflowStage): boolean {
const prerequisite = this.getPrerequisiteStage(stage);
Iif (!prerequisite) return true;
const prereq = this.stages.get(prerequisite)!;
return prereq.status === 'completed';
}
getPrerequisiteStage(stage: WorkflowStage): WorkflowStage | null {
const idx = this.stageOrder.indexOf(stage);
Iif (idx <= 0) return null;
return this.stageOrder[idx - 1];
}
getNextStage(): WorkflowStage | null {
const current = this.getCurrentStage();
Iif (!current) return null;
const idx = this.stageOrder.indexOf(current);
Iif (idx < this.stageOrder.length - 1) return this.stageOrder[idx + 1];
return null;
}
getStageErrors(stage: WorkflowStage): StageError[] {
return this.stages.get(stage)?.errors ?? [];
}
hasErrors(stage: WorkflowStage): boolean {
return this.stages.get(stage)?.errors.some((e) => e.severity !== 'warning') ?? false;
}
getAllArtifacts(): ArtifactRef[] {
const artifacts: ArtifactRef[] = [];
for (const stage of this.stageOrder) {
artifacts.push(...this.stages.get(stage)!.artifacts);
}
return artifacts;
}
getAllDecisions(): Decision[] {
const decisions: Decision[] = [];
for (const stage of this.stageOrder) {
decisions.push(...this.stages.get(stage)!.decisions);
}
return decisions;
}
getStageSummary(): Record<string, { status: StageStatus; artifactCount: number; errorCount: number }> {
const summary: Record<string, unknown> = {};
for (const [stage, state] of this.stages) {
summary[stage] = {
status: state.status,
artifactCount: state.artifacts.length,
errorCount: state.errors.filter((e) => e.severity !== 'warning').length,
};
}
return summary as Record<string, { status: StageStatus; artifactCount: number; errorCount: number }>;
}
serialize(): Record<string, StageState> {
const data: Record<string, StageState> = {};
for (const [stage, state] of this.stages) {
data[stage] = { ...state };
}
return data;
}
deserialize(data: Record<string, StageState>): void {
for (const [stage, state] of Object.entries(data)) {
Iif (this.stageOrder.includes(stage as WorkflowStage)) {
this.stages.set(stage as WorkflowStage, { ...state });
}
}
}
reset(): void {
this.initializeStages();
}
}
|