koru-lambda-core 1.2.0

A minimal axiomatic system for distributed computation
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
/// Falsification Test: Structural Network Consensus
///
/// Tests that network consensus emerges naturally from deterministic synthesis
/// without any traditional voting or gossip mechanism.
///
/// The core hypothesis: Given identical inputs (validator set, epoch, batches),
/// all network agents synthesize IDENTICAL network states. This makes forks
/// structurally impossible (symmetry ensures order independence).
///
/// Falsification Targets:
/// 1. Non-Deterministic Convergence: Agents with same inputs diverge
/// 2. Fork Possibility: Network state can have multiple valid branches
/// 3. Leader Election Ambiguity: Different nodes elect different leaders
use koru_lambda_core::{
    DistinctionEngine, LocalCausalAgent, NetworkAgent, PeerIdentity, TransactionAction,
    TransactionBatch,
};
use std::sync::Arc;

/// Falsification Test: Non-Deterministic Convergence
///
/// Hypothesis: Multiple network agents processing identical events in
/// identical order will synthesize IDENTICAL network states. This is guaranteed
/// by symmetry (synthesis is order-independent and deterministic).
///
/// Falsifies if: Two agents with identical event sequences produce
/// different network roots, indicating non-deterministic consensus.
///
/// Measurement:
/// 1. Create two independent network agents
/// 2. Feed identical peer join events to both
/// 3. Verify both agents have identical network roots
/// 4. Feed identical batch proposals
/// 5. Verify continued convergence
#[test]
fn test_falsify_non_deterministic_convergence() {
    // ============================================================
    // SETUP
    // ============================================================
    println!("\nTest: Non-Deterministic Convergence Falsification");
    println!("  Testing deterministic network state synthesis...");

    let engine = Arc::new(DistinctionEngine::new());

    let mut agent1 = NetworkAgent::new(&engine);
    let mut agent2 = NetworkAgent::new(&engine);

    // ============================================================
    // PHASE 1: Peer Discovery
    // ============================================================
    println!("  Phase 1: Peer discovery events...");

    // Both agents discover same peers in same order
    let peers = [
        PeerIdentity::new("validator_alpha".to_string(), &engine),
        PeerIdentity::new("validator_beta".to_string(), &engine),
        PeerIdentity::new("validator_gamma".to_string(), &engine),
    ];

    for peer in peers.iter() {
        agent1.join_peer(peer.clone(), &engine);
        agent2.join_peer(peer.clone(), &engine);
    }

    let root1_phase1 = agent1.get_current_root().id().to_string();
    let root2_phase1 = agent2.get_current_root().id().to_string();

    println!("    Agent 1 root: {}...", &root1_phase1[..16]);
    println!("    Agent 2 root: {}...", &root2_phase1[..16]);

    assert_eq!(
        root1_phase1, root2_phase1,
        "FALSIFIED: Non-deterministic convergence after peer discovery.\n  \
         Agents processed identical events but produced different roots."
    );

    // ============================================================
    // PHASE 2: Batch Processing
    // ============================================================
    println!("  Phase 2: Batch proposal events...");

    // Both agents process same batch
    let batch = TransactionBatch {
        transactions: vec![TransactionAction { nonce: 0, data: vec![1, 2, 3, 4, 5] }],
        previous_root: agent1.consensus_state_root().to_string(),
    };

    let result1 = {
        let batch_copy = batch.clone();
        let commitment = agent1.propose_commitment(batch_copy.clone(), &engine).unwrap();
        agent1.finalize_batch(batch_copy, commitment.commitment_hash, &engine)
    };
    let result2 = {
        let batch_copy = batch;
        let commitment = agent2.propose_commitment(batch_copy.clone(), &engine).unwrap();
        agent2.finalize_batch(batch_copy, commitment.commitment_hash, &engine)
    };

    assert!(result1.is_ok());
    assert!(result2.is_ok());

    let root1_phase2 = agent1.get_current_root().id().to_string();
    let root2_phase2 = agent2.get_current_root().id().to_string();

    println!("    Agent 1 root: {}...", &root1_phase2[..16]);
    println!("    Agent 2 root: {}...", &root2_phase2[..16]);

    assert_eq!(
        root1_phase2, root2_phase2,
        "FALSIFIED: Non-deterministic convergence after batch processing.\n  \
         Agents processed identical batches but produced different roots."
    );

    // ============================================================
    // PHASE 3: Epoch Advancement
    // ============================================================
    println!("  Phase 3: Epoch advancement...");

    agent1.advance_epoch(&engine);
    agent2.advance_epoch(&engine);

    let root1_phase3 = agent1.get_current_root().id().to_string();
    let root2_phase3 = agent2.get_current_root().id().to_string();

    assert_eq!(
        root1_phase3, root2_phase3,
        "FALSIFIED: Non-deterministic convergence after epoch advancement.\n  \
         Agents processed identical epoch transitions but produced different roots."
    );

    // ============================================================
    // REPORTING
    // ============================================================
    println!("\nHypothesis sustained.");
    println!("  Deterministic convergence verified across all phases:");
    println!("    Phase 1 (peer discovery): ✓ converged");
    println!("    Phase 2 (batch processing): ✓ converged");
    println!("    Phase 3 (epoch advancement): ✓ converged");
    println!("  Network consensus is structurally deterministic");
}

