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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
//! WebSocket client for connecting to the makima server.
use std::sync::Arc;
use std::time::Duration;
use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
use futures::{SinkExt, StreamExt};
use tokio::sync::{mpsc, RwLock};
use tokio_tungstenite::{connect_async, tungstenite::{client::IntoClientRequest, Message}};
use uuid::Uuid;
use super::protocol::{DaemonCommand, DaemonMessage};
use crate::daemon::config::ServerConfig;
use crate::daemon::error::{DaemonError, Result};
/// WebSocket client state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
/// Not connected to server.
Disconnected,
/// Currently connecting.
Connecting,
/// Connected and authenticated.
Connected,
/// Connection failed, will retry.
Reconnecting,
/// Permanently failed (e.g., auth failure).
Failed,
}
/// WebSocket client for daemon-server communication.
pub struct WsClient {
config: ServerConfig,
machine_id: String,
hostname: String,
max_concurrent_tasks: i32,
state: Arc<RwLock<ConnectionState>>,
daemon_id: Arc<RwLock<Option<Uuid>>>,
/// Channel to receive messages to send to server.
outgoing_rx: mpsc::Receiver<DaemonMessage>,
/// Sender for outgoing messages (clone this to send messages).
outgoing_tx: mpsc::Sender<DaemonMessage>,
/// Channel to send received commands to the task manager.
incoming_tx: mpsc::Sender<DaemonCommand>,
}
impl WsClient {
/// Create a new WebSocket client.
pub fn new(
config: ServerConfig,
machine_id: String,
hostname: String,
max_concurrent_tasks: i32,
incoming_tx: mpsc::Sender<DaemonCommand>,
) -> Self {
let (outgoing_tx, outgoing_rx) = mpsc::channel(256);
Self {
config,
machine_id,
hostname,
max_concurrent_tasks,
state: Arc::new(RwLock::new(ConnectionState::Disconnected)),
daemon_id: Arc::new(RwLock::new(None)),
outgoing_rx,
outgoing_tx,
incoming_tx,
}
}
/// Get a sender for outgoing messages.
pub fn sender(&self) -> mpsc::Sender<DaemonMessage> {
self.outgoing_tx.clone()
}
/// Get current connection state.
pub async fn state(&self) -> ConnectionState {
*self.state.read().await
}
/// Get daemon ID if authenticated.
pub async fn daemon_id(&self) -> Option<Uuid> {
*self.daemon_id.read().await
}
/// Run the WebSocket client with automatic reconnection.
pub async fn run(&mut self) -> Result<()> {
let mut backoff = ExponentialBackoff {
initial_interval: Duration::from_secs(self.config.reconnect_interval_secs),
max_interval: Duration::from_secs(60),
max_elapsed_time: if self.config.max_reconnect_attempts > 0 {
Some(Duration::from_secs(
self.config.reconnect_interval_secs * self.config.max_reconnect_attempts as u64 * 10,
))
} else {
None // Infinite retries
},
..Default::default()
};
loop {
*self.state.write().await = ConnectionState::Connecting;
tracing::info!("Connecting to server: {}", self.config.url);
match self.connect_and_run().await {
Ok(()) => {
// Clean shutdown
tracing::info!("WebSocket connection closed cleanly");
break;
}
Err(DaemonError::AuthFailed(msg)) => {
tracing::error!("Authentication failed: {}", msg);
*self.state.write().await = ConnectionState::Failed;
return Err(DaemonError::AuthFailed(msg));
}
Err(e) => {
tracing::warn!("Connection error: {}", e);
*self.state.write().await = ConnectionState::Reconnecting;
if let Some(delay) = backoff.next_backoff() {
tracing::info!("Reconnecting in {:?}...", delay);
tokio::time::sleep(delay).await;
} else {
tracing::error!("Max reconnection attempts reached");
*self.state.write().await = ConnectionState::Failed;
return Err(DaemonError::ConnectionLost);
}
}
}
}
Ok(())
}
/// Connect to server and run the message loop.
async fn connect_and_run(&mut self) -> Result<()> {
// Build WebSocket URL
let ws_url = format!("{}/api/v1/mesh/daemons/connect", self.config.url);
tracing::debug!("Connecting to WebSocket: {}", ws_url);
// Build request with API key header
let mut request = ws_url.into_client_request()?;
request.headers_mut().insert(
"x-makima-api-key",
self.config.api_key.parse().map_err(|_| {
DaemonError::AuthFailed("Invalid API key format".into())
})?,
);
// Connect with API key in headers
let (ws_stream, _response) = connect_async(request).await?;
let (mut write, mut read) = ws_stream.split();
// Send daemon info after connection (server authenticated us via header)
let info_msg = DaemonMessage::authenticate(
&self.config.api_key,
&self.machine_id,
&self.hostname,
self.max_concurrent_tasks,
);
let info_json = serde_json::to_string(&info_msg)?;
write.send(Message::Text(info_json)).await?;
// Wait for authentication response
let auth_response = read
.next()
.await
.ok_or(DaemonError::ConnectionLost)??;
let auth_text = match auth_response {
Message::Text(text) => text,
Message::Close(_) => return Err(DaemonError::ConnectionLost),
_ => return Err(DaemonError::AuthFailed("Unexpected response type".into())),
};
let command: DaemonCommand = serde_json::from_str(&auth_text)?;
match command {
DaemonCommand::Authenticated { daemon_id } => {
tracing::info!("Authenticated with daemon ID: {}", daemon_id);
*self.daemon_id.write().await = Some(daemon_id);
*self.state.write().await = ConnectionState::Connected;
// Send daemon directories info to server
let working_directory = std::env::current_dir()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|_| ".".to_string());
let home_directory = dirs::home_dir()
.map(|h| h.join(".makima").join("home"))
.unwrap_or_else(|| std::path::PathBuf::from("~/.makima/home"));
// Create home directory if it doesn't exist
if let Err(e) = std::fs::create_dir_all(&home_directory) {
tracing::warn!("Failed to create home directory {:?}: {}", home_directory, e);
}
let home_directory_str = home_directory.to_string_lossy().to_string();
let worktrees_directory = dirs::home_dir()
.map(|h| h.join(".makima").join("worktrees").to_string_lossy().to_string())
.unwrap_or_else(|| "~/.makima/worktrees".to_string());
let dirs_msg = DaemonMessage::DaemonDirectories {
working_directory,
home_directory: home_directory_str,
worktrees_directory,
};
let dirs_json = serde_json::to_string(&dirs_msg)?;
write.send(Message::Text(dirs_json)).await?;
tracing::info!("Sent daemon directories info to server");
}
DaemonCommand::Error { code, message } => {
return Err(DaemonError::AuthFailed(format!("{}: {}", code, message)));
}
_ => {
return Err(DaemonError::AuthFailed(
"Unexpected response to authentication".into(),
));
}
}
// Start main message loop
let heartbeat_interval = Duration::from_secs(self.config.heartbeat_interval_secs);
let mut heartbeat_timer = tokio::time::interval(heartbeat_interval);
loop {
tokio::select! {
// Handle incoming server commands
msg = read.next() => {
match msg {
Some(Ok(Message::Text(text))) => {
tracing::info!("Received WebSocket message: {} bytes", text.len());
match serde_json::from_str::<DaemonCommand>(&text) {
Ok(command) => {
tracing::info!("Parsed command: {:?}", command);
tracing::info!("Sending command to task manager channel...");
if self.incoming_tx.send(command).await.is_err() {
tracing::warn!("Command receiver dropped, shutting down");
break;
}
tracing::info!("Command sent to task manager successfully");
}
Err(e) => {
tracing::warn!("Failed to parse server message: {}", e);
tracing::debug!("Raw message: {}", text);
}
}
}
Some(Ok(Message::Ping(data))) => {
write.send(Message::Pong(data)).await?;
}
Some(Ok(Message::Close(_))) | None => {
tracing::info!("Server closed connection");
return Err(DaemonError::ConnectionLost);
}
Some(Err(e)) => {
tracing::warn!("WebSocket error: {}", e);
return Err(e.into());
}
_ => {}
}
}
// Handle outgoing messages
msg = self.outgoing_rx.recv() => {
match msg {
Some(message) => {
let json = serde_json::to_string(&message)?;
tracing::trace!("Sending message: {}", json);
write.send(Message::Text(json)).await?;
}
None => {
// Sender dropped, shutdown
tracing::info!("Outgoing channel closed, shutting down");
break;
}
}
}
// Send heartbeat
_ = heartbeat_timer.tick() => {
// Get active task IDs from task manager
// For now, send empty list - will be connected to task manager
let heartbeat = DaemonMessage::heartbeat(vec![]);
let json = serde_json::to_string(&heartbeat)?;
write.send(Message::Text(json)).await?;
}
}
}
Ok(())
}
}
|