Reference implementation for the Phoenix Architecture. Work in progress.
aicoding.leaflet.pub/
ai
coding
crazy
1/**
2 * Bootstrap State Machine
3 *
4 * Manages system lifecycle:
5 * BOOTSTRAP_COLD → BOOTSTRAP_WARMING → STEADY_STATE
6 */
7
8import type { DRateStatus } from './models/classification.js';
9import { BootstrapState, DRateLevel } from './models/classification.js';
10
11export class BootstrapStateMachine {
12 private state: BootstrapState;
13 private warmPassComplete: boolean = false;
14
15 constructor(initialState: BootstrapState = BootstrapState.BOOTSTRAP_COLD) {
16 this.state = initialState;
17 }
18
19 getState(): BootstrapState {
20 return this.state;
21 }
22
23 /**
24 * Signal that the warm pass (canonicalization + warm hashing) is complete.
25 */
26 markWarmPassComplete(): void {
27 this.warmPassComplete = true;
28 if (this.state === BootstrapState.BOOTSTRAP_COLD) {
29 this.state = BootstrapState.BOOTSTRAP_WARMING;
30 }
31 }
32
33 /**
34 * Evaluate whether to transition to STEADY_STATE based on D-rate.
35 */
36 evaluateTransition(dRateStatus: DRateStatus): void {
37 if (this.state === BootstrapState.BOOTSTRAP_WARMING) {
38 // Need sufficient data and acceptable D-rate
39 if (
40 dRateStatus.total_count >= 10 &&
41 (dRateStatus.level === DRateLevel.TARGET || dRateStatus.level === DRateLevel.ACCEPTABLE)
42 ) {
43 this.state = BootstrapState.STEADY_STATE;
44 }
45 }
46 }
47
48 /**
49 * Check if D-rate alarms should be suppressed (during cold bootstrap).
50 */
51 shouldSuppressAlarms(): boolean {
52 return this.state === BootstrapState.BOOTSTRAP_COLD;
53 }
54
55 /**
56 * Check if severity should be downgraded (during warming).
57 */
58 shouldDowngradeSeverity(): boolean {
59 return this.state === BootstrapState.BOOTSTRAP_WARMING;
60 }
61
62 /**
63 * Serialize state for persistence.
64 */
65 toJSON(): { state: BootstrapState; warm_pass_complete: boolean } {
66 return {
67 state: this.state,
68 warm_pass_complete: this.warmPassComplete,
69 };
70 }
71
72 /**
73 * Restore from serialized state.
74 */
75 static fromJSON(data: { state: BootstrapState; warm_pass_complete: boolean }): BootstrapStateMachine {
76 const machine = new BootstrapStateMachine(data.state);
77 machine.warmPassComplete = data.warm_pass_complete;
78 return machine;
79 }
80}