File size: 9,706 Bytes
f568a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**

 * Decision Explainer Engine

 * Traces WHY each HOLD/BUY/SELL decision was made with human-readable reasoning

 */

import { unifiedBus } from './unifiedBus';

export type DecisionAction = 'BUY' | 'SELL' | 'HOLD';

export interface DecisionFactor {
  name: string;
  value: number;
  threshold: number;
  passed: boolean;
  weight: number;
  contribution: string;
}

export interface DecisionExplanation {
  id: string;
  timestamp: number;
  action: DecisionAction;
  confidence: number;
  factors: DecisionFactor[];
  reasoning: string[];
  summary: string;
  inputFrequency: number;
  outputFrequency: number;
  prismLevel: number;
  waveState: string;
  symbol: string;
}

export interface DecisionMetrics {
  coherence: number;
  lambda: number;
  lighthouseL: number;
  qgitaTier: number;
  qgitaConfidence: number;
  prismFrequency: number;
  prismLevel: number;
  waveState: string;
  harmonicLock: boolean;
  probabilityFused: number;
  sentimentScore: number;
  geometricAlignment: number;
}

class DecisionExplainerClass {
  private explanations: DecisionExplanation[] = [];
  private readonly maxHistory = 100;
  private listeners: Set<(explanation: DecisionExplanation) => void> = new Set();

