Protocol Overview
Undying Terminal’s recovery protocol ensures zero data loss during network disruptions using a sequence-based buffering system. Every byte of terminal output is numbered, buffered, and can be replayed on reconnection.Sequence Numbers
What Are Sequence Numbers?
Each packet exchanged between client and terminal has a monotonically increasing sequence number:- 64-bit unsigned integers (18 quintillion possible values)
- Never reset during session lifetime
- Increment by 1 for each message
- Separate counters for client→terminal and terminal→client
Sequence Tracking
Terminal maintains:last_sent_seq: Last sequence number sent to clientlast_acked_seq: Last sequence number client confirmed receiving
last_received_seq: Last sequence number received from terminalnext_expected_seq: Next sequence number expected
Recovery Buffer
Buffer Architecture
The server maintains a circular buffer for each terminal session:- Fixed size (configurable in server config)
- Circular: Old data overwritten when full
- Per-session: Each terminal has its own buffer
- In-memory: Not persisted to disk
Buffer Sizing
Default: 64MB| Use Case | Recommended Size | Reasoning |
|---|---|---|
| Interactive shells | 16-32MB | Minimal output, long retention |
| Development work | 64MB (default) | Moderate compiler output |
| CI/CD runners | 128-256MB | Heavy log output |
| Streaming logs | 512MB+ | Continuous high-volume data |
Buffer Capacity Examples
64MB buffer can hold:Reconnection Flow
Normal Reconnection
Visual timeline:
Buffer Overflow Scenario
What happens when buffer fills during disconnect:
User experience:
- Clear indication of data loss
- Session still functional
- New data appears normally
- Gap is a one-time warning, not ongoing issue
Keepalive Mechanism
Why Keepalives?
Network devices (routers, firewalls) close “idle” TCP connections:| Device Type | Typical Timeout |
|---|---|
| Home routers | 5-10 minutes |
| Corporate firewalls | 2-5 minutes |
| Public WiFi | 1-3 minutes |
| Mobile carriers | 30-90 seconds |
How It Works
Default: 5-second keepalive intervalConfiguration
Server-side (ut.cfg):- Clients automatically respond to keepalives
- No configuration needed
- Keepalives are transparent to user
Keepalive Overhead
Network usage:- Negligible (less than 0.1% per session)
- Simple timestamp check and small packet send
Data Integrity
Checksums
Every packet includes a CRC32 checksum:- Calculate CRC32 of received data
- Compare with packet’s CRC32
- If mismatch: Discard packet, request retransmission
- Bit flips during transmission
- Memory corruption
- Network equipment errors
Out-of-Order Delivery
TCP guarantees in-order delivery, so Undying Terminal assumes packets arrive in sequence order. However, on reconnection: Scenario: Client reconnects during active transmission- Receive packet with seq 1238
- Expected seq 1236, so buffer 1238
- Receive 1236, deliver to terminal
- Receive 1237, deliver to terminal
- Check buffer: 1238 is now next, deliver it
Duplicate Suppression
Scenario: Retransmission due to network glitchProtocol Efficiency
Overhead Analysis
Per-packet overhead:| Component | Size | Purpose |
|---|---|---|
| Sequence number | 8 bytes | Recovery tracking |
| Length | 4 bytes | Packet framing |
| CRC32 | 4 bytes | Integrity check |
| Total | 16 bytes | Per packet |
| Payload | Overhead | Efficiency |
|---|---|---|
| 10 bytes (keystroke) | 16 bytes | 38% |
| 100 bytes (line) | 16 bytes | 86% |
| 1KB (screen) | 16 bytes | 98.4% |
| 64KB (max) | 16 bytes | 99.975% |
Compression
Current status: Not implemented Potential benefit:| Content Type | Typical Compression Ratio |
|---|---|
| Text output | 2-3× smaller |
| Log files | 3-5× smaller |
| Binary data | 1.1× (minimal) |
| Already compressed | 1× (no benefit) |
- CPU cost: ~5-10% overhead for compression/decompression
- Latency cost: +1-2ms for compression
- Bandwidth savings: 50-70% for text-heavy workloads
Advanced Scenarios
Session Cloning
Can you clone a buffer to a new client? Currently not supported, but architecturally possible:- Training: Instructor shares live session with students
- Debugging: Observer connects to user’s session mid-problem
- Monitoring: Audit trail of session activity
Multi-Path Redundancy
Can you connect over multiple networks simultaneously? Not currently supported, but theoretically possible:Comparison to Other Recovery Protocols
| Protocol | Approach | Data Loss | Complexity |
|---|---|---|---|
| Undying Terminal | Sequence + buffer | Zero (if buffer holds) | Low |
| Mosh | Speculative execution | Zero (via prediction) | Medium |
| EternalTerminal | SSH tunneling | Zero (via SSH) | High |
| Screen/tmux | Session detach | Zero (local only) | Low |
| SSH + autossh | Reconnection only | High (no buffering) | Medium |
- Mosh: Predicts what you’ll type (works offline)
- Undying Terminal: Buffers what actually happened (perfect replay)
- SSH: Gives up on disconnect (no recovery)
Next Steps
- Understand Persistent Sessions for context
- Learn about Encryption for securing recovery data
- Review Architecture Overview for system design