nebu-ctx 0.8.9

NebuCtx runtime for the nebu-ctx self-hosted client/server product.
Documentation
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! In-process async telemetry queue for the MCP server process.
//!
//! MCP tool calls enqueue events without blocking; a single background Tokio
//! task drains the channel and POSTs each event to the configured server on a
//! threadpool thread.
//!
//! Shell hooks run as short-lived separate processes that cannot share the
//! in-process channel. They use [`fire_sync`], which caps overhead at 300 ms
//! via a detached thread and a receive-timeout, so the user's shell command
//! never stalls perceptibly even when the server is unreachable.
//!
//! Unlike the earlier best-effort-only behavior, failed telemetry delivery is
//! now written to the local sync outbox so offline sessions can be replayed.

use std::sync::OnceLock;
use std::time::Duration;

use tokio::sync::mpsc::{self, UnboundedSender};

use crate::models::TelemetryIngestRequest;

static TX: OnceLock<UnboundedSender<TelemetryIngestRequest>> = OnceLock::new();

/// Enqueue a telemetry event for background delivery.
///
/// Returns immediately — no network I/O on the calling thread.
/// Events enqueued before [`start_drain_task`] is called are persisted so they
/// can be replayed when the runtime or server becomes available.
pub fn enqueue(request: TelemetryIngestRequest) {
    if let Some(tx) = TX.get() {
        // UnboundedSender::send only errors when the receiver is dropped,
        // which cannot happen while the drain task is running.
        let _ = tx.send(request);
        return;
    }

    let _ = persist_request(&request);
}

/// Spawn the background drain task inside the running Tokio runtime.
///
/// Must be called once at MCP server startup. Subsequent calls are no-ops;
/// the first call wins and installs the channel sender into [`TX`].
pub fn start_drain_task() {
    let (tx, mut rx) = mpsc::unbounded_channel::<TelemetryIngestRequest>();

    // OnceLock::set is atomic — only the first caller proceeds.
    if TX.set(tx).is_err() {
        return;
    }

    tokio::spawn(async move {
        drain_persisted();

        while let Some(req) = rx.recv().await {
            // Offload the blocking HTTP POST to the threadpool so the async
            // runtime is never stalled by network I/O.
            tokio::task::spawn_blocking(move || {
                if deliver_request(&req).is_err() {
                    let _ = persist_request(&req);
                }
            });
        }
    });
}

/// Send a telemetry event from a short-lived process such as a shell hook.
///
/// Spawns a thread for the HTTP call and waits at most 300 ms before
/// returning so the invoking process can exit promptly. If the server is
/// unreachable the event is queued locally instead of being dropped.
pub fn fire_sync(request: TelemetryIngestRequest) {
    let (done_tx, done_rx) = std::sync::mpsc::channel::<()>();
    std::thread::spawn(move || {
        if deliver_request(&request).is_err() {
            let _ = persist_request(&request);
        }
        let _ = done_tx.send(());
    });
    let _ = done_rx.recv_timeout(Duration::from_millis(300));
}

/// Attempts to flush every queued outbox item once.
/// Returns the number of entries that were pending before the flush attempt.
pub fn flush_pending() -> usize {
    let Ok(entries) = crate::core::sync_outbox::load_entries() else {
        return 0;
    };

    let count = entries.len();
    drain_persisted();
    count
}

fn deliver_request(request: &TelemetryIngestRequest) -> anyhow::Result<()> {
    let client = crate::server_client::ServerClient::load()?;
    client.ingest_telemetry(request)
}

fn persist_request(request: &TelemetryIngestRequest) -> Result<(), String> {
    crate::core::sync_outbox::enqueue(
        crate::core::sync_outbox::OutboxOperationKind::TelemetryIngest,
        serde_json::to_value(request).map_err(|e| e.to_string())?,
    )
    .map(|_| ())
}

