Skip to main content

lean_ctx/tools/registered/
ctx_delta.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{require_resolved_path, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxDeltaTool;
9
10impl McpTool for CtxDeltaTool {
11    fn name(&self) -> &'static str {
12        "ctx_delta"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_delta",
18            "Incremental diff — sends only changed lines since last read.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "path": { "type": "string", "description": "Absolute file path" }
23                },
24                "required": ["path"]
25            }),
26        )
27    }
28
29    fn handle(
30        &self,
31        args: &Map<String, Value>,
32        ctx: &ToolContext,
33    ) -> Result<ToolOutput, ErrorData> {
34        let path = require_resolved_path(ctx, args, "path")?;
35
36        tokio::task::block_in_place(|| {
37            let cache_lock = ctx
38                .cache
39                .as_ref()
40                .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
41            let timeout_dur =
42                crate::core::io_health::adaptive_timeout(std::time::Duration::from_secs(10));
43            let Ok(mut cache) = tokio::runtime::Handle::current()
44                .block_on(tokio::time::timeout(timeout_dur, cache_lock.write()))
45            else {
46                crate::core::io_health::record_freeze();
47                return Err(ErrorData::internal_error(
48                    "cache busy (ctx_delta) — retry in a moment",
49                    None,
50                ));
51            };
52            let output = crate::tools::ctx_delta::handle(&mut cache, &path);
53            let original = cache.get(&path).map_or(0, |e| e.original_tokens);
54            let tokens = crate::core::tokens::count_tokens(&output);
55            drop(cache);
56
57            if let Some(session_lock) = ctx.session.as_ref() {
58                let mut session = session_lock.blocking_write();
59                session.mark_modified(&path);
60            }
61
62            let saved = original.saturating_sub(tokens);
63            Ok(ToolOutput {
64                text: output,
65                original_tokens: original,
66                saved_tokens: saved,
67                mode: Some("delta".to_string()),
68                path: Some(path),
69                changed: false,
70            })
71        })
72    }
73}