  explain(metrics: DecisionMetrics, symbol: string = 'BTCUSDT'): DecisionExplanation {
    const factors: DecisionFactor[] = [];
    const reasoning: string[] = [];

    // Factor 1: Coherence Check
    const coherenceThreshold = 0.70;
    const coherencePassed = metrics.coherence >= coherenceThreshold;
    factors.push({
      name: 'Coherence (Ξ“)',
      value: metrics.coherence,
      threshold: coherenceThreshold,
      passed: coherencePassed,
      weight: 0.25,
      contribution: coherencePassed ? 'SUPPORTS trade' : 'BLOCKS trade'
    });
    reasoning.push(
      coherencePassed 
        ? `βœ“ Coherence ${(metrics.coherence * 100).toFixed(1)}% exceeds ${coherenceThreshold * 100}% threshold`
        : `βœ— Coherence ${(metrics.coherence * 100).toFixed(1)}% below ${coherenceThreshold * 100}% threshold`
    );

    // Factor 2: Lighthouse Signal
    const lighthouseThreshold = 0.5;
    const lighthousePassed = metrics.lighthouseL >= lighthouseThreshold;
    factors.push({
      name: 'Lighthouse (L)',
      value: metrics.lighthouseL,
      threshold: lighthouseThreshold,
      passed: lighthousePassed,
      weight: 0.20,
      contribution: lighthousePassed ? 'SUPPORTS trade' : 'NEUTRAL'
    });
    reasoning.push(
      lighthousePassed
        ? `βœ“ Lighthouse signal ${metrics.lighthouseL.toFixed(3)} indicates opportunity`
        : `β—‹ Lighthouse signal ${metrics.lighthouseL.toFixed(3)} is neutral`
    );

    // Factor 3: QGITA Tier
    const qgitaTierThreshold = 2;
    const qgitaPassed = metrics.qgitaTier <= qgitaTierThreshold && metrics.qgitaTier > 0;
    factors.push({
      name: 'QGITA Tier',
      value: metrics.qgitaTier,
      threshold: qgitaTierThreshold,
      passed: qgitaPassed,
      weight: 0.20,
      contribution: qgitaPassed ? `Tier ${metrics.qgitaTier} SUPPORTS` : 'LOW confidence'
    });
    reasoning.push(
      qgitaPassed
        ? `βœ“ QGITA Tier ${metrics.qgitaTier} with ${(metrics.qgitaConfidence * 100).toFixed(0)}% confidence`
        : `βœ— QGITA Tier ${metrics.qgitaTier} insufficient (need Tier 1-2)`
    );

    // Factor 4: Prism Frequency Lock
    const prismLockThreshold = 500; // Near 528 Hz love frequency
    const prismLocked = metrics.prismFrequency >= prismLockThreshold && metrics.prismLevel >= 4;
    factors.push({
      name: 'Prism Lock',
      value: metrics.prismFrequency,
      threshold: prismLockThreshold,
      passed: prismLocked,
      weight: 0.15,
      contribution: prismLocked ? '528Hz ALIGNED' : 'FORMING'
    });
    reasoning.push(
      prismLocked
        ? `βœ“ Prism at ${metrics.prismFrequency.toFixed(0)}Hz (Level ${metrics.prismLevel}) - 528Hz lock`
        : `β—‹ Prism at ${metrics.prismFrequency.toFixed(0)}Hz (Level ${metrics.prismLevel}) - transforming`
    );

    // Factor 5: 6D Wave State
    const bullishStates = ['ACCUMULATION', 'MARKUP', 'BULLISH'];
    const bearishStates = ['DISTRIBUTION', 'MARKDOWN', 'BEARISH'];
    const isBullish = bullishStates.includes(metrics.waveState.toUpperCase());
    const isBearish = bearishStates.includes(metrics.waveState.toUpperCase());
    factors.push({
      name: '6D Wave State',
      value: isBullish ? 1 : isBearish ? -1 : 0,
      threshold: 0,
      passed: isBullish || isBearish,
      weight: 0.10,
      contribution: isBullish ? 'BULLISH' : isBearish ? 'BEARISH' : 'NEUTRAL'
    });
    reasoning.push(
      `β—‹ 6D Wave State: ${metrics.waveState} (${isBullish ? 'bullish' : isBearish ? 'bearish' : 'neutral'})`
    );

    // Factor 6: Harmonic Lock
    factors.push({
      name: 'Harmonic Lock',
      value: metrics.harmonicLock ? 1 : 0,
      threshold: 1,
      passed: metrics.harmonicLock,
      weight: 0.10,
      contribution: metrics.harmonicLock ? 'LOCKED' : 'UNLOCKED'
    });
    reasoning.push(
      metrics.harmonicLock
        ? `βœ“ Harmonic lock ACTIVE - enhanced confidence`
        : `β—‹ Harmonic lock inactive`
    );

    // Calculate weighted decision
    let buyScore = 0;
    let sellScore = 0;

    // Coherence and Lighthouse must pass for any trade
    const canTrade = coherencePassed;

    if (canTrade) {
      // Direction from wave state and sentiment
      if (isBullish || metrics.sentimentScore > 0.1) {
        buyScore += 0.4;
      }
      if (isBearish || metrics.sentimentScore < -0.1) {
        sellScore += 0.4;
      }

      // QGITA confidence boost
      if (qgitaPassed) {
        const boost = metrics.qgitaConfidence * 0.3;
        if (metrics.probabilityFused > 0.5) buyScore += boost;
        else if (metrics.probabilityFused < 0.5) sellScore += boost;
      }

      // Prism lock boost
      if (prismLocked) {
        buyScore += 0.15;
      }

      // Harmonic lock boost
      if (metrics.harmonicLock) {
        if (buyScore > sellScore) buyScore += 0.1;
        else if (sellScore > buyScore) sellScore += 0.1;
      }
    }

    // Determine action
    let action: DecisionAction = 'HOLD';
    let confidence = 0;

    if (!canTrade) {
      action = 'HOLD';
      confidence = 1 - metrics.coherence; // High confidence in holding when coherence is low
      reasoning.push(`⚠ HOLD: Coherence below threshold - waiting for field alignment`);
    } else if (buyScore > sellScore && buyScore > 0.3) {
      action = 'BUY';
      confidence = Math.min(0.95, buyScore + (metrics.harmonicLock ? 0.1 : 0));
      reasoning.push(`β†’ BUY: Score ${(buyScore * 100).toFixed(0)}% > ${(sellScore * 100).toFixed(0)}%`);
    } else if (sellScore > buyScore && sellScore > 0.3) {
      action = 'SELL';
      confidence = Math.min(0.95, sellScore + (metrics.harmonicLock ? 0.1 : 0));
      reasoning.push(`β†’ SELL: Score ${(sellScore * 100).toFixed(0)}% > ${(buyScore * 100).toFixed(0)}%`);
    } else {
      action = 'HOLD';
      confidence = 0.5;
      reasoning.push(`β†’ HOLD: No clear direction (buy: ${(buyScore * 100).toFixed(0)}%, sell: ${(sellScore * 100).toFixed(0)}%)`);
    }

    // Build summary
    const summary = this.buildSummary(action, confidence, factors);

    const explanation: DecisionExplanation = {
      id: `decision-${Date.now()}-${crypto.randomUUID()}`,
      timestamp: Date.now(),
      action,
      confidence,
      factors,
      reasoning,
      summary,
      inputFrequency: metrics.prismFrequency * 0.8, // Approximate input
      outputFrequency: metrics.prismFrequency,
      prismLevel: metrics.prismLevel,
      waveState: metrics.waveState,
      symbol
    };

    // Store and notify
    this.explanations.unshift(explanation);
    if (this.explanations.length > this.maxHistory) {
      this.explanations.pop();
    }

    // Publish to bus
    unifiedBus.publish({
      systemName: 'DecisionExplainer',
      timestamp: Date.now(),
      ready: true,
      coherence: confidence,
      confidence,
      signal: action === 'BUY' ? 'BUY' : action === 'SELL' ? 'SELL' : 'NEUTRAL',
      data: { explanation }
    });

    // Notify listeners
    this.listeners.forEach(listener => listener(explanation));

    return explanation;
  }

  private buildSummary(action: DecisionAction, confidence: number, factors: DecisionFactor[]): string {
    const passedFactors = factors.filter(f => f.passed).length;
    const totalFactors = factors.length;

    if (action === 'BUY') {
      return `BUY signal with ${(confidence * 100).toFixed(0)}% confidence. ${passedFactors}/${totalFactors} factors aligned for long entry.`;
    } else if (action === 'SELL') {
      return `SELL signal with ${(confidence * 100).toFixed(0)}% confidence. ${passedFactors}/${totalFactors} factors aligned for short/exit.`;
    } else {
      return `HOLD - waiting for better conditions. ${passedFactors}/${totalFactors} factors aligned. Need stronger signal clarity.`;
    }
  }

  getExplanations(): DecisionExplanation[] {
    return [...this.explanations];
  }

  getLatest(): DecisionExplanation | null {
    return this.explanations[0] || null;
  }

  subscribe(listener: (explanation: DecisionExplanation) => void): () => void {
    this.listeners.add(listener);
    return () => this.listeners.delete(listener);
  }

  clear(): void {
    this.explanations = [];
  }
}

export const decisionExplainer = new DecisionExplainerClass();