lean-ctx 3.12.6

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// SPDX-License-Identifier: Apache-2.0
//! Local Service v1 on the daemon socket (`docs/contracts/local-service-v1.md`):
//! the Local Read kinds as routes (`cli::local_read::routes`), and the live
//! event stream `GET /v1/local/events`. Served on the local IPC router only;
//! the socket's 0600 mode is the access control, as for every daemon route.

use std::collections::{BTreeMap, HashSet};
use std::sync::atomic::{AtomicUsize, Ordering};

use axum::{
    Router,
    body::Bytes,
    extract::{Path, Query, State},
    http::{StatusCode, header},
    response::sse::{Event as SseEvent, KeepAlive, Sse},
    response::{IntoResponse, Response},
    routing::{MethodRouter, get, post},
};
use serde_json::{Value, json};
use tokio::time::Duration;

use super::AppState;
use crate::cli::local_read::{
    self, Code, Failure,
    routes::{self, Method, Route},
};

/// Concurrent event streams this daemon serves.
const MAX_STREAMS: usize = 16;
/// How often a stream looks at its sources.
const POLL: Duration = Duration::from_secs(1);
/// Journal events a stream looks at per poll (the newest).
const EVENT_WINDOW: usize = 32;

static STREAMS: AtomicUsize = AtomicUsize::new(0);

/// The HTTP status of an answer with the error `code`.
pub(crate) fn status_of(code: Option<Code>) -> StatusCode {
    match code {
        None => StatusCode::OK,
        Some(Code::InvalidArgument | Code::NoProject) => StatusCode::BAD_REQUEST,
        Some(Code::NotFound | Code::NoVault) => StatusCode::NOT_FOUND,
        Some(Code::Forbidden | Code::Withheld) => StatusCode::FORBIDDEN,
        // `knowledge verify` refused: the anchored code is gone, so the
        // fact has to be rewritten or removed, not confirmed; a local
        // task's state does not allow the write.
        Some(Code::AnchorGone | Code::Conflict) => StatusCode::CONFLICT,
        Some(Code::NotConnected | Code::Unavailable) => StatusCode::SERVICE_UNAVAILABLE,
        // Hive rate-limits this instance and LeanCTX keeps no answer to serve.
        Some(Code::RateLimited) => StatusCode::TOO_MANY_REQUESTS,
        Some(Code::Internal) => StatusCode::INTERNAL_SERVER_ERROR,
    }
}

/// A Local Read document as a response: the CLI's stdout bytes (document
/// and final `\n`).
fn document_response(status: StatusCode, document: &str) -> Response {
    (
        status,
        [(header::CONTENT_TYPE, "application/json")],
        format!("{document}\n"),
    )
        .into_response()
}

fn failure_response(failure: &Failure) -> Response {
    document_response(
        status_of(Some(failure.code)),
        &local_read::error_document(failure),
    )
}

/// The header that tells a socket reader the release point masked a value
/// in the body (the masked value stands in its place). Content-free.
pub(crate) const NOTICE_HEADER: &str = "x-leanctx-release-notice";

pub(crate) fn answer_response(answer: &local_read::Answer) -> Response {
    // G08: without a receipt for them, no document bytes are written.
    let Some(body) = answer.body() else {
        return StatusCode::SERVICE_UNAVAILABLE.into_response();
    };
    let mut response = document_response(status_of(answer.failed), body);
    if let Some(notice) = answer
        .notice
        .as_deref()
        .and_then(|n| header::HeaderValue::from_str(n).ok())
    {
        response.headers_mut().insert(NOTICE_HEADER, notice);
    }
    response
}

async fn respond(
    state: AppState,
    route: &'static Route,
    segment: Option<String>,
    inputs: Vec<(String, Value)>,
) -> Response {
    let work =
        tokio::task::spawn_blocking(move || routes::answer(route, segment.as_deref(), &inputs));
    match tokio::time::timeout(state.timeout, work).await {
        Ok(Ok(answer)) => answer_response(&answer),
        Ok(Err(_)) => failure_response(&Failure::new(Code::Internal, "the read failed")),
        // A write may still complete after this answer: indeterminate.
        Err(_) => failure_response(&Failure::new(
            Code::Unavailable,
            "timed out; a write may still complete",
        )),
    }
}

