lean-ctx 3.11.4

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. 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
// SPDX-License-Identifier: Apache-2.0
use rmcp::ErrorData;
use rmcp::model::Tool;
use serde_json::{Map, Value, json};

use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
use crate::tool_defs::tool_def;

pub struct CtxWorkGraphTool;

impl McpTool for CtxWorkGraphTool {
    fn name(&self) -> &'static str {
        "ctx_work_graph"
    }

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_work_graph",
            "Free persistent project-scoped bounded local Work Graph for multi-agent execution. Actions: create|delegate|claim|execute|consume|complete|receipt|accept|fuse|attribution|value_report|cancel|get|observe|list. Observe returns compact state without context payloads or execution fences.",
            json!({
                "type": "object",
                "properties": {
                    "action": {"type":"string","enum":["create","delegate","claim","execute","consume","complete","receipt","accept","fuse","attribution","value_report","cancel","get","observe","list"]},
                    "graph_id": {"type":"string","minLength":1,"maxLength":128},
                    "if_revision": {"type":"string","pattern":"^[0-9a-f]{64}$","description":"For observe: return only unchanged=true and revision if state matches. Authorization is still enforced."},
                    "node_id": {"type":"string","minLength":1,"maxLength":128},
                    "parent_node_id": {"type":"string","minLength":1,"maxLength":128},
                    "to_agent": {"type":"string","minLength":1,"maxLength":128},
                    "capsule_ref": {"type":"string","minLength":1,"maxLength":4096},
                    "outcome_ref": {"type":"string","minLength":1,"maxLength":4096},
                    "tokens": {"type":"integer","minimum":0},
                    "cost_micros": {"type":"integer","minimum":0}
                    ,"max_concurrency": {"type":"integer","minimum":1,"maximum":16}
                    ,"receipt_id": {"type":"string","minLength":1,"maxLength":128}
                    ,"execution_fence": {"type":"string","pattern":"^fence:[0-9a-f]{64}$"}
                    ,"outcome": {"type":"string","enum":["accepted","rejected","partial"]}
                    ,"claims": {"type":"array","maxItems":64,"items":{"type":"object","properties":{"key":{"type":"string","minLength":1,"maxLength":128},"value":{"type":"string","maxLength":4096}},"required":["key","value"],"additionalProperties":false}}
                    ,"accepted_nodes": {"type":"array","minItems":1,"maxItems":256,"items":{"type":"string","minLength":1,"maxLength":128}}
                    ,"stop_reason": {"type":"string","enum":["manual_stop","stale","redundant","policy_denied","lease_lost","duplicate","low_value","execution_failed"]}
                    ,"connector": {"type":"string","enum":["codex"]}
                    ,"model": {"type":"string","minLength":1,"maxLength":128}
                    ,"timeout_ms": {"type":"integer","minimum":1,"maximum":1800000}
                    ,"max_attempts": {"type":"integer","minimum":1,"maximum":2}
                    ,"path_claims": {"type":"array","maxItems":64,"items":{"type":"string","pattern":"^path:[^/\\\\](?:[^\\r\\n]*[^/\\\\])?$","maxLength":517}}
                    ,"task_ref": {"type":"string","minLength":1,"maxLength":128}
                    ,"policy_ref": {"type":"string","minLength":1,"maxLength":128}
                    ,"expected_outcome_ref": {"type":"string","minLength":1,"maxLength":128}
                },
                "required": ["action"],
                "additionalProperties": false
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let action = get_str(args, "action")
            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
        // Free local reference behavior has no billing dependency. Registration,
        // project scope, ownership, bounded execution and policy still apply.
        let graph_id = get_str(args, "graph_id").unwrap_or_default();
        let agent_id = ctx
            .agent_id
            .as_ref()
            .map(|agent| agent.blocking_read().clone())
            .unwrap_or_default();
        let changed = matches!(
            action.as_str(),
            "create"
                | "delegate"
                | "claim"
                | "execute"
                | "consume"
                | "complete"
                | "receipt"
                | "accept"
                | "cancel"
        );
        let result = crate::tools::ctx_work_graph::handle(crate::tools::ctx_work_graph::Request {
            action: &action,
            project_root: &ctx.project_root,
            agent_id: agent_id.as_deref().unwrap_or_default(),
            graph_id: &graph_id,
            if_revision: get_str(args, "if_revision").as_deref(),
            node_id: get_str(args, "node_id").as_deref(),
            parent_node_id: get_str(args, "parent_node_id").as_deref(),
            to_agent: get_str(args, "to_agent").as_deref(),
            capsule_ref: get_str(args, "capsule_ref").as_deref(),
            outcome_ref: get_str(args, "outcome_ref").as_deref(),
            tokens: args.get("tokens").and_then(Value::as_u64),
            cost_micros: args.get("cost_micros").and_then(Value::as_u64),
            max_concurrency: args
                .get("max_concurrency")
                .and_then(Value::as_u64)
                .and_then(|value| usize::try_from(value).ok()),
            receipt_id: get_str(args, "receipt_id").as_deref(),
            execution_fence: get_str(args, "execution_fence").as_deref(),
            outcome: get_str(args, "outcome").as_deref(),
            claims: args.get("claims"),
            accepted_nodes: args.get("accepted_nodes"),
            stop_reason: get_str(args, "stop_reason").as_deref(),
            connector: get_str(args, "connector").as_deref(),
            model: get_str(args, "model").as_deref(),
            timeout_ms: args.get("timeout_ms").and_then(Value::as_u64),
            max_attempts: args
                .get("max_attempts")
                .and_then(Value::as_u64)
                .and_then(|value| u8::try_from(value).ok()),
            path_claims: args.get("path_claims"),
            task_ref: get_str(args, "task_ref").as_deref(),
            policy_ref: get_str(args, "policy_ref").as_deref(),
            expected_outcome_ref: get_str(args, "expected_outcome_ref").as_deref(),
        })
        .map_err(|error| ErrorData::invalid_params(error, None))?;
        Ok(ToolOutput {
            text: result,
            original_tokens: 0,
            saved_tokens: 0,
            mode: Some(action),
            path: None,
            changed,
            shell_outcome: None,
            content_blocks: None,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use base64::{Engine as _, engine::general_purpose::STANDARD};
    use ed25519_dalek::{Signer as _, SigningKey};
    use lean_ctx_protocol::{
        EntitlementDeploymentV1, EntitlementEnvelopeV1, EntitlementKindV1, EntitlementPlanV1,
        EntitlementSignerV1, Sha256Digest,
    };
    use sha2::{Digest as _, Sha256};

    #[test]
    fn work_graph_stays_free_after_entitlement_removal_and_preserves_security() {
        let isolated = crate::core::data_dir::isolated_data_dir();
        let cloud = isolated.path().join("cloud");
        std::fs::create_dir_all(&cloud).unwrap();
        let trust_path = isolated.path().join("entitlement-trust.json");
        let credentials_path = cloud.join("credentials.json");
        let cache_path = cloud.join("entitlement-v1.json");
        let key = SigningKey::from_bytes(&[47; 32]);
        let public_key = key.verifying_key().to_bytes();
        let digest = crate::core::agent_identity::hex_encode(&Sha256::digest(public_key));
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let mut envelope = EntitlementEnvelopeV1 {
            schema_version: 1,
            entitlement_id: "handler-entitlement".into(),
            kind: EntitlementKindV1::OnlineSubscription,
            account_id: Some("handler-account".into()),
            plan: EntitlementPlanV1::Pro,
            seats: 1,
            capabilities: vec![crate::core::billing::PRO_LOCAL_WORK_GRAPH.into()],
            issued_at: now - 10,
            not_before: now - 10,
            expires_at: now + 600,
            grace_until: now + 1_200,
            allowed_deployments: vec![EntitlementDeploymentV1::Hosted],
            deployment_id: None,
            org_id: None,
            workspace_id: None,
            signer: EntitlementSignerV1 {
                algorithm: "ed25519".into(),
                key_id: "handler-key".into(),
                public_key_digest: Sha256Digest::new(format!("sha256:{digest}")).unwrap(),
            },
            signature: String::new(),
        };
        envelope.signature =
            STANDARD.encode(key.sign(&envelope.signing_bytes().unwrap()).to_bytes());
        std::fs::write(
            &trust_path,
            serde_json::to_vec(&json!({
                "schema_version": 1,
                "keys": [{
                    "key_id": "handler-key",
                    "public_key_base64": STANDARD.encode(public_key)
                }],
                "revoked_entitlement_ids": [],
                "deployment": "hosted"
            }))
            .unwrap(),
        )
        .unwrap();
        std::fs::write(
            &credentials_path,
            br#"{"api_key":"handler-secret","user_id":"handler-account","email":"handler@example.invalid"}"#,
        )
        .unwrap();
        #[cfg(unix)]
        std::fs::set_permissions(
            &credentials_path,
            std::os::unix::fs::PermissionsExt::from_mode(0o600),
        )
        .unwrap();
        let envelope_bytes = envelope.canonical_bytes().unwrap();
        std::fs::write(&cache_path, &envelope_bytes).unwrap();
        let _guard = crate::cloud_client::install_test_signed_entitlement_paths(
            trust_path,
            credentials_path,
            cache_path,
            vec![("handler-key".into(), public_key)],
        );
        crate::cloud_client::accept_test_signed_entitlement("handler-account", &envelope_bytes);
        let args = Map::from_iter([("action".into(), Value::String("list".into()))]);
        let context = ToolContext {
            project_root: isolated.path().to_string_lossy().into_owned(),
            ..ToolContext::default()
        };

        let Err(error) = CtxWorkGraphTool.handle(&args, &context) else {
            panic!("the dispatcher must enforce agent registration after the paid gate");
        };
        assert_eq!(
            error.message,
            "agent must be registered first via ctx_agent"
        );

        let mut registry = crate::core::agents::AgentRegistry::new();
        let agent = registry
            .register(
                "codex",
                Some("signed graph test"),
                &context.project_root,
                None,
            )
            .unwrap();
        registry.save().unwrap();
        let context = ToolContext {
            agent_id: Some(std::sync::Arc::new(tokio::sync::RwLock::new(Some(agent)))),
            ..context
        };
        let create = json!({
            "action": "create", "graph_id": "paid-graph", "node_id": "root",
            "capsule_ref": "capsule:root", "task_ref": "task:root",
            "policy_ref": "policy:root", "expected_outcome_ref": "outcome:root",
            "tokens": 100, "cost_micros": 50
        });
        let created = CtxWorkGraphTool
            .handle(create.as_object().unwrap(), &context)
            .unwrap();
        assert!(created.changed);
        let listed = CtxWorkGraphTool.handle(&args, &context).unwrap();
        assert!(!listed.changed);
        let listed: Value = serde_json::from_str(&listed.text).unwrap();
        assert_eq!(listed["graph_ids"], json!(["paid-graph"]));
        let get = json!({"action": "get", "graph_id": "paid-graph"});
        let before = CtxWorkGraphTool
            .handle(get.as_object().unwrap(), &context)
            .unwrap();
        let observe = json!({"action": "observe", "graph_id": "paid-graph"});
        let observed = CtxWorkGraphTool
            .handle(observe.as_object().unwrap(), &context)
            .unwrap();
        assert!(!observed.changed);
        let observation: Value = serde_json::from_str(&observed.text).unwrap();
        assert!(observation.get("nodes").is_some());
        assert!(!observed.text.contains("capsule:root"));
        let conditional = json!({
            "action": "observe", "graph_id": "paid-graph",
            "if_revision": observation["revision"]
        });
        let unchanged = CtxWorkGraphTool
            .handle(conditional.as_object().unwrap(), &context)
            .unwrap();
        assert!(!unchanged.changed);
        let unchanged: Value = serde_json::from_str(&unchanged.text).unwrap();
        assert_eq!(unchanged["unchanged"], true);
        assert!(unchanged.get("nodes").is_none());
        let wrong_project = ToolContext {
            project_root: isolated
                .path()
                .join("other-project")
                .to_string_lossy()
                .into_owned(),
            agent_id: context.agent_id.clone(),
            ..ToolContext::default()
        };
        assert!(
            CtxWorkGraphTool
                .handle(conditional.as_object().unwrap(), &wrong_project)
                .is_err()
        );
        assert_eq!(
            before.text,
            CtxWorkGraphTool
                .handle(get.as_object().unwrap(), &context)
                .unwrap()
                .text
        );
        let policy_dir = std::path::Path::new(&context.project_root).join(".lean-ctx");
        std::fs::create_dir_all(&policy_dir).unwrap();
        std::fs::write(policy_dir.join("policy.toml"),
            "name = \"deny-execution\"\nversion = \"1.0.0\"\ndescription = \"test\"\n[context]\ndeny_tools = [\"ctx_work_graph\"]\n").unwrap();
        let execute = json!({
            "action": "execute", "graph_id": "paid-graph", "node_id": "root",
            "connector": "codex"
        });
        let error = CtxWorkGraphTool
            .handle(execute.as_object().unwrap(), &context)
            .err()
            .expect("project policy must deny execution even with a valid paid entitlement");
        assert_eq!(error.message, "project policy denies Work Graph execution");
        let after = CtxWorkGraphTool
            .handle(get.as_object().unwrap(), &context)
            .unwrap();
        assert_eq!(
            before.text, after.text,
            "denial must not claim or mutate the graph"
        );
        std::fs::write(policy_dir.join("policy.toml"),
            "name = \"model-ceiling\"\nversion = \"1.0.0\"\ndescription = \"test\"\n[routing]\nallowed_models = [\"claude-*\"]\nmodel_ceiling_groups = [[\"*-opus-*\"]]\n").unwrap();
        for model in [None, Some(""), Some("gpt-5"), Some("claude-sonnet-5")] {
            let mut request = execute.clone();
            if let Some(model) = model {
                request["model"] = json!(model);
            }
            let error = CtxWorkGraphTool
                .handle(request.as_object().unwrap(), &context)
                .err()
                .expect("unproven or forbidden model must be denied before dispatch");
            assert_eq!(
                error.message,
                "project policy denies Work Graph model selection"
            );
            let unchanged = CtxWorkGraphTool
                .handle(get.as_object().unwrap(), &context)
                .unwrap();
            assert_eq!(before.text, unchanged.text);
        }
        drop(_guard);
        let _denied = crate::cloud_client::install_test_signed_entitlement_paths(
            isolated.path().join("missing-trust.json"),
            cloud.join("missing-credentials.json"),
            cloud.join("missing-entitlement.json"),
            Vec::new(),
        );
        let mut free_create = create.clone();
        free_create["graph_id"] = json!("free-graph");
        assert!(
            CtxWorkGraphTool
                .handle(free_create.as_object().unwrap(), &context)
                .expect("registered agents can create local graphs without a paid account")
                .changed
        );
        assert!(
            CtxWorkGraphTool
                .handle(conditional.as_object().unwrap(), &context)
                .is_ok()
        );
        let cancel = json!({
            "action": "cancel", "graph_id": "paid-graph", "node_id": "root",
            "stop_reason": "manual_stop"
        });
        let unregistered = ToolContext {
            project_root: context.project_root.clone(),
            ..ToolContext::default()
        };
        let error = CtxWorkGraphTool
            .handle(cancel.as_object().unwrap(), &unregistered)
            .err()
            .expect("cancel still requires registration");
        assert_eq!(
            error.message,
            "agent must be registered first via ctx_agent"
        );
        let other_project = ToolContext {
            project_root: isolated
                .path()
                .join("other-project")
                .to_string_lossy()
                .into_owned(),
            agent_id: context.agent_id.clone(),
            ..ToolContext::default()
        };
        let error = CtxWorkGraphTool
            .handle(cancel.as_object().unwrap(), &other_project)
            .err()
            .expect("cancel must preserve project isolation");
        assert!(
            error
                .message
                .contains("agent is not active in this project")
        );
        assert!(
            CtxWorkGraphTool
                .handle(cancel.as_object().unwrap(), &context)
                .unwrap()
                .changed
        );
    }

    #[test]
    fn forged_legacy_plan_files_never_bypass_agent_registration() {
        let isolated = crate::core::data_dir::isolated_data_dir();
        let cloud = isolated.path().join("cloud");
        std::fs::create_dir_all(&cloud).unwrap();
        std::fs::write(cloud.join("plan.txt"), "enterprise").unwrap();
        std::fs::write(
            cloud.join("plan.json"),
            br#"{"plan":"enterprise","verified_at":9007199254740991}"#,
        )
        .unwrap();
        let _guard = crate::cloud_client::install_test_signed_entitlement_paths(
            isolated.path().join("missing-trust.json"),
            cloud.join("missing-credentials.json"),
            cloud.join("missing-entitlement.json"),
            Vec::new(),
        );
        let args = Map::from_iter([("action".into(), Value::String("list".into()))]);
        let context = ToolContext {
            project_root: isolated.path().to_string_lossy().into_owned(),
            ..ToolContext::default()
        };

        let Err(error) = CtxWorkGraphTool.handle(&args, &context) else {
            panic!("forged plan files must not bypass agent registration");
        };

        assert_eq!(
            error.message,
            "agent must be registered first via ctx_agent"
        );
    }
}