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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Terminal constants and key sequence definitions
//!
//! NOTE: Many key sequence constants are currently unused since we switched to
//! raw byte passthrough (see issue #87), but are kept for reference and potential
//! future debugging use.
// Allow dead code for unused key sequence constants
// Buffer size constants for allocation optimization
// These values are chosen based on empirical testing and SSH protocol characteristics
/// Maximum size for terminal key sequences (ANSI escape sequences are typically 3-7 bytes)
/// Value: 8 bytes - Accommodates the longest standard ANSI sequences (F-keys: ESC[2x~)
/// Rationale: Most key sequences are 1-5 bytes, 8 provides safe headroom without waste
pub const MAX_KEY_SEQUENCE_SIZE: usize = 8;
/// Buffer size for SSH I/O operations (4KB aligns with typical SSH packet sizes)
/// Value: 4096 bytes - Matches common SSH packet fragmentation boundaries
/// Rationale: SSH protocol commonly uses 4KB packets; larger buffers reduce syscalls
/// but increase memory usage. 4KB provides optimal balance for interactive sessions.
pub const SSH_IO_BUFFER_SIZE: usize = 4096;
/// Maximum size for terminal output chunks processed at once
/// Value: 1024 bytes - Balance between responsiveness and efficiency
/// Rationale: Smaller chunks improve perceived responsiveness for interactive use,
/// while still being large enough to batch terminal escape sequences efficiently.
pub const TERMINAL_OUTPUT_CHUNK_SIZE: usize = 1024;
/// PTY message channel sizing:
/// - 256 messages capacity balances memory usage with responsiveness
/// - Each message is ~8-64 bytes (key presses/small terminal output)
/// - Total memory: ~16KB worst case, prevents unbounded growth
/// - Large enough to handle burst input/output without blocking
pub const PTY_MESSAGE_CHANNEL_SIZE: usize = 256;
/// Input polling timeout design:
/// - 500ms provides good balance between CPU usage and responsiveness
/// - Longer than async timeouts (10-100ms) since this is blocking thread
/// - Still responsive enough that users won't notice delay
/// - Reduces CPU usage compared to tight polling loops
pub const INPUT_POLL_TIMEOUT_MS: u64 = 500;
/// Task cleanup timeout design:
/// - 100ms is sufficient for tasks to receive cancellation signal and exit
/// - Short timeout prevents hanging on cleanup but allows graceful shutdown
/// - Tasks should check cancellation signal frequently (10-50ms intervals)
pub const TASK_CLEANUP_TIMEOUT_MS: u64 = 100;
/// Connection health check interval for PTY sessions
/// - 30 seconds provides periodic checks without excessive overhead
/// - Detects dead connections even when SSH keepalive is disabled
/// - Works alongside SSH-level keepalive for defense in depth
/// - Short enough to detect issues before users get frustrated
pub const CONNECTION_HEALTH_CHECK_INTERVAL_SECS: u64 = 30;
/// Maximum idle time before considering connection potentially dead
/// - 300 seconds (5 minutes) is a reasonable threshold for interactive sessions
/// - If no data received within this time, trigger a health check warning
/// - This is a secondary mechanism to SSH-level keepalive
pub const MAX_IDLE_TIME_BEFORE_WARNING_SECS: u64 = 300;
// Const arrays for frequently used key sequences to avoid repeated allocations.
/// Control key sequences - frequently used in terminal input
pub const CTRL_C_SEQUENCE: & = &; // Ctrl+C (SIGINT)
pub const CTRL_D_SEQUENCE: & = &; // Ctrl+D (EOF)
pub const CTRL_Z_SEQUENCE: & = &; // Ctrl+Z (SIGTSTP)
pub const CTRL_A_SEQUENCE: & = &; // Ctrl+A
pub const CTRL_E_SEQUENCE: & = &; // Ctrl+E
pub const CTRL_U_SEQUENCE: & = &; // Ctrl+U
pub const CTRL_K_SEQUENCE: & = &; // Ctrl+K
pub const CTRL_W_SEQUENCE: & = &; // Ctrl+W
pub const CTRL_L_SEQUENCE: & = &; // Ctrl+L
pub const CTRL_R_SEQUENCE: & = &; // Ctrl+R
/// Special keys - frequently used in terminal input
pub const ENTER_SEQUENCE: & = &; // Carriage return
pub const TAB_SEQUENCE: & = &; // Tab
pub const BACKSPACE_SEQUENCE: & = &; // DEL
pub const ESC_SEQUENCE: & = &; // ESC
/// Arrow keys - Application Cursor Keys mode (SS3 sequences)
/// ncurses applications (htop, etc.) typically expect this format when
/// DECCKM (DEC Cursor Key Mode) is enabled via \x1b[?1h
/// Most terminal emulators and applications (vim, neovim) accept both formats,
/// but ncurses strictly follows terminfo which often specifies SS3 format.
pub const UP_ARROW_SEQUENCE: & = &; // ESC O A
pub const DOWN_ARROW_SEQUENCE: & = &; // ESC O B
pub const RIGHT_ARROW_SEQUENCE: & = &; // ESC O C
pub const LEFT_ARROW_SEQUENCE: & = &; // ESC O D
/// Function keys - commonly used
pub const F1_SEQUENCE: & = &; // F1: ESC OP
pub const F2_SEQUENCE: & = &; // F2: ESC OQ
pub const F3_SEQUENCE: & = &; // F3: ESC OR
pub const F4_SEQUENCE: & = &; // F4: ESC OS
pub const F5_SEQUENCE: & = &; // F5: ESC[15~
pub const F6_SEQUENCE: & = &; // F6: ESC[17~
pub const F7_SEQUENCE: & = &; // F7: ESC[18~
pub const F8_SEQUENCE: & = &; // F8: ESC[19~
pub const F9_SEQUENCE: & = &; // F9: ESC[20~
pub const F10_SEQUENCE: & = &; // F10: ESC[21~
pub const F11_SEQUENCE: & = &; // F11: ESC[23~
pub const F12_SEQUENCE: & = &; // F12: ESC[24~
/// Other special keys
pub const HOME_SEQUENCE: & = &; // ESC[H
pub const END_SEQUENCE: & = &; // ESC[F
pub const PAGE_UP_SEQUENCE: & = &; // ESC[5~
pub const PAGE_DOWN_SEQUENCE: & = &; // ESC[6~
pub const INSERT_SEQUENCE: & = &; // ESC[2~
pub const DELETE_SEQUENCE: & = &; // ESC[3~