fn query_inputs(pairs: Vec<(String, String)>) -> Vec<(String, Value)> {
    pairs
        .into_iter()
        .map(|(k, v)| (k, Value::String(v)))
        .collect()
}

/// The entries of a POST body, which must be one JSON object.
fn body_inputs(body: &Bytes) -> Result<Vec<(String, Value)>, Failure> {
    if body.iter().all(u8::is_ascii_whitespace) {
        return Ok(Vec::new());
    }
    match serde_json::from_slice::<Value>(body) {
        Ok(Value::Object(map)) => Ok(map.into_iter().collect()),
        _ => Err(Failure::invalid("the body is one JSON object")),
    }
}

fn method_router(route: &'static Route) -> MethodRouter<AppState> {
    match (route.method, route.segment().is_some()) {
        (Method::Get, true) => get(
            move |State(state): State<AppState>,
                  Path(segment): Path<String>,
                  Query(q): Query<Vec<(String, String)>>| async move {
                respond(state, route, Some(segment), query_inputs(q)).await
            },
        ),
        (Method::Get, false) => get(
            move |State(state): State<AppState>, Query(q): Query<Vec<(String, String)>>| async move {
                respond(state, route, None, query_inputs(q)).await
            },
        ),
        (Method::Post, _) => post(
            move |State(state): State<AppState>, body: Bytes| async move {
                match body_inputs(&body) {
                    Ok(inputs) => respond(state, route, None, inputs).await,
                    Err(failure) => failure_response(&failure),
                }
            },
        ),
    }
}

async fn no_route() -> Response {
    failure_response(&Failure::new(
        Code::NotFound,
        "no such route (see /v1/local/capabilities)",
    ))
}

/// The Local Service routes, for the IPC router.
pub(crate) fn router() -> Router<AppState> {
    let mut by_path: BTreeMap<&'static str, MethodRouter<AppState>> = BTreeMap::new();
    for route in routes::all() {
        let handler = method_router(route);
        let merged = match by_path.remove(route.path) {
            Some(existing) => existing.merge(handler),
            None => handler,
        };
        by_path.insert(route.path, merged);
    }
    let mut router = Router::new()
        .route(routes::EVENTS_PATH, get(events))
        .route("/v1/local/{*rest}", axum::routing::any(no_route));
    for (path, handler) in by_path {
        router = router.route(path, handler);
    }
    router
}

// ---------------------------------------------------------------------------
// Live events

/// The query of the event stream; unknown keys are refused like on the
/// routes.
#[derive(Debug, Default)]
struct EventsQuery {
    project_root: Option<String>,
    types: Option<String>,
    /// `message` events of every project (the person's console).
    all: bool,
}

fn events_query(pairs: Vec<(String, String)>) -> Result<EventsQuery, Failure> {
    let mut q = EventsQuery::default();
    for (key, value) in pairs {
        match key.as_str() {
            "project_root" => q.project_root = Some(value),
            "types" => q.types = Some(value),
            "all" => {
                q.all = match value.as_str() {
                    "true" => true,
                    "false" => false,
                    _ => return Err(Failure::invalid("all is true or false")),
                }
            }
            other => return Err(Failure::invalid(format!("unknown parameter {other}"))),
        }
    }
    Ok(q)
}

