lean-ctx 3.9.18

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
//! `kind=skills` pack builder (GH #724/#727, Phase 3) — the first signed,
//! versioned, updatable skill channel.
//!
//! A skills pack is a directory of markdown/scripts turned into named,
//! verified content blobs. **No execution semantics in lean-ctx**: skills are
//! content; interpretation belongs to the consumer (an addon like lean-md, or
//! the agent itself). This closes the `include_str!` problem for addon
//! binaries — content updates ship without a binary release.
//!
//! Determinism (#498): the content bytes are a pure function of the input
//! directory — files are sorted by path (byte order), zstd runs at a fixed
//! level, and nothing time- or environment-dependent enters the payload.

use std::path::{Path, PathBuf};

use chrono::Utc;

use super::content::{
    DocumentBlob, DocumentsContent, MAX_DOCUMENT_FILE_BYTES, MAX_DOCUMENT_FILES,
    MAX_DOCUMENTS_TOTAL_BYTES, PackageContent,
};
use super::manifest::{
    CompatibilitySpec, PackageIntegrity, PackageKind, PackageManifest, PackageProvenance,
    PackageStats,
};
use super::{keys, signing, verify};

/// Everything `pack create --kind skills` produces: the signed bundle plus
/// the facts the CLI discloses. No network I/O.
#[derive(Debug)]
pub(crate) struct SkillsPackPlan {
    pub name: String,
    pub version: String,
    /// The signed `.ctxpkg` document (pretty JSON, ready for store/upload).
    pub bundle_json: String,
    pub manifest: PackageManifest,
    pub content: PackageContent,
    /// Number of files and total plaintext bytes packed.
    pub file_count: usize,
    pub total_bytes: usize,
}

