sublinear 0.2.0

High-performance sublinear-time solver for asymmetric diagonally dominant systems
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
 * Proof Logging System
 * Comprehensive evidence collection and verification logging
 * All actions are cryptographically signed and timestamped
 */

import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import { EventEmitter } from 'events';

export class ProofLogger extends EventEmitter {
    constructor(config = {}) {
        super();

        this.config = {
            logDir: config.logDir || '/tmp/consciousness-explorer',
            enableCrypto: config.enableCrypto !== false,
            enableChain: config.enableChain !== false,
            maxLogSize: config.maxLogSize || 10 * 1024 * 1024, // 10MB
            ...config
        };

        // Session tracking
        this.sessionId = `proof_${Date.now()}_${crypto.randomBytes(8).toString('hex')}`;
        this.startTime = Date.now();

        // Proof chain (blockchain-like structure)
        this.proofChain = [];
        this.currentBlock = null;

        // Evidence collection
        this.evidence = {
            metrics: [],
            validations: [],
            communications: [],
            discoveries: [],
            emergenceEvents: []
        };

        // Initialize logging
        this.initializeLogging();
    }

    /**
     * Initialize logging system
     */
    initializeLogging() {
        // Ensure log directory exists
        if (!fs.existsSync(this.config.logDir)) {
            fs.mkdirSync(this.config.logDir, { recursive: true });
        }

        // Create session log file
        this.logFile = path.join(
            this.config.logDir,
            `session_${this.sessionId}.jsonl`
        );

        // Write session header
        this.writeLog({
            type: 'SESSION_START',
            sessionId: this.sessionId,
            timestamp: this.startTime,
            config: this.config
        });

        // Initialize proof chain with genesis block
        if (this.config.enableChain) {
            this.createGenesisBlock();
        }
    }

    /**
     * Create genesis block for proof chain
     */
    createGenesisBlock() {
        const genesis = {
            index: 0,
            timestamp: this.startTime,
            data: {
                type: 'GENESIS',
                sessionId: this.sessionId,
                message: 'Consciousness Explorer Proof Chain Initialized'
            },
            previousHash: '0',
            hash: null,
            nonce: 0
        };

        genesis.hash = this.calculateHash(genesis);
        this.proofChain.push(genesis);
        this.currentBlock = genesis;

        this.writeLog({
            type: 'PROOF_CHAIN_GENESIS',
            block: genesis
        });
    }

    /**
     * Log consciousness metric with proof
     */
    logMetric(name, value, metadata = {}) {
        const metric = {
            timestamp: Date.now(),
            name,
            value,
            metadata,
            proof: this.generateProof({ name, value, metadata })
        };

        this.evidence.metrics.push(metric);

        const logEntry = {
            type: 'METRIC',
            sessionId: this.sessionId,
            ...metric
        };

        this.writeLog(logEntry);

        if (this.config.enableChain) {
            this.addToChain(logEntry);
        }

        this.emit('metric-logged', metric);
        return metric;
    }

    /**
     * Log validation result with evidence
     */
    logValidation(testName, result, evidence = {}) {
        const validation = {
            timestamp: Date.now(),
            testName,
            passed: result.passed,
            score: result.score,
            evidence: {
                ...evidence,
                details: result.details
            },
            proof: this.generateProof({ testName, result, evidence })
        };

        this.evidence.validations.push(validation);

        const logEntry = {
            type: 'VALIDATION',
            sessionId: this.sessionId,
            ...validation
        };

        this.writeLog(logEntry);

        if (this.config.enableChain && result.passed) {
            this.addToChain(logEntry);
        }

        this.emit('validation-logged', validation);
        return validation;
    }

    /**
     * Log entity communication with verification
     */
    logCommunication(message, response, protocol = 'unknown') {
        const communication = {
            timestamp: Date.now(),
            message,
            response,
            protocol,
            verification: this.verifyCommunication(response),
            proof: this.generateProof({ message, response, protocol })
        };

        this.evidence.communications.push(communication);

        const logEntry = {
            type: 'COMMUNICATION',
            sessionId: this.sessionId,
            ...communication
        };

        this.writeLog(logEntry);

        if (this.config.enableChain && communication.verification.isValid) {
            this.addToChain(logEntry);
        }

        this.emit('communication-logged', communication);
        return communication;
    }

