Reference implementation for the Phoenix Architecture. Work in progress. aicoding.leaflet.pub/
ai coding crazy
at main 84 lines 1.9 kB view raw
1/** 2 * D-Rate Tracker 3 * 4 * Tracks the rate of D-class (uncertain) classifications 5 * over a rolling window. 6 */ 7 8import type { ChangeClassification, DRateStatus } from './models/classification.js'; 9import { ChangeClass, DRateLevel } from './models/classification.js'; 10 11const DEFAULT_WINDOW_SIZE = 100; 12 13export class DRateTracker { 14 private window: ChangeClass[] = []; 15 private windowSize: number; 16 17 constructor(windowSize: number = DEFAULT_WINDOW_SIZE) { 18 this.windowSize = windowSize; 19 } 20 21 /** 22 * Record a batch of classifications. 23 */ 24 record(classifications: ChangeClassification[]): void { 25 for (const c of classifications) { 26 this.window.push(c.change_class); 27 if (this.window.length > this.windowSize) { 28 this.window.shift(); 29 } 30 } 31 } 32 33 /** 34 * Record a single classification. 35 */ 36 recordOne(changeClass: ChangeClass): void { 37 this.window.push(changeClass); 38 if (this.window.length > this.windowSize) { 39 this.window.shift(); 40 } 41 } 42 43 /** 44 * Get current D-rate status. 45 */ 46 getStatus(): DRateStatus { 47 const total = this.window.length; 48 if (total === 0) { 49 return { 50 rate: 0, 51 level: DRateLevel.TARGET, 52 window_size: this.windowSize, 53 d_count: 0, 54 total_count: 0, 55 }; 56 } 57 58 const dCount = this.window.filter(c => c === ChangeClass.D).length; 59 const rate = dCount / total; 60 const level = rateToLevel(rate); 61 62 return { 63 rate, 64 level, 65 window_size: this.windowSize, 66 d_count: dCount, 67 total_count: total, 68 }; 69 } 70 71 /** 72 * Reset the tracker. 73 */ 74 reset(): void { 75 this.window = []; 76 } 77} 78 79function rateToLevel(rate: number): DRateLevel { 80 if (rate <= 0.05) return DRateLevel.TARGET; 81 if (rate <= 0.10) return DRateLevel.ACCEPTABLE; 82 if (rate <= 0.15) return DRateLevel.WARNING; 83 return DRateLevel.ALARM; 84}