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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use anyhow::Result;
use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::daemon_types::{checked_frame_len, BannerData, Request, Response};
const SOCKET_NAME: &str = "fabd.sock";
fn socket_path() -> Result<std::path::PathBuf> {
Ok(crate::state::get_data_dir()?.join(SOCKET_NAME))
}
fn send_and_recv(stream: &mut UnixStream, request: &Request) -> Result<Response> {
use std::io::{Read, Write};
// Length-prefixed JSON: 4-byte LE length, then payload.
let req_bytes = serde_json::to_vec(request)?;
let req_len = checked_frame_len(req_bytes.len())
.ok_or_else(|| anyhow::anyhow!("IPC request exceeds the maximum frame size"))?;
let mut combined = Vec::with_capacity(4 + req_bytes.len());
let len_bytes = req_len.to_le_bytes();
combined.extend_from_slice(&len_bytes);
combined.extend_from_slice(&req_bytes);
let t0 = std::time::Instant::now();
stream.write_all(&combined)?;
let t_write = std::time::Instant::now();
// NOTE: previously called stream.shutdown(Shutdown::Write) here, but
// measuring shows it adds 1–78 ms of latency on Linux Unix sockets
// because the kernel needs to deliver a FIN to the peer and reschedule
// the client. The daemon's length-prefixed protocol already tells the
// server when the request ends (it reads exactly req_len bytes), so
// the shutdown is unnecessary and we skip it for latency.
let t_shutdown = std::time::Instant::now();
let mut len_bytes = [0u8; 4];
stream.read_exact(&mut len_bytes)?;
let t_read4 = std::time::Instant::now();
let resp_len = u32::from_le_bytes(len_bytes) as usize;
if checked_frame_len(resp_len).is_none() {
return Err(anyhow::anyhow!(
"IPC response exceeds the maximum frame size"
));
}
let mut resp_bytes = vec![0u8; resp_len];
stream.read_exact(&mut resp_bytes)?;
let t_read_payload = std::time::Instant::now();
let response: Response = serde_json::from_slice(&resp_bytes)?;
let t_deser = std::time::Instant::now();
if std::env::var("FAB_PROFILE").is_ok() {
eprintln!(
"[FAB_PROFILE] ipc: write={:?} shutdown={:?} read4={:?} read_payload={:?} deser={:?} total={:?}",
t_write - t0,
t_shutdown - t_write,
t_read4 - t_shutdown,
t_read_payload - t_read4,
t_deser - t_read_payload,
t_deser - t0
);
}
Ok(response)
}
/// Try to get cached banner data from daemon.
/// Auto-starts daemon if socket doesn't exist or is stale.
pub fn get_banner_cached(path: &Path) -> Option<BannerData> {
use crate::cmd::banner_data_cache;
let t0 = std::time::Instant::now();
// Fast path: read the per-path on-disk cache directly, bypassing
// the IPC round-trip entirely. The daemon writes this file on every
// successful banner compute; the file's mtime is the freshness
// signal. The disk read is <0.1 ms (page cache) versus 1–10 ms for
// the IPC read4 (kernel scheduling).
if banner_data_cache::is_cache_fresh(path) {
if let Some(data) = banner_data_cache::read_cache(path) {
if std::env::var("FAB_PROFILE").is_ok() {
eprintln!(
"[FAB_PROFILE] banner_data_cache: hit total={:?}",
t0.elapsed()
);
}
return Some(data);
}
}
let socket = socket_path().ok()?;
// Try connecting — if it fails, start daemon and poll for readiness.
let mut stream = match UnixStream::connect(&socket) {
Ok(s) => s,
Err(_) => {
// Nobody listening behind the file — stale, safe to remove.
let _ = std::fs::remove_file(&socket);
ensure_daemon_running();
// Poll for the listener (up to 2s) instead of a single fixed
// 50ms sleep: a cold daemon can take longer to bind, and the
// old code silently fell back to a local scan.
let mut connected = None;
for _ in 0..40 {
match UnixStream::connect(&socket) {
Ok(s) => {
connected = Some(s);
break;
}
Err(_) => std::thread::sleep(Duration::from_millis(50)),
}
}
connected?
}
};
let t1 = std::time::Instant::now();
// Banner read timeout: keep it short so an unresponsive daemon falls
// back to a direct local scan quickly (DECIDE 2: short ping 2s +
// direct-scan fallback). Cache hits return in ~1-10ms; a hung daemon
// that doesn't respond within 2s would otherwise stall the prompt for
// up to 30s before `run_banner` could fall through. Large cold scans
// that legitimately take >3s will also fallback to a direct scan —
// the daemon still computes in the background and populates the
// on-disk cache for the next invocation, so the result is correct
// either way.
stream.set_read_timeout(Some(Duration::from_secs(3))).ok()?;
stream
.set_write_timeout(Some(Duration::from_secs(1)))
.ok()?;
let request = Request::Banner {
path: path.to_path_buf(),
};
let result = send_and_recv(&mut stream, &request);
let response = match result {
Ok(r) => r,
Err(e) => {
// Timeout / disconnect / malformed response — fall back to
// a direct scan in the caller (`run_banner` and
// `navigate_by_number` both treat `None` as "daemon
// unavailable, scan directly"). This is the rare path
// where the socket connected but the daemon never answered
// (the `connect`-failed path already falls back via the
// `connected?` early return above).
tracing::debug!(
"daemon Banner request failed, falling back to direct scan: {}",
e
);
return None;
}
};
let t2 = std::time::Instant::now();
if std::env::var("FAB_PROFILE").is_ok() {
eprintln!(
"[FAB_PROFILE] ipc: connect={:?} send_recv={:?} total={:?}",
t1 - t0,
t2 - t1,
t2 - t0,
);
}
match response {
Response::Banner(data) => Some(*data),
_ => None,
}
}
/// Check if daemon is running. Cleans up stale sockets automatically.
pub fn is_daemon_running() -> bool {
let Ok(socket) = socket_path() else {
return false;
};
if !socket.exists() {
return false;
}
let mut stream = match UnixStream::connect(&socket) {
Ok(s) => s,
Err(_) => {
// Socket file exists but nobody is listening — remove stale socket
let _ = std::fs::remove_file(&socket);
return false;
}
};
stream.set_read_timeout(Some(Duration::from_secs(2))).ok();
stream.set_write_timeout(Some(Duration::from_secs(1))).ok();
let request = Request::Ping;
match send_and_recv(&mut stream, &request) {
Ok(Response::Pong) => true,
_ => {
// Unresponsive daemon. NOTE: do NOT unlink the socket here — a
// live-but-busy daemon may simply be slow to answer (we use a
// 2s ping timeout instead of 10s so a dead daemon doesn't
// stall every `f` invocation). Unlinking a live daemon's
// socket orphans it and lets a second daemon start
// (two daemons writing caches concurrently).
false
}
}
}
/// Send warm requests before the CLI exits.
/// Each warm request uses its own short-lived connection because daemon request
/// handlers process one request per accepted stream.
pub fn warm_paths(paths: &[PathBuf]) {
for path in paths {
warm_path(path);
}
}
fn warm_path(path: &Path) {
use std::io::Write;
let Ok(socket) = socket_path() else {
return;
};
let Ok(mut stream) = UnixStream::connect(&socket) else {
return;
};
stream.set_write_timeout(Some(Duration::from_secs(1))).ok();
let request = Request::Warm {
path: path.to_path_buf(),
};
let req_bytes = match serde_json::to_vec(&request) {
Ok(b) => b,
Err(e) => {
tracing::warn!("Failed to serialize warm request: {}", e);
return;
}
};
let Some(req_len) = checked_frame_len(req_bytes.len()) else {
tracing::warn!("Warm request exceeds the maximum IPC frame size");
return;
};
let mut combined = Vec::with_capacity(4 + req_bytes.len());
combined.extend_from_slice(&req_len.to_le_bytes());
combined.extend_from_slice(&req_bytes);
if let Err(e) = stream.write_all(&combined) {
tracing::warn!("Failed to send warm request: {}", e);
return;
}
if let Err(e) = stream.flush() {
tracing::warn!("Failed to flush warm request: {}", e);
}
}
/// Send shutdown signal to daemon
pub fn send_shutdown() {
use std::io::Write;
let Ok(socket) = socket_path() else {
return;
};
let Ok(mut stream) = UnixStream::connect(&socket) else {
return;
};
stream.set_write_timeout(Some(Duration::from_secs(1))).ok();
let request = Request::Shutdown;
let req_bytes = match serde_json::to_vec(&request) {
Ok(bytes) => bytes,
Err(e) => {
tracing::warn!("Failed to serialize shutdown request: {}", e);
return;
}
};
let Some(req_len) = checked_frame_len(req_bytes.len()) else {
tracing::warn!("Shutdown request exceeds the maximum IPC frame size");
return;
};
let mut combined = Vec::with_capacity(4 + req_bytes.len());
combined.extend_from_slice(&req_len.to_le_bytes());
combined.extend_from_slice(&req_bytes);
if let Err(e) = stream.write_all(&combined) {
tracing::warn!("Failed to send shutdown request: {}", e);
}
}
/// Start daemon in background (auto-start)
pub fn ensure_daemon_running() {
if is_daemon_running() {
return;
}
let Ok(exe) = std::env::current_exe() else {
return;
};
let Some(parent) = exe.parent() else {
tracing::warn!("Cannot determine parent directory of executable");
return;
};
let daemon_bin = parent.join("fabd");
if !daemon_bin.exists() {
tracing::warn!("fabd binary not found at {}", daemon_bin.display());
return;
}
// Clean up a stale socket before spawning. `is_daemon_running` deliberately
// treats an unresponsive daemon as potentially live; re-check the listener
// here so a slow daemon cannot be orphaned and replaced by a split-brain
// second instance.
if let Ok(socket) = socket_path() {
if socket.exists() {
if UnixStream::connect(&socket).is_ok() {
return;
}
let _ = std::fs::remove_file(&socket);
}
}
match std::process::Command::new(&daemon_bin)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
{
Ok(_) => {
tracing::info!("Started fabd daemon");
// Poll for socket to appear (up to 2s, checking every 50ms)
for _ in 0..40 {
if let Ok(socket) = socket_path() {
if socket.exists() && UnixStream::connect(&socket).is_ok() {
return;
}
}
std::thread::sleep(Duration::from_millis(50));
}
}
Err(e) => {
tracing::warn!("Failed to start fabd: {}", e);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_socket_path() {
let path = socket_path();
assert!(path.is_ok());
let path = path.unwrap();
assert!(path.to_string_lossy().contains("fabd.sock"));
}
#[test]
fn test_send_and_recv_with_mock() {
// This test verifies the function signature and basic flow
// In a real test, we'd mock the UnixStream, but for now we just test the types
let request = Request::Ping;
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("Ping"));
}
#[test]
fn test_is_daemon_running_when_not_running() {
// This test checks the function doesn't panic when daemon is not running
// It may return false, which is expected
let _result = is_daemon_running();
}
#[test]
fn test_send_shutdown_does_not_panic() {
// send_shutdown may fail if daemon is not running, but shouldn't panic
send_shutdown();
}
}