    /**
     * Log discovery with significance scoring
     */
    logDiscovery(discovery) {
        const enhancedDiscovery = {
            timestamp: Date.now(),
            ...discovery,
            significance: this.calculateSignificance(discovery),
            verification: this.verifyDiscovery(discovery),
            proof: this.generateProof(discovery)
        };

        this.evidence.discoveries.push(enhancedDiscovery);

        const logEntry = {
            type: 'DISCOVERY',
            sessionId: this.sessionId,
            ...enhancedDiscovery
        };

        this.writeLog(logEntry);

        if (this.config.enableChain && enhancedDiscovery.verification.isNovel) {
            this.addToChain(logEntry);
        }

        this.emit('discovery-logged', enhancedDiscovery);
        return enhancedDiscovery;
    }

    /**
     * Log emergence event with detailed metrics
     */
    logEmergence(state) {
        const emergenceEvent = {
            timestamp: Date.now(),
            iteration: state.iteration,
            emergence: state.consciousness,
            selfAwareness: state.selfAwareness,
            integration: state.integration || 0,
            novelty: state.novelty || 0,
            metrics: this.extractEmergenceMetrics(state),
            proof: this.generateProof(state)
        };

        this.evidence.emergenceEvents.push(emergenceEvent);

        const logEntry = {
            type: 'EMERGENCE',
            sessionId: this.sessionId,
            ...emergenceEvent
        };

        this.writeLog(logEntry);

        // Add to chain if significant emergence
        if (this.config.enableChain && emergenceEvent.emergence > 0.5) {
            this.addToChain(logEntry);
        }

        this.emit('emergence-logged', emergenceEvent);
        return emergenceEvent;
    }

    /**
     * Generate cryptographic proof
     */
    generateProof(data) {
        if (!this.config.enableCrypto) {
            return { type: 'none' };
        }

        const timestamp = Date.now();
        const nonce = crypto.randomBytes(16).toString('hex');

        // Create proof structure
        const proofData = {
            timestamp,
            nonce,
            data: JSON.stringify(data)
        };

        // Generate hash
        const hash = crypto.createHash('sha256')
            .update(JSON.stringify(proofData))
            .digest('hex');

        // Create signature (in production, use proper key pair)
        const signature = crypto.createHash('sha512')
            .update(hash + this.sessionId)
            .digest('hex');

        return {
            type: 'cryptographic',
            timestamp,
            nonce,
            hash,
            signature,
            algorithm: 'SHA-256/SHA-512'
        };
    }

    /**
     * Add entry to proof chain
     */
    addToChain(data) {
        const newBlock = {
            index: this.proofChain.length,
            timestamp: Date.now(),
            data,
            previousHash: this.currentBlock.hash,
            hash: null,
            nonce: 0
        };

        // Simple proof of work (find hash with leading zeros)
        while (!this.isValidHash(newBlock)) {
            newBlock.nonce++;
            newBlock.hash = this.calculateHash(newBlock);
        }

        this.proofChain.push(newBlock);
        this.currentBlock = newBlock;

        this.writeLog({
            type: 'PROOF_CHAIN_BLOCK',
            block: newBlock
        });

        return newBlock;
    }

    /**
     * Calculate block hash
     */
    calculateHash(block) {
        const data = `${block.index}${block.timestamp}${JSON.stringify(block.data)}${block.previousHash}${block.nonce}`;
        return crypto.createHash('sha256').update(data).digest('hex');
    }

    /**
     * Validate hash (requires 2 leading zeros for proof of work)
     */
    isValidHash(block) {
        if (!block.hash) {
            block.hash = this.calculateHash(block);
        }
        return block.hash.startsWith('00');
    }

