AI 编码的上下文工程层
。
lean-ctx 是一个包含三个独立层的压缩系统。上下文服务器用 AST 感知的压缩版本替换你的 AI 文件读取。Shell hook 拦截 95+ CLI 命令模式。三个通信协议教 AI 更高效地响应。你安装一次--其余一切自动完成。
lean-ctx 位于 哪里?
lean-ctx 在两个层面拦截:上下文层(文件读取、代码搜索、shell 命令)和 shell 层(原始 CLI 输出)。两者都在数据到达 LLM 之前压缩。AI 以极少的 token 看到相同的信息。
Without lean-ctx: You → AI Tool → reads file (full content) → LLM processes everything With lean-ctx: You → AI Tool → lean-ctx compresses → signal only → LLM: 60–99% less noise ├─ PathJail sandbox + size caps ├─ AST-aware compression ├─ Memory Runtime (knowledge + forgetting) ├─ Session cache (re-read ≈ 13 tok) └─ 10 read modes per file type
你
正常编码。工作流无需任何改变。
AI 工具
Cursor、Claude Code、Copilot、Windsurf……
lean-ctx
通过上下文服务器和 Shell Hook 压缩一切。
LLM
只看到信号。60–90% 更少的 token,相同的信息。
那么 你 实际需要做什么?
几乎不用做什么。lean-ctx 在一次性设置后隐形工作。你继续像以前一样编码。
安装
单个 Rust 二进制文件。无依赖,无云端,无账户。
curl -fsSL leanctx.com/install.sh | sh 设置
自动检测你的 shell 和 AI 工具。几秒内完成配置。
lean-ctx setup 没有第 3 步
正常编码。你的 AI 自动使用压缩工具。
节省的 Token: 自动你 vs. 机器
| 你做的事(一次) | 自动发生的事 |
|---|---|
| lean-ctx setup | AI 使用 ctx_read 代替 Read--减少高达 99% 的 token(缓存重新读取) |
AI 使用 ctx_shell 代替 Shell--减少 60–90% 的 token | |
AI 使用 ctx_search 代替 Grep--减少 50–80% 的 token | |
| Shell hook 透明压缩 CLI 输出 | |
| 会话缓存跨读取记住文件(重新读取仅需约 13 个 token) | |
| lean-ctx gain (可选) | 查看你的终身节省仪表盘 |
Reciprocal Rank Fusion (RRF) Cache Eviction
When the session cache is full, lean-ctx uses Reciprocal Rank Fusion to decide which entries to evict. Each signal (recency, frequency, size) is ranked independently, then fused into a single score - no arbitrary weight tuning required.
Formula: RRF(d) = Σ 1/(K + rank_i(d)) where K=60. This handles signal incomparability (seconds vs counts vs tokens) without needing tuned weights, unlike traditional weighted combinations.
Want to understand the architecture?
Explore the Context OS: the six-step pipeline, integration modes, agent matrix, Context Field Theory, and nine architectural pillars that make lean-ctx work.
Deep dive into Context OSSecurity Layer
lean-ctx enforces a defense-in-depth security model to prevent LLM prompt injection from accessing files outside the project:
- PathJail - All file operations are sandboxed to the project root. Symlink traversal,
../escapes, and absolute paths outside the jail are blocked at theresolve_pathchokepoint. - Bounded Shell Capture - Shell output is capped at 200KB with clear truncation markers. Prevents memory exhaustion from runaway commands.
- TOCTOU Prevention - File edits use same file handle for read-verify-write, eliminating time-of-check-to-time-of-use race conditions.
- Build Integrity - Compile-time integrity seed and hash verification detect tampered binaries. Checked automatically by
lean-ctx doctor.