fn drain_persisted() {
    let Ok(entries) = crate::core::sync_outbox::load_entries() else {
        return;
    };

    for entry in entries {
        let result = match entry.kind {
            crate::core::sync_outbox::OutboxOperationKind::TelemetryIngest => {
                serde_json::from_value::<TelemetryIngestRequest>(entry.payload.clone())
                    .map_err(anyhow::Error::from)
                    .and_then(|request| deliver_request(&request))
            }
            crate::core::sync_outbox::OutboxOperationKind::ServerToolCall => {
                crate::server_client::replay_queued_server_tool_call(entry.payload.clone())
            }
            crate::core::sync_outbox::OutboxOperationKind::CodeIndexSync => {
                crate::server_client::replay_queued_index_sync(entry.payload.clone())
            }
        };

        match result {
            Ok(()) => {
                let _ = crate::core::sync_outbox::delete(&entry.id);
            }
            Err(error) => {
                let _ = crate::core::sync_outbox::mark_failed(&entry, &error.to_string());
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Map;
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::mpsc;
    use std::time::Duration;

    #[test]
    fn enqueue_persists_when_runtime_not_started() {
        let _lock = crate::core::data_dir::test_env_lock();
        let tmp = tempfile::tempdir().unwrap();
        std::env::set_var("NEBU_CTX_DATA_DIR", tmp.path());

        enqueue(TelemetryIngestRequest {
            tool_name: "ctx_read".to_string(),
            tokens_original: 10,
            tokens_saved: 2,
            duration_ms: 0,
            mode: Some("test".to_string()),
            repository_fingerprint: None,
            checkout_binding: None,
            project_slug: None,
            command_preview: None,
        });

        let entries = crate::core::sync_outbox::load_entries().unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0].kind,
            crate::core::sync_outbox::OutboxOperationKind::TelemetryIngest
        );
    }

    #[test]
    fn flush_pending_replays_all_outbox_kinds_to_server() {
        let _lock = crate::core::data_dir::test_env_lock();
        let tmp = tempfile::tempdir().unwrap();
        std::env::set_var("NEBU_CTX_DATA_DIR", tmp.path());
        std::env::set_var("NEBU_CTX_HOME", tmp.path().join("home"));

        let (endpoint, received_paths) = spawn_replay_server(4);
        crate::config::save_connection(&endpoint, "test-token").unwrap();
        enqueue_replay_fixtures(tmp.path());

        assert_eq!(flush_pending(), 3);
        wait_for_empty_outbox();

        let mut paths = Vec::new();
        let required_paths = [
            "/v1/telemetry/ingest",
            "/v1/tools/call",
            "/v1/projects/resolve",
            "/v1/index/sync",
        ];
        while paths.len() < 16 {
            let path = received_paths.recv_timeout(Duration::from_secs(2)).unwrap();
            paths.push(path);
            if required_paths
                .iter()
                .all(|required| paths.iter().any(|path| path == required))
            {
                break;
            }
        }

        assert!(paths.contains(&"/v1/telemetry/ingest".to_string()));
        assert!(paths.contains(&"/v1/tools/call".to_string()));
        assert!(paths.contains(&"/v1/projects/resolve".to_string()));
        assert!(paths.contains(&"/v1/index/sync".to_string()));
    }

    fn wait_for_empty_outbox() {
        for _ in 0..20 {
            if crate::core::sync_outbox::load_entries().unwrap().is_empty() {
                return;
            }

            std::thread::sleep(Duration::from_millis(25));
        }

        let entries = crate::core::sync_outbox::load_entries().unwrap();
        panic!("expected empty outbox after replay, found: {entries:?}");
    }

    fn enqueue_replay_fixtures(root: &std::path::Path) {
        let context = replay_project_context(root);
        crate::core::sync_outbox::enqueue(
            crate::core::sync_outbox::OutboxOperationKind::TelemetryIngest,
            serde_json::to_value(TelemetryIngestRequest {
                tool_name: "ctx_read".to_string(),
                tokens_original: 100,
                tokens_saved: 40,
                duration_ms: 7,
                mode: Some("test".to_string()),
                repository_fingerprint: Some(context.fingerprint.clone()),
                checkout_binding: Some(context.checkout_binding.clone()),
                project_slug: Some(context.project_slug.clone()),
                command_preview: None,
            })
            .unwrap(),
        )
        .unwrap();

        crate::core::sync_outbox::enqueue(
            crate::core::sync_outbox::OutboxOperationKind::ServerToolCall,
            serde_json::to_value(crate::server_client::QueuedServerToolCall {
                tool_name: "ctx_brain".to_string(),
                arguments: Map::from_iter([
                    ("action".to_string(), serde_json::json!("store")),
                    ("key".to_string(), serde_json::json!("session-test")),
                    ("value".to_string(), serde_json::json!("replayed")),
                ]),
                project_context: (&context).into(),
            })
            .unwrap(),
        )
        .unwrap();

        crate::core::sync_outbox::enqueue(
            crate::core::sync_outbox::OutboxOperationKind::CodeIndexSync,
            serde_json::to_value(crate::server_client::QueuedIndexSync {
                project_context: (&context).into(),
                files: vec![crate::server_client::IndexSyncFile {
                    path: "src/lib.rs".to_string(),
                    hash: "abc".to_string(),
                    language: "rust".to_string(),
                    line_count: 8,
                    token_count: 30,
                    exports: vec!["run".to_string()],
                    summary: "library".to_string(),
                }],
                symbols: vec![crate::server_client::IndexSyncSymbol {
                    file_path: "src/lib.rs".to_string(),
                    name: "run".to_string(),
                    kind: "function".to_string(),
                    start_line: 1,
                    end_line: 3,
                    is_exported: true,
                }],
                edges: vec![crate::server_client::IndexSyncEdge {
                    from_symbol: "run".to_string(),
                    to_symbol: "helper".to_string(),
                    kind: "calls".to_string(),
                }],
            })
            .unwrap(),
        )
        .unwrap();
    }

    fn replay_project_context(root: &std::path::Path) -> crate::models::ProjectContext {
        crate::models::ProjectContext {
            project_slug: "sync-test".to_string(),
            project_root: root.to_string_lossy().to_string(),
            fingerprint: crate::models::RepositoryFingerprint {
                remote_url: Some("https://github.com/example/sync-test.git".to_string()),
                host: Some("github.com".to_string()),
                owner: Some("example".to_string()),
                repo_name: Some("sync-test".to_string()),
                default_branch: Some("main".to_string()),
            },
            checkout_binding: crate::models::CheckoutBinding::default(),
            project_metadata: None,
        }
    }

    fn spawn_replay_server(expected_requests: usize) -> (String, mpsc::Receiver<String>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let endpoint = format!("http://{}", listener.local_addr().unwrap());
        let (tx, rx) = mpsc::channel();

        std::thread::spawn(move || {
            let mut served_requests = 0usize;
            let mut idle_after_expected_since = None;

            loop {
                match listener.accept() {
                    Ok((mut stream, _)) => {
                        idle_after_expected_since = None;
                        served_requests += 1;
                        stream
                            .set_read_timeout(Some(Duration::from_secs(2)))
                            .unwrap();
                        let path = read_request_path(&mut stream);
                        let body = response_body_for(&path);
                        let response = format!(
                            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                            body.len(),
                            body
                        );
                        let _ = stream.write_all(response.as_bytes());
                        let _ = tx.send(path);
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                        if served_requests >= expected_requests {
                            let idle_since = idle_after_expected_since
                                .get_or_insert_with(std::time::Instant::now);
                            if idle_since.elapsed() >= Duration::from_millis(250) {
                                break;
                            }
                        }

                        std::thread::sleep(Duration::from_millis(10));
                    }
                    Err(_) => break,
                }
            }
        });

        (endpoint, rx)
    }

    fn read_request_path(stream: &mut std::net::TcpStream) -> String {
        let mut buffer = Vec::new();
        let mut chunk = [0u8; 512];
        loop {
            let Ok(read) = stream.read(&mut chunk) else {
                break;
            };
            if read == 0 {
                break;
            }
            buffer.extend_from_slice(&chunk[..read]);
            if request_complete(&buffer) {
                break;
            }
        }

        let request = String::from_utf8_lossy(&buffer);
        request
            .lines()
            .next()
            .and_then(|line| line.split_whitespace().nth(1))
            .unwrap_or("/")
            .to_string()
    }

    fn request_complete(buffer: &[u8]) -> bool {
        let Some(header_end) = buffer.windows(4).position(|window| window == b"\r\n\r\n") else {
            return false;
        };
        let headers = String::from_utf8_lossy(&buffer[..header_end]);
        let content_length = headers
            .lines()
            .find_map(|line| line.split_once(':'))
            .filter(|(name, _)| name.eq_ignore_ascii_case("content-length"))
            .and_then(|(_, value)| value.trim().parse::<usize>().ok())
            .unwrap_or(0);
        buffer.len() >= header_end + 4 + content_length
    }

    fn response_body_for(path: &str) -> &'static str {
        match path {
            "/v1/projects/resolve" => {
                r#"{"project":{"project_id":"proj_sync_test","slug":"sync-test","fingerprint":null,"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z"},"checkout_bound":true}"#
            }
            "/v1/tools/call" => r#"{"result":{"ok":true}}"#,
            "/v1/telemetry/ingest" | "/v1/index/sync" => r#"{"ok":true}"#,
            _ => r#"{"ok":true}"#,
        }
    }
}