-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzeFailure.ts
More file actions
279 lines (249 loc) · 9.22 KB
/
analyzeFailure.ts
File metadata and controls
279 lines (249 loc) · 9.22 KB
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
// analyzeFailure.ts
// Event 014: Failure Analysis for Self-Improvement
// Analyzes WHY synthesis failed and identifies missing concepts
import type { SynthesisFailure, IntentRequirements } from './types.js';
import type { Principle } from '../reflection/types.js';
/**
* Failure analysis result
*/
export interface FailureAnalysis {
synthesisAttempt: {
intent: string;
morphism?: any;
confidence: number;
failureReason: string;
};
testFailures?: Array<{
input: any;
expected: any;
actual: any;
reason: string;
}>;
rootCause: string;
missingConcept: string;
recommendation: string;
suggestedPrinciple?: {
name: string;
statement: string;
application: string;
};
}
/**
* Analyze why synthesis failed
* Identifies root cause and missing concepts
*/
export const analyzeFailure = (
failure: SynthesisFailure | any,
testCases?: Array<{ input: any; expected: any }>,
principleBase?: Principle[]
): FailureAnalysis => {
console.log(`\n🔬 Analyzing synthesis failure...`);
console.log(` Intent: "${failure.intent}"`);
console.log(` Reason: ${failure.reason}`);
const analysis: FailureAnalysis = {
synthesisAttempt: {
intent: failure.intent,
morphism: 'morphism' in failure ? failure.morphism : undefined,
confidence: 'confidence' in failure ? failure.confidence : 0,
failureReason: failure.reason
},
rootCause: '',
missingConcept: '',
recommendation: ''
};
// Case 1: Test cases failed (morphism created but semantically incorrect)
if (failure.reason.includes('Test cases failed') && testCases && 'morphism' in failure) {
console.log(`\n Failure type: Test validation failed`);
console.log(` Analyzing test case execution...`);
const testFailures = executeTestCases(failure.morphism, testCases);
analysis.testFailures = testFailures;
// Analyze pattern of failures
const rootCause = identifyRootCause(failure.intent, testFailures, principleBase || []);
analysis.rootCause = rootCause.cause;
analysis.missingConcept = rootCause.missingConcept;
analysis.recommendation = rootCause.recommendation;
if (rootCause.suggestedPrinciple) {
analysis.suggestedPrinciple = rootCause.suggestedPrinciple;
}
console.log(`\n Root cause: ${analysis.rootCause}`);
console.log(` Missing concept: ${analysis.missingConcept}`);
console.log(` Recommendation: ${analysis.recommendation}`);
}
// Case 2: Insufficient confidence (no morphism created)
else if (failure.reason.includes('Insufficient')) {
analysis.rootCause = 'Principle base lacks concepts for this intent';
analysis.missingConcept = `Concepts specific to "${failure.intent}"`;
analysis.recommendation = 'Add domain-specific principles';
}
// Case 3: Construction error
else if (failure.reason.includes('Construction error')) {
analysis.rootCause = 'Matched principles insufficient for construction';
analysis.missingConcept = 'Complete construction strategy';
analysis.recommendation = 'Add more detailed construction principles';
}
// Case 4: Validation failed (≤2 Rule or purity)
else if (failure.reason.includes('≤2 Rule') || failure.reason.includes('purity')) {
analysis.rootCause = 'Constructed morphism violates ontological constraints';
analysis.missingConcept = 'Simpler construction approach';
analysis.recommendation = 'Add decomposition principles for complex intents';
}
return analysis;
};
/**
* Execute test cases manually to see actual vs expected
*/
const executeTestCases = (
morphism: any,
testCases: Array<{ input: any; expected: any }>
): Array<{ input: any; expected: any; actual: any; reason: string }> => {
const failures: Array<{ input: any; expected: any; actual: any; reason: string }> = [];
for (const testCase of testCases) {
try {
// Execute morphism
let result = morphism.init;
let state = testCase.input;
let iterations = 0;
const maxIterations = 10000;
while (iterations < maxIterations) {
const next = morphism.coalgebra(state);
if (next === null || next === undefined) break;
const [val, newState] = next;
result = morphism.algebra(result, val);
state = newState;
iterations++;
}
// Apply postProcess if exists
if (morphism.postProcess) {
result = morphism.postProcess(result);
}
// Check if matches expected
const matches = compareResults(result, testCase.expected);
if (!matches) {
failures.push({
input: testCase.input,
expected: testCase.expected,
actual: result,
reason: describeDiscrepancy(testCase.expected, result)
});
}
} catch (error: any) {
failures.push({
input: testCase.input,
expected: testCase.expected,
actual: null,
reason: `Execution error: ${error.message}`
});
}
}
return failures;
};
/**
* Compare results (with tolerance for floating point)
*/
const compareResults = (actual: any, expected: any): boolean => {
if (typeof actual === 'number' && typeof expected === 'number') {
return Math.abs(actual - expected) <= 0.001;
}
return JSON.stringify(actual) === JSON.stringify(expected);
};
/**
* Describe discrepancy between expected and actual
*/
const describeDiscrepancy = (expected: any, actual: any): string => {
if (typeof expected === 'number' && typeof actual === 'number') {
if (actual > expected) {
return `Result too large (${actual} > ${expected})`;
} else {
return `Result too small (${actual} < ${expected})`;
}
}
if (Array.isArray(expected) && Array.isArray(actual)) {
if (actual.length !== expected.length) {
return `Wrong array length (${actual.length} vs ${expected.length})`;
}
return `Array content mismatch`;
}
return `Type or value mismatch`;
};
/**
* Identify root cause from test failures
*/
const identifyRootCause = (
intent: string,
failures: Array<{ input: any; expected: any; actual: any; reason: string }>,
principleBase: Principle[]
): {
cause: string;
missingConcept: string;
recommendation: string;
suggestedPrinciple?: { name: string; statement: string; application: string };
} => {
const intentLower = intent.toLowerCase();
// Case: mode (most frequent element)
if (intentLower.includes('mode')) {
// Check if failures show wrong element selection
const hasWrongSelection = failures.some(f =>
f.reason.includes('mismatch') || typeof f.expected === 'number'
);
if (hasWrongSelection) {
// Check if principleBase has frequency/counting concept
const hasFrequencyPrinciple = principleBase.some(p =>
p.statement.toLowerCase().includes('frequency') ||
p.statement.toLowerCase().includes('count') ||
p.statement.toLowerCase().includes('map')
);
if (!hasFrequencyPrinciple) {
return {
cause: 'Missing principle for frequency tracking',
missingConcept: 'Accumulator as frequency map (value → count)',
recommendation: 'Add Map-Based Accumulation principle',
suggestedPrinciple: {
name: 'Map-Based Accumulation Principle',
statement: 'When intent requires frequency/grouping/counting, use Map/Object accumulator to track value occurrences',
application: 'algebra: (map, val) => { map[val] = (map[val] || 0) + 1; return map; }'
}
};
}
}
}
// Case: median (positional selection after sort)
if (intentLower.includes('median')) {
return {
cause: 'Missing principle for order-dependent selection',
missingConcept: 'Positional selection after sorting',
recommendation: 'Add Order-Dependent Selection principle',
suggestedPrinciple: {
name: 'Order-Dependent Selection Principle',
statement: 'When intent requires positional value (median, percentile), preserve order via array + sort in postProcess',
application: 'algebra: collect array, postProcess: sort + select by index'
}
};
}
// Case: distinct (unique values)
if (intentLower.includes('distinct') || intentLower.includes('unique')) {
// Check if principleBase has Set/uniqueness concept
const hasSetPrinciple = principleBase.some(p =>
p.statement.toLowerCase().includes('set') ||
p.statement.toLowerCase().includes('unique') ||
p.statement.toLowerCase().includes('distinct')
);
if (!hasSetPrinciple) {
return {
cause: 'Missing principle for uniqueness tracking',
missingConcept: 'Accumulator as Set (value deduplication)',
recommendation: 'Add Set-Based Accumulation principle',
suggestedPrinciple: {
name: 'Set-Based Accumulation Principle',
statement: 'When intent requires uniqueness/distinct values, use Set-like accumulator to track seen values and avoid duplicates',
application: 'algebra: (seen, val) => seen.has(val) ? seen : seen.add(val), postProcess: Set → Array'
}
};
}
}
// Generic case
return {
cause: `Synthesis created morphism but logic incorrect for "${intent}"`,
missingConcept: `Domain-specific concept for "${intent}"`,
recommendation: 'Analyze test failures to identify missing accumulation strategy'
};
};