ws_service_exec.rs:handle_ws (line 239) has #[allow(clippy::cognitive_complexity)]. The function is ~140 lines handling a PTY-backed exec session for running commands inside a service container.
What it does
handle_ws(WebSocket, AppState, project, name, service, container_id, session_id) — WebSocket handler for interactive shell/exec sessions into a service container. If a session_id is provided and active, reconnects to that session. Otherwise creates a new session: resolves the inner container, opens a PTY via docker exec, stores a PtySession in state, spawns a reader task that pushes PTY output into a scrollback ring buffer + broadcast, sends an init message with the new session_id, and runs the bridge loop.
What to do
1. Extract session creation
Lines 260–332 set up a brand-new session: generate UUID, resolve inner container, open PTY via spawn_blocking, dup the fd, build the PtySession, insert into state. Extract:
async fn create_pty_session(
state: &AppState,
docker: &bollard::Docker,
container_id: &str,
project: &str,
service: &str,
composite_key: String,
socket: &mut WebSocket,
) -> Option<(String, Arc<Mutex<VecDeque<u8>>>, broadcast::Sender<Vec<u8>>, i32, i32)>
// returns (session_id, scrollback, output_tx, read_fd, write_fd) on success
2. Extract PTY reader task spawn
Lines 334–366 spawn the background task that reads from the PTY master fd into the scrollback ring + broadcast. Extract:
fn spawn_pty_reader_task(
state: Arc<AppState>,
sid: String,
read_fd: i32,
scrollback: Arc<Mutex<VecDeque<u8>>>,
output_tx: broadcast::Sender<Vec<u8>>,
)
3. Extract error-and-return helper
The pattern let _ = socket.send(Message::Text(...)).await; return; appears 5+ times for different error paths. Extract:
async fn send_error_and_return(
socket: &mut WebSocket,
message: impl Into<String>,
)
4. Add unit tests
Unit testing this handler directly is hard (requires Docker + PTY). Focus on:
send_error_and_return — called via a mock socket to verify message format (if feasible)
- Any pure helper functions that come out of the extraction
Important
The workspace Cargo.toml sets cognitive_complexity, too_many_lines, too_many_arguments, and several other clippy lints to warn globally. Run make lint after every change and make sure the output is clean. Don't fix a suppression here only to create new warnings elsewhere.
Acceptance criteria
Related
Tracking issue pattern from previous PRs: #122, #133, #157, #158, #171, #172, #173, #174, #175
ws_service_exec.rs:handle_ws(line 239) has#[allow(clippy::cognitive_complexity)]. The function is ~140 lines handling a PTY-backed exec session for running commands inside a service container.What it does
handle_ws(WebSocket, AppState, project, name, service, container_id, session_id)— WebSocket handler for interactive shell/exec sessions into a service container. If asession_idis provided and active, reconnects to that session. Otherwise creates a new session: resolves the inner container, opens a PTY viadocker exec, stores aPtySessionin state, spawns a reader task that pushes PTY output into a scrollback ring buffer + broadcast, sends an init message with the new session_id, and runs the bridge loop.What to do
1. Extract session creation
Lines 260–332 set up a brand-new session: generate UUID, resolve inner container, open PTY via
spawn_blocking, dup the fd, build thePtySession, insert into state. Extract:2. Extract PTY reader task spawn
Lines 334–366 spawn the background task that reads from the PTY master fd into the scrollback ring + broadcast. Extract:
3. Extract error-and-return helper
The pattern
let _ = socket.send(Message::Text(...)).await; return;appears 5+ times for different error paths. Extract:4. Add unit tests
Unit testing this handler directly is hard (requires Docker + PTY). Focus on:
send_error_and_return— called via a mock socket to verify message format (if feasible)Important
The workspace
Cargo.tomlsetscognitive_complexity,too_many_lines,too_many_arguments, and several other clippy lints towarnglobally. Runmake lintafter every change and make sure the output is clean. Don't fix a suppression here only to create new warnings elsewhere.Acceptance criteria
create_pty_sessionextractedspawn_pty_reader_taskextractedsend_error_and_returnextracted to deduplicate error pathshandle_wsuses the new helpersmake test)make lintpasses with zero new warnings#[allow(clippy::cognitive_complexity)]ifmake lintpasses without it, keep if not (note why)Related
Tracking issue pattern from previous PRs: #122, #133, #157, #158, #171, #172, #173, #174, #175