This documents the console-to-console communication feature that allows multiple KAI console instances to communicate over a network.
The networking functionality is integrated directly into the base Console class, allowing any KAI console to:
- Start a network server to accept connections
- Connect to other console instances
- Send commands to specific peers or broadcast to all
- Execute commands remotely with real-time results
- Maintain cross-language compatibility (Pi ↔ Rho)
# Make the demo script executable
chmod +x demo_console_communication.sh
# Run the tmux-based demo
./demo_console_communication.shThe demo will automatically:
- Create a tmux session with two console panes
- Set up networking on both consoles
- Establish connection between them
- Demonstrate various networking commands
- Show cross-language communication
- Provide interactive panes for manual testing
./consolepi> /network start 14600
pi> 2 3 +
pi> stack
./console pi> /network start 14601
pi> /connect localhost 14600
pi> /@0 10 *
pi> /broadcast stack
pi> /peers
| Command | Description | Example |
|---|---|---|
/network start [port] |
Start networking server | /network start 14600 |
/network stop |
Stop networking | /network stop |
/network status |
Show network status | /network status |
/connect <host> <port> |
Connect to peer | /connect localhost 14600 |
/peers |
List connected peers | /peers |
/broadcast <command> |
Send to all peers | /broadcast 2 3 + |
/@<peer> <command> |
Send to specific peer | /@0 stack |
/nethistory |
Show message history | /nethistory |
help network |
Show network help | help network |
Run the comprehensive test suite:
# Build and run all tests
make test
# Run specific networking tests
ctest -R ConsoleNetworking
# Run with verbose output
ctest -R ConsoleNetworking -VThe main test file is located at:
Test/Console/TestConsoleNetworking.cpp
- BasicNetworkSetup - Network initialization and peer connection
- SendCommandToPeer - Direct command execution on remote console
- BroadcastCommand - Broadcasting commands to all connected peers
- CrossLanguageCommunication - Pi/Rho language interoperability
- NetworkErrorHandling - Error conditions and edge cases
- MessageHistory - Network message logging and retrieval
- NetworkStartStop - Dynamic network enable/disable
- CompleteWorkflow - Full integration test scenario
The networking is integrated into the base Console class through:
- ENet Integration: Uses existing ENet adapter for reliable P2P communication
- Thread Safety: Separate network message processing thread
- Message Protocol: Custom message types for commands, results, broadcasts
- Language Support: Commands execute in sender's language context
enum class NetworkMessageType : ENet::MessageID {
CONSOLE_COMMAND = ENet::ID_USER_PACKET_ENUM + 10,
CONSOLE_RESULT = ENet::ID_USER_PACKET_ENUM + 11,
CONSOLE_BROADCAST = ENet::ID_USER_PACKET_ENUM + 12,
CONSOLE_LANGUAGE_SWITCH = ENet::ID_USER_PACKET_ENUM + 13
};- NetworkConsoleMessage: Message structure for history tracking
- ProcessNetworkCommand(): Network command parser and dispatcher
- HandleNetworkPacket(): Incoming message handler
- Peer Management: Connection tracking and addressing
- Peer Executor Map: Every connected system is allocated its own executor so remote commands run with an isolated data stack. The console synchronises the shared executor only when the caller already has stack items or when a language switch requires it.
Console 1:
pi> /network start 14600
pi> 10 20 +
pi> stack
30
Console 2:
pi> /network start 14601
pi> /connect localhost 14600
pi> /@0 5 *
<- [Console-1234] Result: 150
Console 1 (Pi mode):
pi> /network start 14600
pi> 42
Console 2 (Rho mode):
pi> rho
rho> /network start 14601
rho> /connect localhost 14600
rho> /@0 dup *
<- [Console-1234] Result: 1764
Console 2:
pi> /broadcast clear
>> [BROADCAST] clear
<- [Console-1234] Result:
<- [Console-5678] Result:
Thanks to the in-memory ENet implementation, it is trivial to host multiple
console peers inside the same process. The MultiPeerBroadcast unit test
connects three consoles and confirms that a broadcast updates each remote stack
while leaving the sender's stack untouched.
Include/KAI/Console/Console.h- Main console class with networking
Source/Library/Executor/Source/Console.cpp- Network implementation
Test/Console/TestConsoleNetworking.cpp- Comprehensive test suite (per-peer execution, broadcasts, disconnect handling)
demo_console_communication.sh- Interactive tmux democonsole_demo.md- Usage documentation
- Port Management: Each console needs a unique port for networking
- Peer Addressing: Use IP:port or peer index (0, 1, 2...) to address peers
- Language Context: Commands execute in the sender's language mode
- Error Handling: Network errors are reported with colored output
- History Tracking: All network messages are logged with timestamps
- Thread Safety: Network operations are thread-safe with proper synchronization
TestConsole --gtest_filter=ConsoleNetworkingTest.* covers:
- SendCommandToPeer – validates isolated peer executors and result logging.
- MultiPeerBroadcast – exercises fan-out to multiple peers.
- PeerDisconnectCleanup – ensures executor and history state is reclaimed when a peer disconnects.
- ResultHistoryNormalization – confirms stack dumps are normalised before entering history.
Potential improvements for the networking system:
- Peer discovery and auto-connection
- Secure authentication between peers
- File transfer capabilities
- Distributed variable/object sharing
- Load balancing for computational tasks
- Network topology visualization
For more information, see the comprehensive test cases and demo script included in this repository.