Expand description
Public Client - the entry point consumers hold.
Client is a cheap-clone handle (Arc-backed) over a background
reader task that owns the claude subprocess. All methods take
&self, so the same Client can be passed by reference into
multiple tasks. The events stream is returned alongside the Client
at spawn time as a ClientEvents receiver - single-consumer,
independent of the writer-side handle.
Internally:
- The reader task pulls lines from the subprocess, decodes them,
pushes regular
Messages onto an mpsc that backs the returnedClientEvents, dispatches inboundcontrol_requests on detached tasks, and routes outboundcontrol_responses to per-request oneshots soClient::send_controlcallers canawaittheir typed reply. - The writer half is a clonable
SharedWritercloned fromSubprocess; outbound writes go through it without contending on&mut self.
Re-exportsยง
pub(crate) use control_dispatch::ControlDispatchHandle;๐use std::collections::HashMap;๐use std::collections::VecDeque;๐use std::sync::Arc;๐use std::sync::atomic::AtomicBool;๐use std::sync::atomic::Ordering;๐use tokio::sync::Mutex;๐use tokio::sync::mpsc;๐use tokio::sync::oneshot;๐use tokio::task::JoinHandle;๐use tracing::debug;๐use crate::Error;๐use crate::client::runtime::PendingControls;๐use crate::client::runtime::excerpt;๐use crate::client::runtime::spawn_reader_task;๐use crate::mcp::orchestration::McpHosts;๐use crate::options::Options;๐use crate::transport::codec::DecodedLine;๐use crate::transport::codec::decode_dispatch;๐use crate::transport::process::Subprocess;๐use forge_primitives::Message;๐
Modulesยง
- control_
dispatch ๐ - Inbound
control_requestdispatch:ControlDispatchHandleroutes permission checks, MCP JSON-RPC, and hook callbacks to the appropriate handler and writes the matchingcontrol_responsevia a clonableSharedWriter. - control_
send ๐ - Outbound
control_requesttyped wrappers - the user-facing commands (interrupt,set_permission_mode,set_model,mcp_reconnect,mcp_toggle,stop_task,mcp_status,get_context_usage) plus the_rawescape hatches formcp_statusandget_context_usage. - runtime ๐
- Background runtime for
Client- owns the subprocess reader, decodes incoming frames, dispatches inboundcontrol_requests on detached tasks, and routes outboundcontrol_responses to thePendingControlsmap sosend_controlcallers canawaittheir typed reply.
Structsยง
- Client
- An active
claudebinary subprocess. - Client
Inner ๐ - Pending
Guard ๐ - Removes a
pending_controlsentry when the awaiting future is dropped without a response having been routed - the drop guardsend_controlparks in its future.
Constantsยง
- INIT_
HANDSHAKE_ TIMEOUT - Budget for the
initializehandshake insideClient::spawn. A wedged CLI (alive, stdout open, never answering) would otherwise park the caller forever with the child leaked.
Type Aliasesยง
- Client
Events - Stream of regular
Messages produced by the reader task. Returned alongsideClientfromClient::spawn. Single-consumer; the writer-sideClientisCloneand can move freely between tasks while one task owns the receiver.