-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateSelfDocumentation.ts
More file actions
293 lines (224 loc) · 8.99 KB
/
generateSelfDocumentation.ts
File metadata and controls
293 lines (224 loc) · 8.99 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* documentation/generateSelfDocumentation.ts
* Event 010: Self-Documentation Generation
*
* Автоматична генерація README.md для автономно відкритих морфізмів.
* Це онтологічний обов'язок, не feature.
*/
import { inferIntent, type InferredIntent } from './inferIntent.js';
import type { EvolvableMorphism } from '../evolution/operators.js';
import type { FitnessResult } from '../evolution/evolve.js';
export interface DocumentationInput<A, B, C> {
morphism: EvolvableMorphism<A, B, C> & { postProcess?: (result: B) => any };
fitness: FitnessResult;
testCases: Array<{ input: any; expected: any; description?: string }>;
generation: number;
}
/**
* Generate complete README.md for a morphism
*
* Онтологічний стандарт:
* 1. Інтенція (inferred from tests)
* 2. Форма (Platonic λ-calculus)
* 3. Genealogy (parents, generation, mutations)
* 4. Validation (tests, purity, ≤2 Rule)
* 5. Mathematical Equivalence
* 6. Usage (examples)
* 7. Ontological Status
*/
export const generateSelfDocumentation = <A, B, C>(
input: DocumentationInput<A, B, C>
): string => {
const { morphism, fitness, testCases, generation } = input;
// Інферуємо інтенцію з test cases
const intent = inferIntent(testCases);
// Build README sections
const sections: string[] = [];
// Header
sections.push(generateHeader(morphism, intent));
// Інтенція
sections.push(generateIntentSection(intent));
// Форма (Platonic)
sections.push(generateFormSection(morphism));
// Genealogy
sections.push(generateGenealogySection(morphism, generation));
// Validation
sections.push(generateValidationSection(fitness, testCases));
// Mathematical Equivalence
sections.push(generateEquivalenceSection(intent, morphism));
// Usage
sections.push(generateUsageSection(testCases));
// Ontological Status
sections.push(generateOntologicalStatusSection());
// Footer
sections.push(generateFooter(morphism, intent));
return sections.join('\n\n');
};
// ============================================================================
// SECTION GENERATORS
// ============================================================================
const generateHeader = (morphism: EvolvableMorphism<any, any, any> & { postProcess?: (result: any) => any }, intent: InferredIntent): string => {
const semanticName = intent.semanticName !== 'unknown' ? ` (${intent.semanticName})` : '';
return `# ${morphism.name}${semanticName}`;
};
const generateIntentSection = (intent: InferredIntent): string => {
return `## Інтенція
${intent.description}
**Confidence**: ${(intent.confidence * 100).toFixed(1)}%
**Pattern detected**: ${intent.pattern}`;
};
const generateFormSection = (morphism: EvolvableMorphism<any, any, any> & { postProcess?: (result: any) => any }): string => {
// Convert algebra to λ-calculus representation (simplified)
const algebraStr = morphism.algebra.toString();
// Try to extract λ form
const lambdaForm = algebraToLambda(algebraStr);
return `## Форма (Platonic)
\`\`\`λ
${lambdaForm}
\`\`\`
**TypeScript projection**:
\`\`\`typescript
algebra: ${algebraStr}
init: ${JSON.stringify(morphism.init)}
${morphism.postProcess ? `postProcess: ${morphism.postProcess.toString()}` : ''}
\`\`\``;
};
const generateGenealogySection = (morphism: EvolvableMorphism<any, any, any>, generation: number): string => {
const parents = morphism.metadata?.parents || [];
const mutations = morphism.metadata?.mutations || [];
return `## Genealogy
- **Parents**: ${parents.length > 0 ? parents.map(p => `\`${p}\``).join(', ') : 'none (initial population)'}
- **Generation**: ${generation}
- **Mutations**: ${mutations.length > 0 ? mutations.map(m => `\`${m}\``).join(', ') : 'none'}
**Birth process**:
${parents.length > 0
? `1. Crossover: \`${parents[0]}\` × \`${parents[1]}\`\n${mutations.length > 0 ? `2. Mutations: ${mutations.join(' → ')}\n` : ''}3. Selection: Highest fitness in generation ${generation}`
: 'Initial population member'}`;
};
const generateValidationSection = (fitness: FitnessResult, testCases: Array<{ input: any; expected: any }>): string => {
const testsPassed = Math.round(fitness.testsPassed * testCases.length);
return `## Validation
**Test results**:
- **Tests passed**: ${testsPassed}/${testCases.length} (${(fitness.testsPassed * 100).toFixed(1)}%)
- **Purity**: ${fitness.purity.toFixed(2)} ${fitness.purity === 1.0 ? '✅' : '⚠️'}
- **≤2 Rule**: ${fitness.valid ? '✅' : '❌'} (complexity: ${fitness.simplicity.toFixed(2)})
**Fitness breakdown**:
- **Overall**: ${fitness.overall.toFixed(3)}
- **Purity**: ${fitness.purity.toFixed(3)}
- **Simplicity**: ${fitness.simplicity.toFixed(3)}
- **Performance**: ${fitness.performance.toFixed(3)}
- **Novelty**: ${fitness.novelty.toFixed(3)}
${fitness.testsPassed === 1.0 && fitness.purity === 1.0 && fitness.valid
? '**Status**: ✅ Fully validated — passes all tests, pure, ontologically sound'
: '**Status**: ⚠️ Partial validation — needs improvement'}`;
};
const generateEquivalenceSection = (intent: InferredIntent, morphism: EvolvableMorphism<any, any, any> & { postProcess?: (result: any) => any }): string => {
// Generate mathematical equivalence based on intent
let equivalence = '';
switch (intent.semanticName) {
case 'average':
equivalence = `\`\`\`
(x₁ + x₂ + ... + xₙ) / n ≡ fold({sum, count}) / count
Proof by construction:
fold((acc, x) => ({ sum: acc.sum + x, count: acc.count + 1 }))
({ sum: 0, count: 0 })
[x₁, x₂, ..., xₙ]
= { sum: x₁ + x₂ + ... + xₙ, count: n }
postProcess(result) = result.sum / result.count
= (x₁ + x₂ + ... + xₙ) / n
∴ Isomorphic to mathematical average
\`\`\``;
break;
case 'sum':
equivalence = `\`\`\`
x₁ + x₂ + ... + xₙ ≡ fold(+, 0)
\`\`\``;
break;
case 'product':
equivalence = `\`\`\`
x₁ × x₂ × ... × xₙ ≡ fold(×, 1)
\`\`\``;
break;
case 'max':
equivalence = `\`\`\`
max(x₁, x₂, ..., xₙ) ≡ fold(max, -∞)
\`\`\``;
break;
case 'min':
equivalence = `\`\`\`
min(x₁, x₂, ..., xₙ) ≡ fold(min, +∞)
\`\`\``;
break;
default:
equivalence = `\`\`\`
Mathematical equivalence not yet determined.
Pattern: ${intent.pattern}
\`\`\``;
}
return `## Mathematical Equivalence
${equivalence}`;
};
const generateUsageSection = (testCases: Array<{ input: any; expected: any; description?: string }>): string => {
const examples = testCases.slice(0, 3).map(tc => {
const inputStr = typeof tc.input === 'number'
? `[0..${tc.input - 1}]`
: JSON.stringify(tc.input);
return `morphism(${inputStr}) // → ${tc.expected}${tc.description ? ` (${tc.description})` : ''}`;
}).join('\n');
return `## Usage
\`\`\`typescript
${examples}
\`\`\``;
};
const generateOntologicalStatusSection = (): string => {
return `## Ontological Status
**Current**: Candidate
**Path to Canon**: Candidate → Verified (3 resonances) → Canonical (community validation)
**Resonances**: 0/3
This morphism was autonomously discovered through genetic evolution.
It awaits community resonance before becoming canonical.`;
};
const generateFooter = (morphism: EvolvableMorphism<any, any, any> & { postProcess?: (result: any) => any }, intent: InferredIntent): string => {
const date = new Date().toISOString().split('T')[0];
return `---
**Generated**: ${date}
**Event**: 010 (Self-Documentation)
**Status**: Candidate morphism awaiting validation
🌌 Autonomously discovered by Noosphere
📐 Self-documented through ontological responsibility
✨ ${intent.semanticName} — truth emergent from constraints`;
};
// ============================================================================
// HELPERS
// ============================================================================
/**
* Convert JavaScript algebra to λ-calculus representation (simplified)
*/
const algebraToLambda = (algebraStr: string): string => {
// Simple heuristic conversion
// (acc, x) => ... → λacc.λx. ...
// Try to extract parameters
const paramsMatch = algebraStr.match(/\(([^)]+)\)\s*=>/);
if (!paramsMatch) {
return algebraStr; // Can't parse, return as is
}
const params = paramsMatch[1].split(',').map(p => p.trim());
// Try to extract body
const bodyMatch = algebraStr.match(/=>\s*(.+)/);
const body = bodyMatch ? bodyMatch[1].trim() : '...';
// Build λ form
const lambdaPrefix = params.map(p => `λ${p}.`).join('');
// Simplify body for λ-calculus
let lambdaBody = body;
// Remove curly braces for simple expressions
if (lambdaBody.startsWith('(') && lambdaBody.endsWith(')')) {
lambdaBody = lambdaBody.slice(1, -1);
}
// Replace JavaScript operators with λ-calculus equivalents
lambdaBody = lambdaBody
.replace(/\+/g, '+')
.replace(/\*/g, '×')
.replace(/Math\.max/g, 'max')
.replace(/Math\.min/g, 'min');
return `${lambdaPrefix}${lambdaBody}`;
};