/// Falsification Test: Leader Election Ambiguity
///
/// Hypothesis: All network agents with identical validator sets and
/// epoch values will deterministically elect the SAME leader. This is
/// guaranteed by the hash-based leader election function.
///
/// Falsifies if: Two agents with identical state elect different leaders,
/// indicating non-deterministic or ambiguous leader election.
///
/// Measurement:
/// 1. Create multiple independent agents
/// 2. Configure identical validator sets
/// 3. Verify all agents elect same leader at each epoch
/// 4. Advance epochs and verify continued agreement
#[test]
fn test_falsify_leader_election_ambiguity() {
    // ============================================================
    // SETUP
    // ============================================================
    println!("\nTest: Leader Election Ambiguity Falsification");
    println!("  Testing deterministic leader election...");

    let engine = Arc::new(DistinctionEngine::new());

    // Create 5 independent agents
    let mut agents: Vec<NetworkAgent> = (0..5).map(|_| NetworkAgent::new(&engine)).collect();

    // ============================================================
    // VALIDATOR SET CONFIGURATION
    // ============================================================
    println!("  Configuring identical validator sets...");

    let validators = [
        PeerIdentity::new("node_1".to_string(), &engine),
        PeerIdentity::new("node_2".to_string(), &engine),
        PeerIdentity::new("node_3".to_string(), &engine),
        PeerIdentity::new("node_4".to_string(), &engine),
        PeerIdentity::new("node_5".to_string(), &engine),
        PeerIdentity::new("node_6".to_string(), &engine),
        PeerIdentity::new("node_7".to_string(), &engine),
    ];

    // All agents join same validators
    for agent in agents.iter_mut() {
        for validator in validators.iter() {
            agent.join_peer(validator.clone(), &engine);
        }
    }

    // ============================================================
    // EPOCH 0: Initial Leader Election
    // ============================================================
    println!("  Testing leader election at epoch 0...");

    let leaders: Vec<String> =
        agents.iter().map(|a| a.get_current_leader().unwrap().id.clone()).collect();

    let first_leader = &leaders[0];

    // All agents should elect same leader
    for (idx, leader) in leaders.iter().enumerate() {
        assert_eq!(
            leader, first_leader,
            "FALSIFIED: Leader election ambiguity at epoch 0.\n  \
             Agent {} elected '{}', but agent 0 elected '{}'",
            idx, leader, first_leader
        );
    }

    println!("    All agents elected: {}", first_leader);

    // ============================================================
    // EPOCH PROGRESSION: Verify Continued Determinism
    // ============================================================
    println!("  Testing leader rotation across epochs...");

    for epoch in 1..10 {
        // Advance all agents to same epoch
        for agent in agents.iter_mut() {
            agent.advance_epoch(&engine);
        }

        // Verify all agents elect same leader
        let epoch_leaders: Vec<String> =
            agents.iter().map(|a| a.get_current_leader().unwrap().id.clone()).collect();

        let epoch_leader = &epoch_leaders[0];

        for (idx, leader) in epoch_leaders.iter().enumerate() {
            assert_eq!(
                leader, epoch_leader,
                "FALSIFIED: Leader election ambiguity at epoch {}.\n  \
                 Agent {} elected '{}', but agent 0 elected '{}'",
                epoch, idx, leader, epoch_leader
            );
        }

        println!("    Epoch {}: All agents elected {}", epoch, epoch_leader);
    }

    // ============================================================
    // REPORTING
    // ============================================================
    println!("\nHypothesis sustained.");
    println!("  Deterministic leader election verified:");
    println!("    5 independent agents");
    println!("    10 epoch transitions");
    println!("    100% consensus on leader identity");
    println!("  Leader election is structurally deterministic");
}