/// One event ready to send: its type and its `data` line, a finished Local
/// Read document. A released one is exactly the bytes its receipt binds
/// (with the line's final `\n`).
pub(crate) type Emitted = (&'static str, String);

fn plain(kind: &'static str, data: &Value) -> Emitted {
    (kind, local_read::document(&format!("event.{kind}"), data))
}

/// Releases the stream's slot when the client goes away.
struct Slot;

impl Drop for Slot {
    fn drop(&mut self) {
        STREAMS.fetch_sub(1, Ordering::SeqCst);
    }
}

/// A file's change marker (modified time, length).
type Marker = Option<(std::time::SystemTime, u64)>;

fn marker(path: Option<&std::path::Path>) -> Marker {
    let meta = std::fs::metadata(path?).ok()?;
    Some((meta.modified().ok()?, meta.len()))
}

/// What one stream has already told its client.
pub(crate) struct Tail {
    project: Option<String>,
    all: bool,
    types: Vec<&'static str>,
    started: bool,
    seen_events: HashSet<u64>,
    registry_path: Option<std::path::PathBuf>,
    registry: Marker,
    seen_messages: HashSet<String>,
    active_agents: Option<usize>,
    presence_path: Option<std::path::PathBuf>,
    presence: Marker,
}

impl Tail {
    pub(crate) fn new(project: Option<String>, all: bool, types: Vec<&'static str>) -> Self {
        let registry_path = lean_ctx::core::data_dir::lean_ctx_data_dir()
            .ok()
            .map(|dir| dir.join("agents").join("registry.json"));
        let presence_path = lean_ctx_hive::HiveStore::open()
            .ok()
            .map(|store| store.data_dir().join("bus").join("presence.json"));
        Self {
            project,
            all,
            types,
            started: false,
            seen_events: HashSet::new(),
            registry_path,
            registry: None,
            seen_messages: HashSet::new(),
            active_agents: None,
            presence_path,
            presence: None,
        }
    }

    fn wants(&self, kind: &str) -> bool {
        self.types.contains(&kind)
    }

    /// The `hello` event's data.
    pub(crate) fn hello(&self) -> Value {
        json!({
            "project_root": self.project,
            "all": self.all,
            "types": self.types,
            "service": {
                "version": env!("CARGO_PKG_VERSION"),
                "build": lean_ctx::core::integrity::build().to_json(),
            },
        })
    }

    /// The events since the last poll. The first poll only takes note of the
    /// current state: the stream is live, not a replay.
    pub(crate) fn poll(&mut self) -> Vec<Emitted> {
        let mut out = Vec::new();
        self.poll_journal(&mut out);
        self.poll_registry(&mut out);
        let presence = marker(self.presence_path.as_deref());
        if self.started && presence != self.presence && self.wants("presence_changed") {
            out.push(plain(
                "presence_changed",
                &json!({ "route": "/v1/local/presence" }),
            ));
        }
        self.presence = presence;
        self.started = true;
        out
    }

    /// The project a released event is released under (none for `all`).
    fn release_root(&self) -> String {
        if self.all {
            String::new()
        } else {
            self.project.clone().unwrap_or_default()
        }
    }

    /// `data` released through the CLI tool run of `tool` for the socket,
    /// as a route answer is (redaction, gateway, ONE transport receipt for
    /// the exact document sent). A withheld event is sent as its type with
    /// the `withheld` error document as data (that is what was receipted);
    /// without a stored receipt nothing is sent. An event in which a value
    /// was masked is followed by a `release_notice` when the stream asked
    /// for that type.
    pub(crate) fn push_released(
        &self,
        out: &mut Vec<Emitted>,
        tool: &str,
        kind: &'static str,
        data: Value,
    ) {
        let answer = local_read::answer_released(
            tool,
            &self.release_root(),
            &format!("event.{kind}"),
            local_read::Via::Socket,
            || Ok(data),
        );
        let Some(text) = answer.body() else {
            return;
        };
        out.push((kind, text.to_owned()));
        if let Some(notice) = answer.notice
            && self.wants("release_notice")
        {
            out.push(plain(
                "release_notice",
                &json!({ "event": kind, "notice": notice }),
            ));
        }
    }

    fn poll_journal(&mut self, out: &mut Vec<Emitted>) {
        if !(self.wants("tool_call") || self.wants("activity") || self.wants("knowledge_changed")) {
            return;
        }
        let project = self.project.as_deref().map(std::path::Path::new);
        let events = lean_ctx::core::events::load_served_events(EVENT_WINDOW, project);
        let window: HashSet<u64> = events.iter().map(|e| e.id).collect();
        for event in events {
            if !self.started || self.seen_events.contains(&event.id) {
                continue;
            }
            let Some((kind, mut data)) = journal_event(&event) else {
                continue;
            };
            if !self.wants(kind) {
                continue;
            }
            if carries_free_text(kind, &data) {
                // A policy reason, a warning's detail, an agent's action, a
                // fact key: through the release point like any project text.
                if kind == "knowledge_changed" {
                    data["project_root"] = json!(self.project);
                }
                self.push_released(out, "ctx_session", kind, data);
            } else {
                // Tool names, modes, paths the gate admitted, numbers.
                out.push(plain(kind, &data));
            }
        }
        // The window is the memory: ids that left it are not served again.
        self.seen_events = window;
    }

    fn poll_registry(&mut self, out: &mut Vec<Emitted>) {
        let now = marker(self.registry_path.as_deref());
        if self.started && now == self.registry {
            return;
        }
        self.registry = now;
        let registry = lean_ctx::core::agents::AgentRegistry::load().unwrap_or_default();
        let active = registry.list_active(self.project.as_deref()).len();
        if self.started && self.active_agents != Some(active) && self.wants("agents_changed") {
            out.push(plain("agents_changed", &json!({ "active": active })));
        }
        self.active_agents = Some(active);
        // Messages carry agent text: only for a named project or the
        // person's console (`all`), released as the `agent messages` read is.
        if self.project.is_none() && !self.all {
            return;
        }
        let selection = local_read::bus::Selection {
            project_root: if self.all { None } else { self.project.clone() },
            all: self.all,
            since: None,
            to: None,
            from: None,
            thread: None,
            limit: 200,
        };
        let rows = local_read::bus::select(&registry.scratchpad, &selection, chrono::Utc::now());
        let current: HashSet<String> = rows.iter().map(|r| r.id.clone()).collect();
        if self.started && self.wants("message") {
            for row in rows {
                if self.seen_messages.contains(&row.id) {
                    continue;
                }
                if let Ok(data) = serde_json::to_value(&row) {
                    self.push_released(out, "ctx_agent", "message", data);
                }
            }
        }
        self.seen_messages = current;
    }
}

/// Whether a journal event carries free text and so passes the release
/// point. One release costs about 2 ms under protection (3.5 ms with 16
/// streams contending for the receipt store, measured 2026-11-05), once per
/// stream: the per-tool-call kinds without free text (`tool_call`,
/// `cache_hit`, `compression`, threshold changes) are served through the
/// observation gate only; the rare ones with free text are released.
fn carries_free_text(kind: &str, data: &Value) -> bool {
    match kind {
        "tool_call" => false,
        "activity" => !matches!(
            data["activity"].as_str(),
            Some("cache_hit" | "compression" | "threshold_shift" | "threshold_adapted")
        ),
        _ => true,
    }
}

/// The stream event of one journal event, by its kind.
fn journal_event(event: &lean_ctx::core::events::LeanCtxEvent) -> Option<(&'static str, Value)> {
    use lean_ctx::core::events::EventKind;
    let at = event.timestamp.clone();
    Some(match &event.kind {
        EventKind::ToolCall {
            tool,
            tokens_original,
            tokens_saved,
            mode,
            duration_ms,
            path,
        } => (
            "tool_call",
            json!({
                "at": at, "event_id": event.id, "tool": tool, "mode": mode, "path": path,
                "duration_ms": duration_ms, "tokens_original": tokens_original,
                "tokens_saved": tokens_saved,
            }),
        ),
        EventKind::KnowledgeUpdate {
            category,
            key,
            action,
        } => (
            "knowledge_changed",
            json!({
                "at": at, "event_id": event.id, "route": "/v1/local/knowledge",
                "category": category, "key": key, "action": action,
            }),
        ),
        other => {
            let Value::Object(mut detail) = serde_json::to_value(other).ok()? else {
                return None;
            };
            let name = detail
                .remove("type")
                .and_then(|t| t.as_str().map(snake_case))?;
            (
                "activity",
                json!({ "at": at, "event_id": event.id, "activity": name, "detail": detail }),
            )
        }
    })
}

fn snake_case(name: &str) -> String {
    let mut out = String::with_capacity(name.len() + 4);
    for (i, c) in name.chars().enumerate() {
        if c.is_ascii_uppercase() {
            if i > 0 {
                out.push('_');
            }
            out.push(c.to_ascii_lowercase());
        } else {
            out.push(c);
        }
    }
    out
}

/// The requested event types, sorted; all when none are named.
pub(crate) fn event_types(raw: Option<&str>) -> Result<Vec<&'static str>, Failure> {
    let Some(raw) = raw.filter(|r| !r.trim().is_empty()) else {
        return Ok(routes::EVENT_TYPES.to_vec());
    };
    let mut out = Vec::new();
    for name in raw.split(',').map(str::trim).filter(|n| !n.is_empty()) {
        let known = routes::EVENT_TYPES
            .iter()
            .find(|t| **t == name)
            .ok_or_else(|| Failure::invalid(format!("unknown event type {name}")))?;
        if !out.contains(known) {
            out.push(*known);
        }
    }
    out.sort_unstable();
    Ok(out)
}