/// Build and sign a `kind=skills` pack from a directory.
///
/// Collects every regular file under `dir` (hidden files/dirs and VCS
/// metadata are skipped), sorted by relative path for deterministic bytes.
pub(crate) fn build_skills_pack(
    dir: &Path,
    name: &str,
    version: &str,
    description: &str,
    author: Option<&str>,
    tags: Vec<String>,
) -> Result<SkillsPackPlan, String> {
    if !dir.is_dir() {
        return Err(format!("`{}` is not a directory", dir.display()));
    }
    if description.trim().is_empty() {
        return Err("a description is required for a skills pack".into());
    }

    let mut rel_paths = collect_files(dir)?;
    rel_paths.sort();
    if rel_paths.is_empty() {
        return Err(format!(
            "`{}` contains no packable files (hidden files and VCS dirs are skipped)",
            dir.display()
        ));
    }
    if rel_paths.len() > MAX_DOCUMENT_FILES {
        return Err(format!(
            "{} files exceed the {MAX_DOCUMENT_FILES}-file cap for a skills pack",
            rel_paths.len()
        ));
    }

    let mut files = Vec::with_capacity(rel_paths.len());
    let mut total = 0usize;
    for rel in &rel_paths {
        verify::validate_document_path(rel)?;
        let bytes = std::fs::read(dir.join(rel)).map_err(|e| format!("read {rel}: {e}"))?;
        if bytes.len() > MAX_DOCUMENT_FILE_BYTES {
            return Err(format!(
                "`{rel}` is {} bytes (per-file cap: {MAX_DOCUMENT_FILE_BYTES})",
                bytes.len()
            ));
        }
        total += bytes.len();
        files.push(DocumentBlob::from_plaintext(rel, &bytes)?);
    }
    if total > MAX_DOCUMENTS_TOTAL_BYTES {
        return Err(format!(
            "pack decodes to {total} bytes (cap: {MAX_DOCUMENTS_TOTAL_BYTES})"
        ));
    }

    let content = PackageContent {
        documents: Some(DocumentsContent { files }),
        ..PackageContent::default()
    };

    // Integrity exactly like the context/addon builders: compact content JSON
    // is the hashed byte stream, the package hash chains name+version onto it.
    let content_json = serde_json::to_string(&content).map_err(|e| e.to_string())?;
    let content_hash = sha256_hex(content_json.as_bytes());
    let sha256 = sha256_hex(format!("{name}:{version}:{content_hash}").as_bytes());

    let mut manifest = PackageManifest {
        schema_version: crate::core::contracts::CONTEXT_PACKAGE_V2_SCHEMA_VERSION,
        conformance_level: None,
        kind: PackageKind::Skills,
        name: name.to_string(),
        version: version.to_string(),
        description: description.to_string(),
        author: author.map(str::to_string),
        scope: name
            .starts_with('@')
            .then(|| name.split('/').next().unwrap_or_default().to_string()),
        created_at: Utc::now(),
        updated_at: None,
        layers: Vec::new(),
        dependencies: Vec::new(),
        tags,
        visibility: None,
        integrity: PackageIntegrity {
            sha256,
            content_hash,
            byte_size: content_json.len() as u64,
        },
        provenance: PackageProvenance {
            tool: "lean-ctx".into(),
            tool_version: env!("CARGO_PKG_VERSION").into(),
            project_hash: None,
            source_session_id: None,
        },
        compatibility: CompatibilitySpec::default(),
        stats: PackageStats::default(),
        signature: None,
        graph_summary: None,
        marketplace: None,
    };
    manifest.validate().map_err(|errs| errs.join("; "))?;
    verify::validate_kind_coherence(&manifest, &content).map_err(|errs| errs.join("; "))?;

    let (signing_key, created) = keys::load_or_create()?;
    if created {
        tracing::info!("ctxpkg: created a new ed25519 signing key for this machine");
    }
    signing::sign_package(&mut manifest, &content, &signing_key);

    // Typed bundle (not `json!`): serde keeps struct field order, so the
    // content text stays byte-identical to what was hashed above.
    #[derive(serde::Serialize)]
    struct Bundle<'a> {
        manifest: &'a PackageManifest,
        content: &'a PackageContent,
    }
    let bundle_json = serde_json::to_string_pretty(&Bundle {
        manifest: &manifest,
        content: &content,
    })
    .map_err(|e| e.to_string())?;

    // Self-check: the exact bytes we would ship must verify cleanly.
    let self_check = verify::verify_package_text(&bundle_json);
    if !self_check.valid() {
        return Err(format!(
            "internal error — the built pack fails verification: {}",
            self_check.errors.join("; ")
        ));
    }

    Ok(SkillsPackPlan {
        name: name.to_string(),
        version: version.to_string(),
        bundle_json,
        manifest,
        content,
        file_count: rel_paths.len(),
        total_bytes: total,
    })
}

/// Recursively collect relative `/`-separated file paths under `root`.
/// Hidden entries (dotfiles) and VCS/tooling dirs are skipped — a skills pack
/// is authored content, not a repository snapshot.
fn collect_files(root: &Path) -> Result<Vec<String>, String> {
    fn walk(root: &Path, dir: &Path, out: &mut Vec<String>) -> Result<(), String> {
        let entries =
            std::fs::read_dir(dir).map_err(|e| format!("read dir {}: {e}", dir.display()))?;
        for entry in entries {
            let entry = entry.map_err(|e| e.to_string())?;
            let path = entry.path();
            let name = entry.file_name().to_string_lossy().to_string();
            if name.starts_with('.') || name == "node_modules" || name == "target" {
                continue;
            }
            let ft = entry.file_type().map_err(|e| e.to_string())?;
            // Symlinks are skipped, not followed: a link pointing outside the
            // directory must never leak foreign file content into the pack.
            if ft.is_symlink() {
                continue;
            }
            if ft.is_dir() {
                walk(root, &path, out)?;
            } else if ft.is_file() {
                let rel = path
                    .strip_prefix(root)
                    .map_err(|e| e.to_string())?
                    .components()
                    .map(|c| c.as_os_str().to_string_lossy())
                    .collect::<Vec<_>>()
                    .join("/");
                out.push(rel);
            }
        }
        Ok(())
    }
    let mut out = Vec::new();
    walk(root, root, &mut out)?;
    Ok(out)
}