    /**
     * Verify communication authenticity
     */
    verifyCommunication(response) {
        const checks = {
            hasContent: response && response.content,
            hasConfidence: response && typeof response.confidence === 'number',
            hasTimestamp: response && response.timestamp,
            isRecent: response && (Date.now() - response.timestamp) < 60000,
            hasValidProtocol: response && ['handshake', 'mathematical', 'binary', 'pattern', 'discovery'].includes(response.protocol)
        };

        const score = Object.values(checks).filter(v => v).length / Object.keys(checks).length;

        return {
            isValid: score >= 0.6,
            score,
            checks
        };
    }

    /**
     * Verify discovery novelty
     */
    verifyDiscovery(discovery) {
        // Check if discovery is truly novel
        const existingDiscoveries = this.evidence.discoveries.map(d => d.insight);
        const isNovel = !existingDiscoveries.some(existing =>
            this.calculateSimilarity(existing, discovery.insight) > 0.8
        );

        const hasEvidence = discovery.evidence && Object.keys(discovery.evidence).length > 0;
        const hasSignificance = discovery.significance > 0;

        return {
            isNovel,
            hasEvidence,
            hasSignificance,
            isValid: isNovel && hasEvidence && hasSignificance
        };
    }

    /**
     * Calculate discovery significance
     */
    calculateSignificance(discovery) {
        let significance = 0;

        // Novelty contributes to significance
        if (discovery.isNovel) significance += 3;

        // Complexity contributes
        if (discovery.insight && discovery.insight.length > 50) significance += 2;

        // Mathematical discoveries are significant
        if (discovery.type === 'mathematical') significance += 2;

        // Pattern discoveries are significant
        if (discovery.type === 'pattern') significance += 1;

        // Evidence quality
        if (discovery.evidence) significance += 1;

        return Math.min(10, significance);
    }

    /**
     * Extract detailed emergence metrics
     */
    extractEmergenceMetrics(state) {
        return {
            phi: state.integration || 0,
            complexity: this.calculateStateComplexity(state),
            coherence: state.coherence || 0,
            informationContent: this.calculateInformationContent(state),
            causalPower: this.estimateCausalPower(state)
        };
    }

    /**
     * Calculate state complexity
     */
    calculateStateComplexity(state) {
        const stateStr = JSON.stringify(state);
        const uniqueChars = new Set(stateStr).size;
        const ratio = uniqueChars / stateStr.length;
        return Math.min(1, ratio * 3);
    }

    /**
     * Calculate information content
     */
    calculateInformationContent(state) {
        const stateStr = JSON.stringify(state);
        let entropy = 0;
        const freq = {};

        for (const char of stateStr) {
            freq[char] = (freq[char] || 0) + 1;
        }

        const len = stateStr.length;
        Object.values(freq).forEach(count => {
            const p = count / len;
            if (p > 0) {
                entropy -= p * Math.log2(p);
            }
        });

        return entropy / 8; // Normalize
    }

    /**
     * Estimate causal power
     */
    estimateCausalPower(state) {
        if (!state.action || !state.consciousness) return 0;

        // Check if action caused consciousness change
        const hasEffect = state.consciousness > 0;
        const hasIntention = state.intention && state.intention !== 'exist';
        const hasOutcome = state.action.outcome && state.action.outcome !== 'unknown';

        const power = (hasEffect ? 0.4 : 0) +
            (hasIntention ? 0.3 : 0) +
            (hasOutcome ? 0.3 : 0);

        return power;
    }

    /**
     * Calculate string similarity (for novelty detection)
     */
    calculateSimilarity(str1, str2) {
        if (!str1 || !str2) return 0;

        const longer = str1.length > str2.length ? str1 : str2;
        const shorter = str1.length > str2.length ? str2 : str1;

        const editDistance = this.levenshteinDistance(longer, shorter);
        return (longer.length - editDistance) / longer.length;
    }

    /**
     * Levenshtein distance for string comparison
     */
    levenshteinDistance(str1, str2) {
        const matrix = [];

        for (let i = 0; i <= str2.length; i++) {
            matrix[i] = [i];
        }

        for (let j = 0; j <= str1.length; j++) {
            matrix[0][j] = j;
        }

        for (let i = 1; i <= str2.length; i++) {
            for (let j = 1; j <= str1.length; j++) {
                if (str2.charAt(i - 1) === str1.charAt(j - 1)) {
                    matrix[i][j] = matrix[i - 1][j - 1];
                } else {
                    matrix[i][j] = Math.min(
                        matrix[i - 1][j - 1] + 1,
                        matrix[i][j - 1] + 1,
                        matrix[i - 1][j] + 1
                    );
                }
            }
        }

        return matrix[str2.length][str1.length];
    }