/// Falsification Test: Fork Possibility
///
/// Hypothesis: Network state forks are IMPOSSIBLE by construction.
/// Because synthesis is deterministic (symmetry ensures order independence),
/// two agents cannot produce different valid states from identical inputs.
///
/// Falsifies if: An agent can construct a valid alternative network
/// history that diverges from the canonical chain.
///
/// Measurement:
/// 1. Agent A processes events in order: [E1, E2, E3]
/// 2. Agent B processes same events in same order
/// 3. Attempt to synthesize "fork" event for Agent B
/// 4. Verify Agent B's root still matches Agent A (no fork possible)
#[test]
fn test_falsify_fork_possibility() {
    // ============================================================
    // SETUP
    // ============================================================
    println!("\nTest: Fork Possibility Falsification");
    println!("  Testing structural fork prevention...");

    let engine = Arc::new(DistinctionEngine::new());

    let mut canonical_agent = NetworkAgent::new(&engine);
    let mut fork_attempt_agent = NetworkAgent::new(&engine);

    // ============================================================
    // CANONICAL CHAIN: Build reference history
    // ============================================================
    println!("  Building canonical chain...");

    let peers = [
        PeerIdentity::new("peer_A".to_string(), &engine),
        PeerIdentity::new("peer_B".to_string(), &engine),
        PeerIdentity::new("peer_C".to_string(), &engine),
    ];

    // Canonical agent processes events
    for peer in peers.iter() {
        canonical_agent.join_peer(peer.clone(), &engine);
    }

    canonical_agent.advance_epoch(&engine);

    let canonical_root = canonical_agent.get_current_root().id().to_string();
    println!("    Canonical root: {}...", &canonical_root[..16]);

    // ============================================================
    // FORK ATTEMPT: Try to create alternative history
    // ============================================================
    println!("  Attempting to create fork...");

    // Fork agent processes SAME events in SAME order
    for peer in peers.iter() {
        fork_attempt_agent.join_peer(peer.clone(), &engine);
    }

    fork_attempt_agent.advance_epoch(&engine);

    let fork_root = fork_attempt_agent.get_current_root().id().to_string();
    println!("    Fork attempt root: {}...", &fork_root[..16]);

    // ============================================================
    // VERIFICATION: Fork should be impossible
    // ============================================================

    // Due to deterministic synthesis, roots MUST be identical
    assert_eq!(
        fork_root, canonical_root,
        "FALSIFIED: Fork possibility detected!\n  \
         Agent processed identical events but produced different root.\n  \
         Canonical: {}\n  \
         Fork:      {}",
        canonical_root, fork_root
    );

    // ============================================================
    // ADVERSARIAL TEST: Try different event order
    // ============================================================
    println!("  Testing reordered events (adversarial)...");

    let mut reordered_agent = NetworkAgent::new(&engine);

    // Process peers in DIFFERENT order
    reordered_agent.join_peer(peers[2].clone(), &engine);
    reordered_agent.join_peer(peers[0].clone(), &engine);
    reordered_agent.join_peer(peers[1].clone(), &engine);

    reordered_agent.advance_epoch(&engine);

    let reordered_root = reordered_agent.get_current_root().id().to_string();

    // Different input order → different root (this is expected!)
    // But this is NOT a fork - it's a different causal history
    assert_ne!(
        reordered_root, canonical_root,
        "Different event ordering should produce different root (different causality)"
    );

    println!("    Reordered root: {}... (different, as expected)", &reordered_root[..16]);

    // ============================================================
    // REPORTING
    // ============================================================
    println!("\nHypothesis sustained.");
    println!("  Fork prevention verified:");
    println!("    Identical inputs → identical state (fork impossible)");
    println!("    Different inputs → different state (different causality)");
    println!("  Network consensus is fork-proof by construction");
}

