Expand description
Background runtime for Client - owns the
subprocess reader, decodes incoming frames, dispatches inbound
control_requests on detached tasks, and routes outbound
control_responses to the PendingControls map so
send_control callers can await
their typed reply.
ยงLifecycle
Client::spawnruns the init handshake inline (sends theinitializecontrol_request, drains the response, captures any pre-init Messages).- Once init completes,
spawn_reader_tasktakes ownership of theSubprocess, the dispatch handle, the events channel, and any pre-init Messages. - The reader task pre-pumps the buffered messages, then loops on
subprocess.read_line()until shutdown / EOF / I/O error. - On exit, the reader task closes the subprocess and drains
pending_controlswith an EOF error so blockedsend_controlcallers wake up.
Re-exportsยง
use std::collections::HashMap;๐use std::sync::Arc;๐use parking_lot::RwLock;๐use tokio::sync::Mutex;๐use tokio::sync::mpsc;๐use tokio::sync::oneshot;๐use tokio::task::JoinHandle;๐use crate::Error;๐use crate::client::ControlDispatchHandle;๐use crate::transport::codec::DecodedLine;๐use crate::transport::codec::decode_dispatch;๐use crate::transport::process::Subprocess;๐use forge_primitives::Message;๐
Structsยง
- Skip
Counters ๐ - Consecutive skips of unparsable lines. At
UNPARSABLE_ESCALATE_AFTERin a row the stream is not speaking stream-json, and the near-100%-malformed session would otherwise present to the user as a silent hang.
Enumsยง
- Line
Outcome ๐ - What
handle_linedecided about one decoded line.
Constantsยง
- UNPARSABLE_
ESCALATE_ ๐AFTER - Consecutive unparsable lines after which the reader escalates once to a session-visible error. A sparse bad frame still just skips.
Functionsยง
- close_
subprocess ๐ - control_
request_ ๐id request_idoflinewhen it is acontrol_request, elseNone.- drain_
pending ๐ - excerpt ๐
- First
max_charsoflinefor logs and error strings - a corrupt line can be arbitrarily large. - handle_
line ๐ - Process one decoded line. Returns
LineOutcome::Stoponly when the events channel has closed and the read loop should exit; a line that fails to decode is skipped and the loop continues. - spawn_
reader_ ๐task - Spawn the post-init reader task.
Type Aliasesยง
- Control
Outcome ๐ - Outcome of one outbound
control_request- either the success payload (the innerresponseJSON) or a typed error. - Inflight
Dispatches ๐ - In-flight inbound
control_requestdispatch tasks. Keyed byrequest_idso a subsequentcontrol_cancel_requestcan abort the matching task instead of letting the slow callback finish and write back acontrol_responsethe CLI has already moved past. - Pending
Controls ๐ - In-flight outbound
control_requests waiting on responses. - Shared
Session ๐Id - Shared session-id container - held by
Client,ControlDispatchHandle, and the reader task. Reader updates it as messages arrive; consumers read the current value.