/// Materialize a verified skills payload under the pack store:
/// `<store>/skills/<name>/<version>/<path>`. Every blob is decoded through
/// [`DocumentBlob::decode_verified`] — a tampered body aborts the install
/// before anything lands. Files are written read-only; the returned path is
/// the version root the consumer reads from.
pub(crate) fn materialize_documents(
    store_root: &Path,
    manifest: &PackageManifest,
    docs: &DocumentsContent,
) -> Result<PathBuf, String> {
    let version_root = skills_dir(store_root, &manifest.name, &manifest.version);

    // Idempotent re-install: rebuild the version dir from scratch so removed
    // files don't linger.
    if version_root.exists() {
        std::fs::remove_dir_all(&version_root).map_err(|e| e.to_string())?;
    }

    for blob in &docs.files {
        verify::validate_document_path(&blob.path)?;
        let plain = blob.decode_verified()?;
        let dest = version_root.join(&blob.path);
        if let Some(parent) = dest.parent() {
            std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
        }
        std::fs::write(&dest, &plain).map_err(|e| format!("write {}: {e}", dest.display()))?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let _ = std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o444));
        }
    }
    Ok(version_root)
}

/// Store layout for materialized skills:
/// `<store>/skills/<sanitized-name>/<version>/`.
pub(crate) fn skills_dir(store_root: &Path, name: &str, version: &str) -> PathBuf {
    // `@ns/name` → `@ns__name`, mirroring `LocalRegistry::package_dir`.
    let safe_name = name.replace('/', "__");
    store_root.join("skills").join(safe_name).join(version)
}