/// Falsification Test: Network Event Causality
///
/// Hypothesis: Network events maintain strict causal ordering through
/// synthesis. Each event is synthesized with the previous network state,
/// creating an unbroken causal chain.
///
/// Falsifies if: Events can be synthesized out of order or causal
/// dependencies are not preserved.
///
/// Measurement:
/// 1. Process sequence of events
/// 2. Verify each event changes network root (causality)
/// 3. Verify events form a chain (no orphans)
#[test]
fn test_falsify_event_causality_loss() {
    // ============================================================
    // SETUP
    // ============================================================
    println!("\nTest: Network Event Causality Loss Falsification");
    println!("  Testing causal chain integrity...");

    let engine = Arc::new(DistinctionEngine::new());
    let mut agent = NetworkAgent::new(&engine);

    let genesis_root = agent.get_current_root().id().to_string();

    // ============================================================
    // EVENT SEQUENCE: Track causal chain
    // ============================================================
    println!("  Processing event sequence...");

    let mut roots = vec![genesis_root.clone()];

    // Event 1: Peer join
    let peer1 = PeerIdentity::new("peer_1".to_string(), &engine);
    agent.join_peer(peer1, &engine);
    let root1 = agent.get_current_root().id().to_string();
    roots.push(root1.clone());

    // Event 2: Another peer join
    let peer2 = PeerIdentity::new("peer_2".to_string(), &engine);
    agent.join_peer(peer2, &engine);
    let root2 = agent.get_current_root().id().to_string();
    roots.push(root2.clone());

    // Event 3: Epoch advance
    agent.advance_epoch(&engine);
    let root3 = agent.get_current_root().id().to_string();
    roots.push(root3);

    // ============================================================
    // VERIFICATION: Causal chain properties
    // ============================================================
    println!("  Verifying causal chain properties...");

    // Each event must change the root (causality)
    for i in 1..roots.len() {
        assert_ne!(
            roots[i],
            roots[i - 1],
            "FALSIFIED: Event {} did not change network state.\n  \
             Causality broken - event had no effect.",
            i
        );
    }

    println!("    ✓ All {} events changed network state", roots.len() - 1);

    // All roots must be unique (no cycles)
    let unique_roots: std::collections::HashSet<_> = roots.iter().collect();
    assert_eq!(
        unique_roots.len(),
        roots.len(),
        "FALSIFIED: Causal chain contains cycles.\n  \
         Found duplicate roots in event sequence."
    );

    println!("    ✓ All roots unique (no cycles)");

    // ============================================================
    // REPORTING
    // ============================================================
    println!("\nHypothesis sustained.");
    println!("  Causal chain integrity verified:");
    println!("    {} events processed", roots.len() - 1);
    println!("    {} unique state transitions", unique_roots.len() - 1);
    println!("  Network events maintain strict causality");
}

/// Falsification Test: Peer Identity Determinism
///
/// Hypothesis: Peer identities are deterministic distinctions derived
/// from peer IDs. Same ID always produces same distinction.
///
/// Falsifies if: Same peer ID produces different distinctions across
/// different network agents or engine instances.
#[test]
fn test_falsify_peer_identity_non_determinism() {
    // ============================================================
    // SETUP
    // ============================================================
    println!("\nTest: Peer Identity Non-Determinism Falsification");
    println!("  Testing deterministic peer canonicalization...");

    let engine1 = Arc::new(DistinctionEngine::new());
    let engine2 = Arc::new(DistinctionEngine::new());

    // ============================================================
    // CROSS-ENGINE DETERMINISM
    // ============================================================
    println!("  Testing peer identity across engines...");

    let peer_id = "validator_node_42";

    let peer1 = PeerIdentity::new(peer_id.to_string(), &engine1);
    let peer2 = PeerIdentity::new(peer_id.to_string(), &engine2);

    assert_eq!(
        peer1.distinction_id(),
        peer2.distinction_id(),
        "FALSIFIED: Peer identity non-determinism across engines.\n  \
         Same peer ID produced different distinctions."
    );

    println!("    ✓ Same peer ID → same distinction across engines");

    // ============================================================
    // CROSS-AGENT DETERMINISM
    // ============================================================
    println!("  Testing peer identity across agents...");

    let agent1 = NetworkAgent::new(&engine1);
    let agent2 = NetworkAgent::new(&engine1);

    let peer1_agent1 = PeerIdentity::new("node_alpha".to_string(), &engine1);
    let peer1_agent2 = PeerIdentity::new("node_alpha".to_string(), &engine1);

    assert_eq!(
        peer1_agent1.distinction_id(),
        peer1_agent2.distinction_id(),
        "FALSIFIED: Peer identity non-determinism across agents.\n  \
         Same peer ID produced different distinctions."
    );

    // Verify agents recognize same peer
    assert_eq!(agent1.validator_count(), 0);
    assert_eq!(agent2.validator_count(), 0);

    println!("    ✓ Same peer ID → same distinction across agents");

    // ============================================================
    // REPORTING
    // ============================================================
    println!("\nHypothesis sustained.");
    println!("  Peer identity determinism verified:");
    println!("    Cross-engine consistency: ✓");
    println!("    Cross-agent consistency: ✓");
    println!("  Peer identities are structurally deterministic");
}