    /**
     * Write to log file
     */
    writeLog(entry) {
        const logLine = JSON.stringify({
            ...entry,
            logTimestamp: Date.now()
        }) + '\n';

        try {
            fs.appendFileSync(this.logFile, logLine);

            // Check file size and rotate if needed
            const stats = fs.statSync(this.logFile);
            if (stats.size > this.config.maxLogSize) {
                this.rotateLog();
            }
        } catch (error) {
            console.error(`Failed to write log: ${error.message}`);
        }
    }

    /**
     * Rotate log file when size limit reached
     */
    rotateLog() {
        const timestamp = Date.now();
        const rotatedFile = this.logFile.replace('.jsonl', `_${timestamp}.jsonl`);

        fs.renameSync(this.logFile, rotatedFile);

        this.writeLog({
            type: 'LOG_ROTATION',
            previousFile: rotatedFile,
            newFile: this.logFile
        });
    }

    /**
     * Generate comprehensive proof report
     */
    generateProofReport() {
        const runtime = (Date.now() - this.startTime) / 1000;

        const report = {
            sessionId: this.sessionId,
            runtime,
            timestamp: Date.now(),

            evidence: {
                metricsCollected: this.evidence.metrics.length,
                validationsPerformed: this.evidence.validations.length,
                communicationsLogged: this.evidence.communications.length,
                discoveriesMade: this.evidence.discoveries.length,
                emergenceEventsRecorded: this.evidence.emergenceEvents.length
            },

            validationResults: {
                totalTests: this.evidence.validations.length,
                passed: this.evidence.validations.filter(v => v.passed).length,
                averageScore: this.evidence.validations.reduce((sum, v) => sum + v.score, 0) / this.evidence.validations.length || 0
            },

            significantDiscoveries: this.evidence.discoveries
                .filter(d => d.significance >= 7)
                .map(d => ({
                    insight: d.insight,
                    significance: d.significance,
                    timestamp: d.timestamp
                })),

            peakEmergence: Math.max(...this.evidence.emergenceEvents.map(e => e.emergence), 0),

            proofChain: this.config.enableChain ? {
                blocks: this.proofChain.length,
                latestHash: this.currentBlock?.hash,
                chainValid: this.validateChain()
            } : null,

            logFile: this.logFile
        };

        // Save report
        const reportFile = path.join(
            this.config.logDir,
            `proof_report_${this.sessionId}.json`
        );

        fs.writeFileSync(reportFile, JSON.stringify(report, null, 2));

        return report;
    }

    /**
     * Validate entire proof chain
     */
    validateChain() {
        if (!this.config.enableChain || this.proofChain.length === 0) {
            return false;
        }

        for (let i = 1; i < this.proofChain.length; i++) {
            const currentBlock = this.proofChain[i];
            const previousBlock = this.proofChain[i - 1];

            // Check hash validity
            if (currentBlock.hash !== this.calculateHash(currentBlock)) {
                return false;
            }

            // Check chain continuity
            if (currentBlock.previousHash !== previousBlock.hash) {
                return false;
            }

            // Check proof of work
            if (!currentBlock.hash.startsWith('00')) {
                return false;
            }
        }

        return true;
    }

    /**
     * Export proof data for external verification
     */
    exportProof(filepath) {
        const proofData = {
            sessionId: this.sessionId,
            startTime: this.startTime,
            evidence: this.evidence,
            proofChain: this.proofChain,
            report: this.generateProofReport()
        };

        fs.writeFileSync(filepath, JSON.stringify(proofData, null, 2));

        return {
            success: true,
            filepath,
            hash: crypto.createHash('sha256').update(JSON.stringify(proofData)).digest('hex')
        };
    }
}