Expand description
Input Fairness Guard (bd-1rz0.17)
Prevents resize scheduling from starving input/keyboard events by monitoring event latencies and intervening when fairness thresholds are violated.
§Design Philosophy
In a responsive TUI, keyboard input must feel instantaneous. Even during rapid resize sequences (e.g., user dragging terminal corner), keystrokes should be processed without noticeable delay. This module enforces that guarantee.
§Mathematical Model
§Jain’s Fairness Index
We track fairness across event types using Jain’s fairness index:
F(x₁..xₙ) = (Σxᵢ)² / (n × Σxᵢ²)When applied to processing time allocations:
- F = 1.0: Perfect fairness (equal allocation)
- F = 1/n: Maximal unfairness (all time to one type)
We maintain F ≥ fairness_threshold (default 0.5 for two event types).
§Starvation Detection
Input starvation is detected when:
- Input latency exceeds
max_input_latency, OR - Consecutive resize-dominated cycles exceed
dominance_threshold
§Intervention
When starvation is detected:
- Force resize coalescer to yield (return
ApplyNowinstead ofShowPlaceholder) - Log the intervention with evidence
- Reset dominance counter
§Invariants
-
Bounded Input Latency: Input events are processed within
max_input_latencyfrom their arrival time, guaranteed by intervention mechanism. -
Work Conservation: The guard never blocks event processing; it only changes priority ordering between event types.
-
Monotonic Time: All timestamps use
Instant(monotonic) to prevent clock drift from causing priority inversions.
§Failure Modes
| Condition | Behavior | Rationale |
|---|---|---|
| Clock drift | Use monotonic Instant | Prevent priority inversion |
| Resize storm | Force input processing | Bounded latency guarantee |
| Input flood | Yield to BatchController | Not our concern; batch handles it |
| Zero events | Return default (fair) | Safe default, no intervention |
Structs§
- Fairness
Config - Configuration for input fairness.
- Fairness
Decision - Fairness decision returned by the guard.
- Fairness
LogEntry - Fairness log entry for telemetry.
- Fairness
Stats - Statistics about fairness scheduling.
- Input
Fairness Guard - Guard for input fairness scheduling.
- Intervention
Counts - Counts of interventions by type.
Enums§
- Event
Type - Event type for fairness classification.
- Intervention
Reason - Intervention reason for fairness.
Type Aliases§
- Fairness
Event Type - Type alias for compatibility with program.rs