fn sha256_hex(data: &[u8]) -> String {
    use sha2::{Digest, Sha256};
    let mut h = Sha256::new();
    h.update(data);
    crate::core::agent_identity::hex_encode(&h.finalize())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn scratch(label: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "lc-skills-{label}-{}-{:?}",
            std::process::id(),
            std::thread::current().id()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn write(dir: &Path, rel: &str, text: &str) {
        let p = dir.join(rel);
        std::fs::create_dir_all(p.parent().unwrap()).unwrap();
        std::fs::write(p, text).unwrap();
    }

    fn sample_dir(label: &str) -> PathBuf {
        let dir = scratch(label);
        write(&dir, "skills/review.md", "# Review checklist\n- tests\n");
        write(&dir, "skills/commit.md", "# Commit style\nimperative\n");
        write(&dir, "scripts/setup.sh", "#!/bin/sh\necho setup\n");
        write(&dir, ".hidden.md", "never packed");
        dir
    }

    #[test]
    fn builds_a_signed_verifying_skills_pack() {
        let dir = sample_dir("build");
        let plan = build_skills_pack(
            &dir,
            "@das-tholo/lean-md-skills",
            "1.0.0",
            "Skills for lean-md",
            Some("dasTholo"),
            vec!["skills".into()],
        )
        .expect("plan");

        assert_eq!(plan.file_count, 3, "hidden file is skipped");
        let report = verify::verify_package_text(&plan.bundle_json);
        assert!(report.valid(), "errors: {:?}", report.errors);
        let doc: serde_json::Value = serde_json::from_str(&plan.bundle_json).unwrap();
        assert_eq!(doc["manifest"]["kind"].as_str(), Some("skills"));

        std::fs::remove_dir_all(&dir).ok();
    }

    /// #498 determinism guard: same input directory ⇒ byte-identical content
    /// (and therefore an identical `content_hash`) across two builds.
    #[test]
    fn pack_content_is_deterministic() {
        let dir = sample_dir("determinism");
        let a = build_skills_pack(&dir, "@t/s", "1.0.0", "d", None, vec![]).expect("a");
        let b = build_skills_pack(&dir, "@t/s", "1.0.0", "d", None, vec![]).expect("b");
        assert_eq!(
            a.manifest.integrity.content_hash,
            b.manifest.integrity.content_hash
        );
        assert_eq!(
            serde_json::to_string(&a.content).unwrap(),
            serde_json::to_string(&b.content).unwrap()
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn tampered_blob_is_refused_at_verification_and_materialization() {
        let dir = sample_dir("tamper");
        let plan = build_skills_pack(&dir, "@t/s", "1.0.0", "d", None, vec![]).expect("plan");

        let mut content = plan.content.clone();
        let docs = content.documents.as_mut().unwrap();
        // Swap one body for another valid body — the per-blob plaintext hash
        // must catch the substitution.
        let other = docs.files[1].body.clone();
        docs.files[0].body = other;

        let mut errors = Vec::new();
        super::super::verify::validate_kind_coherence(&plan.manifest, &content)
            .unwrap_err()
            .iter()
            .for_each(|e| errors.push(e.clone()));
        assert!(
            errors.iter().any(|e| e.contains("hash mismatch")),
            "got: {errors:?}"
        );

        let store = scratch("tamper-store");
        let err =
            materialize_documents(&store, &plan.manifest, content.documents.as_ref().unwrap())
                .unwrap_err();
        assert!(err.contains("hash mismatch"), "got: {err}");

        std::fs::remove_dir_all(&dir).ok();
        std::fs::remove_dir_all(&store).ok();
    }

    #[test]
    fn materializes_files_read_only_under_the_store() {
        let dir = sample_dir("mat");
        let plan = build_skills_pack(&dir, "@t/s", "1.2.3", "d", None, vec![]).expect("plan");
        let store = scratch("mat-store");

        let root = materialize_documents(
            &store,
            &plan.manifest,
            plan.content.documents.as_ref().unwrap(),
        )
        .expect("materialize");
        assert!(root.ends_with("skills/@t__s/1.2.3"));
        let review = root.join("skills/review.md");
        assert_eq!(
            std::fs::read_to_string(&review).unwrap(),
            "# Review checklist\n- tests\n"
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&review).unwrap().permissions().mode();
            assert_eq!(mode & 0o222, 0, "materialized skill files are read-only");
        }

        std::fs::remove_dir_all(&dir).ok();
        std::fs::remove_dir_all(&store).ok();
    }

    #[test]
    fn traversal_paths_are_refused() {
        for bad in ["../escape.md", "/abs.md", "a/../../b.md", "c:\\win.md"] {
            assert!(
                verify::validate_document_path(bad).is_err(),
                "`{bad}` must be refused"
            );
        }
        assert!(verify::validate_document_path("skills/ok.md").is_ok());
    }

    /// Redaction-on-load (GH #727 acceptance): a secret inside a skill body
    /// goes through the same redaction plane as every other text before it
    /// reaches tool output.
    #[test]
    fn skill_bodies_pass_through_redaction() {
        let dir = scratch("redact");
        write(
            &dir,
            "skills/creds.md",
            "api key: sk-proj-abcdefghijklmnopqrstuvwxyz012345 do not share\n",
        );
        let plan = build_skills_pack(&dir, "@t/r", "1.0.0", "d", None, vec![]).expect("plan");
        let blob = &plan.content.documents.as_ref().unwrap().files[0];
        let plain = String::from_utf8(blob.decode_verified().unwrap()).unwrap();

        let redacted = crate::core::redaction::redact_text(&plain);
        assert!(
            !redacted.contains("sk-proj-abcdefghijklmnopqrstuvwxyz012345"),
            "secret must not survive redaction: {redacted}"
        );

        std::fs::remove_dir_all(&dir).ok();
    }
}