fn sse_event(seq: u64, kind: &str, data: &str) -> SseEvent {
    SseEvent::default()
        .id(seq.to_string())
        .event(kind)
        .data(data)
}

async fn events(Query(pairs): Query<Vec<(String, String)>>) -> Response {
    let q = match events_query(pairs) {
        Ok(q) => q,
        Err(failure) => return failure_response(&failure),
    };
    let types = match event_types(q.types.as_deref()) {
        Ok(types) => types,
        Err(failure) => return failure_response(&failure),
    };
    let project = match q.project_root.as_deref() {
        None => None,
        Some(raw) => match routes::project_dir(raw) {
            Ok(root) => Some(root),
            Err(failure) => return failure_response(&failure),
        },
    };
    let all = q.all;
    if STREAMS.fetch_add(1, Ordering::SeqCst) >= MAX_STREAMS {
        STREAMS.fetch_sub(1, Ordering::SeqCst);
        return failure_response(&Failure::new(
            Code::Unavailable,
            format!("at most {MAX_STREAMS} event streams"),
        ));
    }
    let slot = Slot;
    let Ok(tail) = tokio::task::spawn_blocking(move || {
        let mut tail = Tail::new(project, all, types);
        let _ = tail.poll();
        tail
    })
    .await
    else {
        return failure_response(&Failure::new(Code::Internal, "stream start failed"));
    };
    let hello = plain("hello", &tail.hello());
    let pending: std::collections::VecDeque<Emitted> = std::iter::once(hello).collect();
    let stream = futures::stream::unfold(
        (Some(tail), pending, 0_u64, slot),
        |(mut tail, mut pending, mut seq, slot)| async move {
            loop {
                if let Some((kind, data)) = pending.pop_front() {
                    seq += 1;
                    let event = sse_event(seq, kind, &data);
                    return Some((
                        Ok::<_, std::convert::Infallible>(event),
                        (tail, pending, seq, slot),
                    ));
                }
                tokio::time::sleep(POLL).await;
                let mut current = tail.take()?;
                let (back, found) = tokio::task::spawn_blocking(move || {
                    let found = current.poll();
                    (current, found)
                })
                .await
                .ok()?;
                tail = Some(back);
                pending.extend(found);
            }
        },
    );
    Sse::new(stream)
        .keep_alive(KeepAlive::new().interval(Duration::from_secs(15)))
        .into_response()
}