33aa4b4e54
CodeQL / Analyze (rust) (push) Has been cancelled
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Build (no embeddings / no ORT) (push) Has been cancelled
CI / Format (push) Has been cancelled
Security Check / Security Scan (push) Has been cancelled
CI / Cookbook (Node) (push) Has been cancelled
CI / Pi Extension (Node) (push) Has been cancelled
CI / Rust SDK (lean-ctx-client) (push) Has been cancelled
CI / Embed SDK (lean-ctx-sdk) (push) Has been cancelled
CI / Python SDK (leanctx) (push) Has been cancelled
CI / Hermes Plugin (Python) (push) Has been cancelled
CI / SDK Conformance Matrix (push) Has been cancelled
CI / Coverage (push) Has been cancelled
CI / cargo-deny (push) Has been cancelled
CI / Adversarial Safety (push) Has been cancelled
CI / Benchmarks (push) Has been cancelled
CI / Output-Quality Gate (eval A/B) (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI / CI Green (push) Has been cancelled
654 lines
35 KiB
Markdown
654 lines
35 KiB
Markdown
<!-- WEHUB_ZH_README -->
|
||
> [!NOTE]
|
||
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
|
||
> [English](./README.en.md) · [原始项目](https://github.com/yvgude/lean-ctx) · [上游 README](https://github.com/yvgude/lean-ctx/blob/HEAD/README.md)
|
||
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
|
||
|
||
<div align="center">
|
||
|
||
<pre>
|
||
██╗ ███████╗ █████╗ ███╗ ██╗ ██████╗████████╗██╗ ██╗
|
||
██║ ██╔════╝██╔══██╗████╗ ██║ ██╔════╝╚══██╔══╝╚██╗██╔╝
|
||
██║ █████╗ ███████║██╔██╗ ██║ ██║ ██║ ╚███╔╝
|
||
██║ ██╔══╝ ██╔══██║██║╚██╗██║ ██║ ██║ ██╔██╗
|
||
███████╗███████╗██║ ██║██║ ╚████║ ╚██████╗ ██║ ██╔╝ ██╗
|
||
╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
|
||
</pre>
|
||
|
||
### **掌控你的 AI 能看到什么。**
|
||
|
||
**LeanCTX — 面向 AI 智能体的精简上下文工程(Lean Context Engineering)**
|
||
|
||
LeanCTX — **Lean Context** 的缩写 — 是面向 AI 智能体的上下文工程层。它以单一本地二进制程序的形式运行在你的智能体与其所接触的一切之间 — 你的代码、Shell、数据以及模型本身:它**决定**它们读取什么,**压缩**它们发送的内容(可选的本地代理会缩小每一次请求 — 系统提示、历史记录与工具结果 — 且对 prompt cache 安全),**记住**它们所学,**守护**它们所接触的内容 — 并通过带签名的可验证节省账本**证明**它们节省了什么。结果是:token 减少 60–90%。
|
||
零配置。
|
||
本地优先(Local-first)。
|
||
|
||
| 问题 | 使用 LeanCTX |
|
||
|---------|-------------|
|
||
| 重复读取文件:每次约 2000 tokens | 缓存后重读:**约 13 tokens** |
|
||
| 原始 `git status`:约 800 tokens | 压缩后:**约 120 tokens** |
|
||
| 每一轮都重新发送完整历史 | 代理压缩每次请求,**对 prompt cache 安全** |
|
||
| 每次聊天上下文都会重置 | 会话记忆跨聊天持久保存 |
|
||
| 无法洞察上下文使用情况 | 实时仪表盘 + 预算控制 |
|
||
|
||
---
|
||
|
||
<p>
|
||
<a href="https://github.com/yvgude/lean-ctx/stargazers"><img src="https://img.shields.io/github/stars/yvgude/lean-ctx?style=social" alt="GitHub Stars"></a>
|
||
<a href="https://github.com/yvgude/lean-ctx/actions/workflows/ci.yml"><img src="https://github.com/yvgude/lean-ctx/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
||
<a href="https://github.com/yvgude/lean-ctx/actions/workflows/security-check.yml"><img src="https://github.com/yvgude/lean-ctx/actions/workflows/security-check.yml/badge.svg" alt="Security"></a>
|
||
<a href="https://crates.io/crates/lean-ctx"><img src="https://img.shields.io/crates/v/lean-ctx?color=%23e6522c" alt="crates.io"></a>
|
||
<a href="https://crates.io/crates/lean-ctx"><img src="https://img.shields.io/crates/d/lean-ctx?color=%23e6522c" alt="Downloads"></a>
|
||
<a href="https://www.npmjs.com/package/lean-ctx-bin"><img src="https://img.shields.io/npm/v/lean-ctx-bin?label=npm&color=%23cb3837" alt="npm"></a>
|
||
<a href="https://aur.archlinux.org/packages/lean-ctx"><img src="https://img.shields.io/aur/version/lean-ctx?color=%231793d1" alt="AUR"></a>
|
||
<a href="https://pi.dev/packages/pi-lean-ctx"><img src="https://img.shields.io/badge/Pi.dev-pi--lean--ctx-6366f1?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJ3aGl0ZSI+PHRleHQgeD0iNCIgeT0iMTgiIGZvbnQtc2l6ZT0iMTYiIGZvbnQtZmFtaWx5PSJzZXJpZiI+z4A8L3RleHQ+PC9zdmc+" alt="Pi.dev"></a>
|
||
<a href="LICENSE"><img src="https://img.shields.io/badge/License-Apache%202.0-blue.svg" alt="License"></a>
|
||
<a href="https://discord.gg/pTHkG9Hew9"><img src="https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&logoColor=white" alt="Discord"></a>
|
||
<a href="https://x.com/leanctx"><img src="https://img.shields.io/badge/𝕏-Follow-000000?logo=x&logoColor=white" alt="X/Twitter"></a>
|
||
<img src="https://img.shields.io/badge/Telemetry-Opt--in%20Only-brightgreen?logo=shield&logoColor=white" alt="Opt-in Telemetry">
|
||
</p>
|
||
|
||
<p>
|
||
<a href="https://leanctx.com">Website</a> · <a href="https://leanctx.com/docs/getting-started">Docs</a> · <a href="#get-started-60-seconds">Install</a> · <a href="#use-it-from-your-own-code-sdks">SDKs</a> · <a href="#real-world-scenarios">Scenarios</a> · <a href="#demo">Demo</a> · <a href="#benchmarks">Benchmarks</a> · <a href="cookbook/README.md">Cookbook</a> · <a href="SECURITY.md">Security</a> · <a href="CHANGELOG.md">Changelog</a>
|
||
</p>
|
||
|
||
</div>
|
||
|
||
---
|
||
|
||
> **掌控你的 AI 能看到什么。** LeanCTX — **Lean Context** 的缩写 — 是面向 AI 智能体的**上下文工程层**:一个本地 Rust 二进制程序,决定你的智能体读取什么、压缩它们发送给模型的内容、记住它们所学、守护它们所接触的内容 — 并证明它们节省了什么。
|
||
|
||
> Token 节省是收据,智能才是产品。可与 **Cursor、Claude Code、Copilot、Windsurf、Codex、Gemini** 及其他 30+ 智能体配合使用 — 无需配置。
|
||
|
||
<p align="center"><strong>眼见为实:</strong></p>
|
||
|
||
<table>
|
||
<tr>
|
||
<td align="center" width="33%">
|
||
<img src="assets/leanctx-demo.gif" width="320" alt="Map-mode file read + compressed git output demo">
|
||
<br/>
|
||
<strong>Read + Shell</strong>
|
||
<br/>
|
||
Map 模式读取 + 压缩后的 CLI 输出
|
||
</td>
|
||
<td align="center" width="33%">
|
||
<img src="assets/leanctx-gain.gif" width="320" alt="lean-ctx gain live dashboard demo">
|
||
<br/>
|
||
<strong>Gain (live)</strong>
|
||
<br/>
|
||
实时 token 与美元节省
|
||
</td>
|
||
<td align="center" width="33%">
|
||
<img src="assets/leanctx-benchmark.gif" width="320" alt="lean-ctx benchmark report demo">
|
||
<br/>
|
||
<strong>Benchmark proof</strong>
|
||
<br/>
|
||
按语言与模式测量压缩效果
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
|
||
<p align="center"><sub>所有 GIF 均由 <code>demo/</code> 中可复现的 VHS 录像带生成。</sub></p>
|
||
|
||
## 开发者为何使用 LeanCTX
|
||
|
||
- **更长的有效编码会话** — 减少上下文浪费 = 为实际代码推理留出更多空间
|
||
- **更低的 API 成本** — 读取与 Shell 输出 token 减少 60–90%,缓存后重读仅需约 13 tokens
|
||
- **告别「我已经给你看过这个文件了」** — 会话记忆跨聊天持久保存
|
||
- **适配你现有环境** — 一条 `lean-ctx setup` 命令,无需更改配置
|
||
- **完全可见** — 精确了解上下文窗口预算花在哪里
|
||
- **模型无关且归你所有** — 可自由切换 OpenAI/Anthropic/Gemini;你的上下文与记忆保持本地、可移植,永不锁死在厂商黑盒中
|
||
|
||
---
|
||
|
||
<p align="center">
|
||
<strong>帮你省了 token?</strong> <a href="https://github.com/yvgude/lean-ctx">点个 Star</a> — 帮助更多人发现 LeanCTX。
|
||
</p>
|
||
|
||
---
|
||
|
||
## 为何是现在 — 拥有你的上下文
|
||
|
||
模型正在趋同于商品化。持久的优势不在于你调用*哪一个*模型 — 而在于你的**上下文**:智能体读取什么、记住什么,以及你能证明什么。而优化并*拥有*该上下文的层,不可能来自按 token 计费或将你的记忆锁在黑盒中的厂商 — 它必须站在你这一侧。
|
||
|
||
这就是「agent entities」浪潮背后的转变:它们活在你的聊天中并记住你的公司(Claude in Slack、ClickUp Brain)— 这是一种**上下文登录,而非模型登录** — 你最终是在租回自己的公司知识。LeanCTX 是相反的一层。它让你的护城河归你所有:本地优先、可移植(`.ctxpkg`)、模型无关 — 切换 OpenAI、Anthropic 或 Gemini 而不丢失上下文或缓存。**拥有你的上下文;不要把它租回来。**
|
||
|
||
---
|
||
|
||
## 它能做什么 — 上下文的四个维度
|
||
|
||
LeanCTX 将上下文视为受管资源,而非事后补救。一个二进制程序覆盖决定 AI 智能体实际表现优劣的四个维度:
|
||
|
||
### 1. 压缩 — 输入效率
|
||
|
||
你的 AI 智能体会读取文件并运行命令。LeanCTX 自动压缩两者。
|
||
|
||
- **文件读取**:10 种读取模式(`full`、`map`、`signatures`、`diff`、`lines:N-M`、`density:X`、…)— 缓存后重读仅需约 13 tokens
|
||
- **目标密度**(`density:0.4`):SDE 风格预算压缩 — 保留最高熵行直至剩余约原 token 数的 40%,结果确定
|
||
- **JIT 披露**:`signatures` 携带行跨度并指向 `lines:N-M` 以按需定向展开 — 先给大纲,正文按需获取
|
||
- **Shell 输出**:95+ 种 Shell 输出模式压缩 git、npm、cargo、docker、kubectl、terraform 等(270 条直通规则)
|
||
- **Tree-sitter AST**:对 27 种语言进行结构化理解 — 不只是文本压缩
|
||
- **可逆设计(CCR)**:压缩从不*丢弃*内容 — 被剪枝或截断的载荷移至内容寻址存储并附带确定性句柄,模型可通过 `ctx_expand`、`ctx_retrieve`、带内标记或 `GET /v1/references/{id}` 按需取回原始字节。[五种恢复路径 →](docs/comparisons/vs-headroom.md#reversibility)
|
||
|
||
### 2. 路由 — 按读取场景选择合适粒度
|
||
|
||
并非每个文件都需要同等深度。LeanCTX 传递信号,过滤噪声。
|
||
|
||
- **10 种读取模式**:从完整内容到 AST 签名(AST signatures)以及熵过滤视图
|
||
- **自适应 `ModePredictor`**:根据过往会话为各文件类型学习最优读取模式
|
||
- **`IntentEngine`**:对查询复杂度分类,让简单查找保持低成本
|
||
|
||
### 3. 记忆 — 持久化上下文
|
||
|
||
上下文不再在聊天之间消失。
|
||
|
||
- **会话记忆(CCP)**:在聊天之间持久化任务/事实/决策 — 结构化恢复查询可经受压缩
|
||
- **知识图谱(Knowledge graph)**:带有效期窗口的时序事实,情节记忆 + 程序性记忆
|
||
- **Property Graph**:多边代码图谱(imports、calls、exports、type_ref)支撑影响分析与搜索排序
|
||
- **属于你,而非厂商**:记忆保持本地且可移植 — 导出为 `.ctxpkg` 包,可在机器或模型间迁移,而非锁死在厂商黑箱中
|
||
|
||
### 4. 验证 — 控制进入模型的内容
|
||
|
||
性能关乎准确性,而非仅速度。你始终掌控上下文窗口。
|
||
|
||
- **Context Manager**:浏览器仪表盘,实时 token 追踪、压缩统计、利用率仪表
|
||
- **预算与 SLO**:配置文件、角色、按智能体预算及限流策略
|
||
- **Context Proof**(`ctx_proof`、`ctx_verify`):四层验证引擎,附带 CI 漂移门禁
|
||
|
||
<details>
|
||
<summary><strong>完整功能列表(81 个 MCP 工具)</strong></summary>
|
||
|
||
- **Web & Research**(`ctx_url_read`):将公开网页、PDF 或 YouTube 转录稿拉入上下文,形成压缩且带引用的文本 — `facts`/`quotes` 返回带置信度评分 + 来源 URL 的断言,按相关性排序的研究压缩可精炼至 token 预算,SSRF 防护(仅 http/https)
|
||
- **Graph-Powered Intelligence**:混合搜索(BM25 + 嵌入 + 经 RRF 的图邻近度),增量 git-diff 更新
|
||
- **LSP Refactoring**(`ctx_refactor`):经语言服务器驱动的重命名、引用、跳转定义,支持 rust-analyzer、typescript-language-server、pylsp、gopls
|
||
- **Multi-Agent**(`ctx_agent`、`ctx_handoff`):智能体交接含上下文传输包、日记系统、同步共享状态
|
||
- **Archive Full-Text Search**(`ctx_expand search_all`):FTS5 驱动的跨归档全文搜索,覆盖所有此前归档的工具输出
|
||
- **PR Context Packs**:`lean-ctx pack --pr` 构建 PR 就绪上下文包(变更文件、相关测试、影响、产物)
|
||
- **Context Packages**:`lean-ctx pack create` 将 Knowledge + Graph + Session 打包为可移植的 `.ctxpkg` 文件,SHA-256 完整性校验
|
||
- **Context Time Machine**:`lean-ctx snapshot create|list|show|verify|restore|publish|import` — 以 git 为锚、ed25519 签名的层状态快照(lineage、ledger Φ、ROI、session),存于仅追加时间线;在仪表盘中回放,`restore` 恢复会话(`--git` 检出提交),或 `publish`/`import` 签名快照以共享([概念 →](docs/concepts/context-time-machine.md))
|
||
- **Observability**:`lean-ctx gain --live` 实时节省,`lean-ctx wrapped` 周/月摘要(`gain --svg`/`--share` 可分享卡片或自托管页面),`lean-ctx watch` TUI 监控
|
||
- **Verified savings**:`lean-ctx savings` 是可审计的逐事件账本(分词器透明、bounce-netting、防篡改 SHA-256 链)— 仅本地,默认开启
|
||
- **HTTP mode**:`lean-ctx serve` 用于 Streamable HTTP MCP + `/v1/tools/call`(Cookbook + SDK 使用)
|
||
|
||
</details>
|
||
|
||
## Addons — 通过单一网关运行整个生态
|
||
|
||
你不必在 LeanCTX 与现有其他上下文工具之间二选一。**addon** 将任意 MCP 服务器封装在精简的 `lean-ctx-addon.toml` 清单中;LeanCTX 一条命令即在网关后运行它,并将其返回值当作自有读取处理,而非单纯代理。
|
||
|
||
```bash
|
||
lean-ctx addon search memory # browse the registry by category
|
||
lean-ctx addon add headroom # installs the upstream package + wires the MCP server, on add
|
||
lean-ctx addon list # what's wired into your gateway
|
||
```
|
||
|
||
- **一条命令添加** — `addon add <name>` 通过原生包管理器(uv、pip、cargo、npm、brew、dotnet)安装上游包,并将 MCP 服务器接入网关。无需 fork,无需重新编译。
|
||
- **融入处理,而非仅代理** — 可选后处理将 addon 输出经与代码相同的流水线:压缩至预算、将超大块溢出至 `ctx_expand` 句柄、索引至 BM25 / graph / knowledge。类型化适配器将特定工具直接路由至 `ctx_expand`、`ctx_callgraph` 和 `ctx_knowledge`。
|
||
- **默认不可信** — 每个 addon 输出在抵达模型前均会清除机密并标记为不可信。始终开启,非可选开关。
|
||
- **你始终掌控** — 通过单一 `[addons]` 策略将单机或集群固定为仅 verified、白名单或完全关闭,单个仓库无法覆盖。
|
||
|
||
注册表涵盖压缩(Headroom、Sophon)、代码智能(Repomix、Serena)、记忆(Mem0、Cognee、Letta)与推理(Sequential Thinking)。参阅 **[addon 指南](docs/guides/addons.md)** 或 [浏览全部](https://leanctx.com/addons/).
|
||
|
||
## 发展方向
|
||
|
||
LeanCTX 正从单一上下文*层*成长为面向整支团队的完整**认知上下文层**:版本化的上下文策略、统一图谱,以及跨多智能体的治理层。
|
||
|
||
- **Context Time Machine → 托管历史** — 快照引擎、仪表盘回放、恢复及基于签名文件的共享/导入已交付(见上文);下一步是 `ctxpkg.com` 注册表,用于托管、版本化的上下文历史,以及并排模型视图 | git-diff 回放。时间轴贯穿 LeanCTX 所做一切 — 它*决策、记忆、守护、验证并回放*。([概念 →](docs/concepts/context-time-machine.md))
|
||
- **Context as Code** — TOML 中的声明式流水线、配置文件与策略,像基础设施一样版本化
|
||
- **Unified Context Graph** — 代码、测试、提交、CI 运行与知识条目汇入单一语义图谱
|
||
- **Agent Harness** — 多智能体治理的角色、预算与工具权限
|
||
- **Context Observability** — 上下文消耗的 SLO、异常检测、OpenTelemetry / Prometheus 导出
|
||
|
||
完整路线图见 **[VISION.md](VISION.md)**。
|
||
|
||
## 工作原理(30 秒)
|
||
|
||
LeanCTX 在**两个平面**上运作 — 智能体*读取*什么,以及向模型*发送*什么:
|
||
|
||
```
|
||
read path: AI tool → (MCP tools + shell) → lean-ctx → your repo + CLI
|
||
wire path: AI tool → lean-ctx proxy → model provider (every request, compressed)
|
||
```
|
||
|
||
- **MCP server** *(读取路径)*:暴露 `ctx_*` 工具(读取模式、缓存、增量、搜索、记忆、多智能体)
|
||
- **Shell hook** *(读取路径)*:透明压缩常用命令,让 LLM 看到更少噪声
|
||
- **Request proxy** *(线路路径,可选)*:`lean-ctx proxy enable` 在智能体与模型之间放置本地代理,压缩**每个请求** — 系统提示、完整历史与工具结果 — 提示缓存安全,并计量 USD 花费。还可跨 OpenAI、Anthropic 与 Gemini 固定**单一推理力度级别**(`proxy.effort`)而不破坏该缓存,通过缓存安全的冗长度引导加计量 holdout 削减**输出** token,并将**易变字段**(日期、UUID、commit SHA)移出可缓存前缀,使稳定的系统提示终于可缓存。每次重写可逆(按内容寻址恢复),按契约字节稳定。与独立请求压缩代理(如 Headroom)同一层 — 无需再叠一层。
|
||
- **Property Graph**:多边代码图谱支撑影响分析、相关文件发现与搜索排序
|
||
- **Session memory**:持久化状态并支持结构化恢复,长跑任务永不「冷启动」
|
||
- **Context Manager**:浏览器仪表盘,实时查看上下文窗口内容
|
||
|
||
## 快速开始(30 秒)
|
||
|
||
```bash
|
||
# 1) Install (pick one)
|
||
curl -fsSL https://leanctx.com/install.sh | sh # universal (no Rust needed)
|
||
brew tap yvgude/lean-ctx && brew install lean-ctx # macOS / Linux
|
||
npm install -g lean-ctx-bin # Node.js
|
||
cargo install lean-ctx # Rust
|
||
pi install npm:pi-lean-ctx # Pi Coding Agent
|
||
|
||
# 2) One-command setup for your agent
|
||
lean-ctx wrap cursor # or: wrap claude / wrap codex / wrap vscode
|
||
|
||
# Done. Savings appear after your AI's first lean-ctx call.
|
||
lean-ctx gain
|
||
```
|
||
|
||
`lean-ctx wrap` 一条命令即可完成:安装 shell hooks、注册 MCP 服务器、配置 agent hooks、启动守护进程并验证连接。随时可用 `lean-ctx unwrap cursor` 撤销。
|
||
|
||
<details>
|
||
<summary><strong>备选方案:完全控制</strong></summary>
|
||
|
||
```bash
|
||
lean-ctx onboard # connect all detected AI tools (zero prompts)
|
||
lean-ctx setup # interactive wizard with every option
|
||
```
|
||
|
||
</details>
|
||
|
||
**在 Windows 上从源码构建?** 克隆仓库并在 PowerShell 中运行 `./install.ps1` —— 它会构建 release 二进制文件并安装到 Cargo 的 bin 目录(传入 `-BuildOnly` 可仅构建不安装)。
|
||
|
||
<details>
|
||
<summary><strong>故障排查 / 安全</strong></summary>
|
||
|
||
- 立即禁用(当前 shell):`lean-ctx-off`
|
||
- 以未压缩方式运行单条命令:`lean-ctx -c --raw "git status"`
|
||
- 仅在 AI agent 会话中启用:在 `~/.config/lean-ctx/config.toml` 中设置 `shell_activation = "agents-only"`
|
||
- 按项目覆盖配置:在项目根目录创建 `.lean-ctx.toml`(自动与全局配置合并)
|
||
- 共享 `/workspace` 的 Docker 项目:创建 `.lean-ctx-id` 并使用唯一名称,以避免上下文冲突
|
||
- 更新:`lean-ctx update`
|
||
- 诊断(可分享):`lean-ctx doctor --json`
|
||
|
||
</details>
|
||
|
||
## 在你的代码中使用(SDK)
|
||
|
||
除 CLI 外,lean-ctx 还提供已发布的库,可直接从应用中调用。
|
||
|
||
**即插即用的 prompt 压缩 — [`lean-ctx-sdk`](https://pypi.org/project/lean-ctx-sdk/) ([npm](https://www.npmjs.com/package/lean-ctx-sdk)).** 在对话式 `messages` 数组到达任何模型之前进行压缩——确定性且对 prompt 缓存友好;图片、tool-calls 和 id 均原样透传。
|
||
|
||
```python
|
||
# pip install lean-ctx-sdk
|
||
from lean_ctx import compress
|
||
messages = compress(messages, model="claude-sonnet-4")
|
||
```
|
||
|
||
```ts
|
||
// npm install lean-ctx-sdk
|
||
import { compress } from "lean-ctx-sdk";
|
||
messages = await compress(messages, { model: "gpt-4o" });
|
||
```
|
||
|
||
包含框架适配器(LiteLLM、LangChain、Vercel AI SDK)。→ **[compress() cookbook](docs/guides/compress-sdk.md)**
|
||
|
||
**轻量 `/v1` 契约客户端 — [`lean-ctx-client`](https://pypi.org/project/lean-ctx-client/) ([npm](https://www.npmjs.com/package/lean-ctx-client) · [crates.io](https://crates.io/crates/lean-ctx-client)).** 在进程边界上封装完整的 `/v1` 工具、事件与会话 API——不链接引擎,因此在 lean-ctx 演进时保持稳定。
|
||
|
||
```bash
|
||
pip install lean-ctx-client # Python (imports as `leanctx`)
|
||
npm install lean-ctx-client # TypeScript / Node
|
||
cargo add lean-ctx-client # Rust
|
||
```
|
||
|
||
使用 `lean-ctx serve` 启动服务器,然后将客户端指向它。→ **[API 参考](https://leanctx.com/docs/api-reference/)**
|
||
|
||
## 真实场景
|
||
|
||
LeanCTX 会随你一起成长。以下是大多数人实际会经历的路径——每条都链接到 **[参考文档](docs/reference/README.md)** 中的完整、逐项功能讲解(所有 CLI 命令及全部 79 个 MCP 工具均记录于其中)。
|
||
|
||
<table>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🟢 最初 30 秒
|
||
*「刚装好——接下来做什么?」*
|
||
|
||
```bash
|
||
lean-ctx wrap cursor # one-command setup for your agent
|
||
lean-ctx doctor # confirm you're wired up
|
||
```
|
||
一条命令即可安装 hooks、完成 MCP 注册并验证连接。
|
||
→ **[旅程 1 — 安装与入门](docs/reference/01-setup-and-onboarding.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 📖 日常编码
|
||
*「别再反复读同一批文件。」*
|
||
|
||
```bash
|
||
lean-ctx read src/server.rs -m map # API surface, ~13 tok on re-read
|
||
lean-ctx -c "git status" # compressed shell output
|
||
```
|
||
你的 agent 自动读得更少、搜得更准。
|
||
→ **[旅程 2 — 日常使用](docs/reference/02-daily-use.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🧠 从上次中断处继续
|
||
*「新对话把之前全忘了。」*
|
||
|
||
```bash
|
||
lean-ctx overview # task-aware project recap
|
||
lean-ctx knowledge recall "auth" # facts that survive resets
|
||
lean-ctx knowledge consolidate # import session + compact lifecycle
|
||
lean-ctx knowledge consolidate --all # compact every project store
|
||
```
|
||
会话记忆 + 项目知识图谱可在多次对话间持久保存。
|
||
→ **[旅程 3 — 记忆与知识](docs/reference/03-memory-and-knowledge.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🗺️ 理解新代码库
|
||
*「这个函数会影响到哪里?」*
|
||
|
||
```bash
|
||
lean-ctx graph impact src/auth.rs # blast radius
|
||
lean-ctx smells scan # code-smell hotspots
|
||
```
|
||
多边属性图(property graph)支撑影响分析 + 排序搜索。
|
||
→ **[旅程 4 — 代码智能](docs/reference/04-code-intelligence.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🔌 提供方与多仓库
|
||
*「拉取 GitHub issues 和我们的 Postgres schema。」*
|
||
|
||
```bash
|
||
lean-ctx provider list
|
||
lean-ctx serve --root ./api --root ./web # multi-repo
|
||
```
|
||
外部数据经同一整合流水线流入。
|
||
→ **[旅程 5 — 高级功能与集成](docs/reference/05-advanced.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🛠️ 保持健康运行
|
||
*「更新、修复或干净卸载。」*
|
||
|
||
```bash
|
||
lean-ctx doctor --fix
|
||
lean-ctx update
|
||
```
|
||
自愈式诊断;精准卸载,仅移除自身相关块。
|
||
→ **[旅程 6 — 生命周期与故障排查](docs/reference/06-lifecycle.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🎛️ 掌控上下文窗口
|
||
*「像高手一样规划上下文预算。」*
|
||
|
||
```bash
|
||
lean-ctx plan "refactor billing" --budget 8000
|
||
lean-ctx compile --mode balanced
|
||
```
|
||
Phi 评分规划 + 背包(knapsack)编译 + 上下文账本。
|
||
→ **[旅程 7 — 上下文工程](docs/reference/07-context-engineering.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🤝 运行 agent 团队
|
||
*「同一仓库上的规划者 + 编码者 + 审查者。」*
|
||
|
||
```text
|
||
ctx_agent action=register role=dev
|
||
ctx_handoff action=create # baton-pass with full context
|
||
```
|
||
共享消息总线、日记、知识库及确定性交接。
|
||
→ **[旅程 8 — 多 Agent 协作](docs/reference/08-multi-agent.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🏢 团队 / CI 共享
|
||
*「一套共享索引,在流水线中以无头模式运行。」*
|
||
|
||
```bash
|
||
lean-ctx team serve --config team.toml
|
||
lean-ctx bootstrap # zero-prompt CI setup
|
||
```
|
||
作用域 token、可选云同步、可验证的上下文门控。
|
||
→ **[旅程 9 — 团队、云与 CI](docs/reference/09-team-cloud-ci.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 🎚️ 调优与治理
|
||
*「让它完全按我们的方式运行。」*
|
||
|
||
```bash
|
||
lean-ctx compression standard
|
||
lean-ctx harden # enforce token discipline
|
||
```
|
||
压缩级别、工具配置、主题与规则治理。
|
||
→ **[旅程 10 — 自定义与治理](docs/reference/10-customization-and-governance.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td width="50%" valign="top">
|
||
|
||
### 📊 证明收益
|
||
*「给我看数据。」*
|
||
|
||
```bash
|
||
lean-ctx gain --deep # savings, cost, per-agent, heatmap
|
||
lean-ctx wrapped # shareable recap (also: gain --svg / gain --share)
|
||
lean-ctx savings # verified per-event ledger (auditable; savings verify)
|
||
```
|
||
所有分析均在 CLI/仪表板中——绝不消耗 agent token。
|
||
→ **[旅程 11 — 分析与洞察](docs/reference/11-analytics-and-insights.md)**
|
||
|
||
</td>
|
||
<td width="50%" valign="top">
|
||
|
||
### 📚 完整参考
|
||
*「我想通读全部内容。」*
|
||
|
||
所有命令及全部 81 个 MCP 工具,按用户旅程组织,并附有 [CLI 映射](docs/reference/appendix-cli-map.md)、
|
||
[MCP 工具](docs/reference/appendix-mcp-tools.md) 与
|
||
[路径与配置](docs/reference/appendix-paths-and-config.md) 附录。
|
||
→ **[参考索引](docs/reference/README.md)**
|
||
|
||
</td>
|
||
</tr>
|
||
</table>
|
||
|
||
## 支持的 IDE 与 AI 工具
|
||
|
||
LeanCTX 是标准 **MCP 服务器**,可与任何兼容 MCP 的客户端配合使用。系统会按 agent 自动选择两种集成模式之一:
|
||
|
||
| 模式 | 工作原理 | 最适合 |
|
||
|---|---|---|
|
||
| **Hybrid(混合)** | MCP 用于缓存读取(约 13 tokens)+ shell hooks 压缩命令 | 具备 shell 访问权限的 agent(Cursor、Claude Code、Codex 等) |
|
||
| **MCP** | 通过 MCP 协议提供全部 80 个工具,无 shell hooks | 仅支持协议的 agent(JetBrains、VS Code、Zed 等) |
|
||
|
||
### 智能体兼容性矩阵
|
||
|
||
| 智能体 | 混合模式 | MCP | 设置 |
|
||
|---|:---:|:---:|---|
|
||
| Cursor | ● | | `lean-ctx init --agent cursor` |
|
||
| Claude Code | ● | | `lean-ctx init --agent claude` |
|
||
| CodeBuddy | ● | | `lean-ctx init --agent codebuddy` |
|
||
| Augment CLI / VS Code | ● | | `lean-ctx init --agent augment` |
|
||
| Codex CLI | ● | | `lean-ctx init --agent codex` |
|
||
| Gemini CLI | ● | | `lean-ctx init --agent gemini` |
|
||
| Windsurf | ● | | `lean-ctx init --agent windsurf` |
|
||
| GitHub Copilot | ● | | `lean-ctx init --agent copilot` |
|
||
| CRUSH | ● | | `lean-ctx init --agent crush` |
|
||
| Hermes | ● | | `lean-ctx init --agent hermes` |
|
||
| OpenCode | ● | | `lean-ctx init --agent opencode` |
|
||
| Pi | ● | | `lean-ctx init --agent pi` |
|
||
| Qoder | ● | | `lean-ctx init --agent qoder` |
|
||
| Amp | ● | | `lean-ctx init --agent amp` |
|
||
| Cline | ● | | `lean-ctx init --agent cline` |
|
||
| Roo Code | ● | | `lean-ctx init --agent roo` |
|
||
| Kiro | ● | | `lean-ctx init --agent kiro` |
|
||
| Antigravity | ● | | `lean-ctx init --agent antigravity` |
|
||
| Amazon Q | ● | | `lean-ctx init --agent amazonq` |
|
||
| Qwen | ● | | `lean-ctx init --agent qwen` |
|
||
| Trae | ● | | `lean-ctx init --agent trae` |
|
||
| Verdent | ● | | `lean-ctx init --agent verdent` |
|
||
| Aider | | ● | `lean-ctx init --agent aider` |
|
||
| Continue | | ● | `lean-ctx init --agent continue` |
|
||
| JetBrains IDEs | | ● | `lean-ctx init --agent jetbrains` |
|
||
| QoderWork | | ● | `lean-ctx init --agent qoderwork` |
|
||
| VS Code | | ● | `lean-ctx init --agent vscode` |
|
||
| Zed | | ● | `lean-ctx init --agent zed` |
|
||
| Neovim | | ● | `lean-ctx init --agent neovim` |
|
||
| Emacs | | ● | `lean-ctx init --agent emacs` |
|
||
| Sublime Text | | ● | `lean-ctx init --agent sublime` |
|
||
|
||
> **任何兼容 MCP 的客户端**均可开箱即用 — 上表列出的是支持一流自动配置的智能体。
|
||
|
||
### 何时适用(何时不适用)
|
||
|
||
**非常适合,如果你……**
|
||
- 每天使用 AI 编程工具,且会话高度依赖 shell(git/测试/构建)
|
||
- 在中大型仓库中工作(50+ 文件 / 单体仓库)
|
||
- 需要默认**无遥测**的本地优先层
|
||
|
||
**可以跳过,如果你……**
|
||
- 主要在小型仓库中工作,且很少从 AI 工具调用 shell
|
||
- 总是需要原始/未过滤的日志(你仍可使用 `--raw`,但投资回报率较低)
|
||
|
||
实话实说的细节:收益取决于三个杠杆 — **覆盖范围**(通过代理/引擎掌控窗口,而不仅是 `ctx_*` 工具层)、**上下文生命周期**(一个长生命周期会话 vs. 每个阶段一个新进程),以及**提供商定价**(按 prompt cache 计价 vs. 每轮重新计费)。三者对齐时叠加为明显收益,不对齐时净效果为**收支平衡**。完整分析与各场景的调优方法见[收益 vs. 收支平衡矩阵](docs/reference/14-performance-tuning.md#win-vs-break-even-at-a-glance)。
|
||
|
||
<a id="demo"></a>
|
||
|
||
## 演示
|
||
|
||
可在任意仓库中尝试:
|
||
|
||
```bash
|
||
lean-ctx read rust/src/server/mod.rs -m map
|
||
lean-ctx -c "git log -n 5 --oneline"
|
||
lean-ctx gain --live
|
||
lean-ctx dashboard # Context Manager (browser)
|
||
lean-ctx watch # TUI monitor
|
||
lean-ctx benchmark report .
|
||
```
|
||
|
||
- 本仓库附带用于渲染 GIF 的完整录制脚本,见 `demo/`
|
||
- 本地重新生成:
|
||
|
||
```bash
|
||
vhs demo/leanctx.tape
|
||
vhs demo/gain.tape
|
||
vhs demo/benchmark.tape
|
||
```
|
||
|
||
<a id="benchmarks"></a>
|
||
|
||
## 基准测试
|
||
|
||
真实、可复现的数据 — 从不估算。在本仓库上使用 GPT-4o 分词器(`o200k_base`)测量;未安装的工具会如实报告,从不猜测。
|
||
|
||
| 读取模式 | 压缩率 | Token 数(50 个文件) | 质量 |
|
||
|---|---:|---:|---:|
|
||
| Raw read | 0% | 533.2K | 100% |
|
||
| `map` | **98.1%** | 8.0K | 78% |
|
||
| `signatures` | **96.7%** | 14.0K | 96% |
|
||
| Cached re-read | ~99.99% | ~13 tok | 100% |
|
||
|
||
lean-ctx **自身成本也有测量**:其注入的固定每会话占用(公布的工具 schema + MCP 指令)约为 2.1K token,并通过 `lean-ctx doctor overhead --gate` 进行 CI 门禁,因此只会缩减。长生命周期代理通道还有确定性自验证 — `lean-ctx benchmark dual-arm --json` 重放 15 轮会话并按模型计价(摘要 `f5ed145e61ce3689`,在按 cache 计价的通道上输入侧节省 99.4%;方法论:[bench/agent-task/r2](bench/agent-task/r2/README.md))。
|
||
|
||
准确性不是凭感觉:有损阶段均受 **CI 门禁**约束。无模型的 A/B 门禁证明 JSON crusher 在削减 token 的同时保留*每一个*黄金答案,且代理重写按合约保持字节稳定,因此 Anthropic(90%)/ OpenAI(50%)的 prompt-cache 折扣在压缩后依然有效。确定性 **关闭 vs. 开启测试台**(`lean-ctx eval testbench`)将证明扩展到*答案*:它在相同 token 预算下,让固定的真实仓库分别经过 raw-dump 基线与 lean-ctx,用 LLM 评判器对自由形式问答评分、用各仓库自有测试对代码评分,并输出 `FINDINGS.md`(token / 轮次 / 墙钟时间 / 质量)及回归文件 — 其中包含已提交的录制子集,任何回归都会阻断 CI。
|
||
|
||
- **最新快照**:[BENCHMARKS.md](BENCHMARKS.md)
|
||
- **复现**:`lean-ctx benchmark report .`
|
||
|
||
## 数据一览
|
||
|
||
- **3,000+ GitHub stars** — 且仍在增长
|
||
- **280+ forks** — 活跃的社区贡献
|
||
- **200+ releases** — 自发布以来近乎每日发布
|
||
- **30+ 支持的 AI 编程智能体** — 最广的 MCP 兼容性
|
||
- **81 个 MCP 工具** — 从简单文件读取到多智能体编排
|
||
- 生产环境中被每天使用 Claude Code、Cursor 和 Codex 的团队采用
|
||
- **实时采用指标**:[leanctx.com/metrics](https://leanctx.com/metrics/) — 安装量、star 数与节省量,持续更新
|
||
|
||
## 文档
|
||
|
||
- **参考文档(按用户旅程组织的每个功能)**:[docs/reference/](docs/reference/README.md) — 11 条旅程 + CLI/MCP/配置附录
|
||
- **面向 AI 智能体 / LLM**:[llms.txt](llms.txt) — lean-ctx 的精选、机器可读地图(遵循 [llms.txt](https://llmstxt.org) 约定)
|
||
- 入门指南:https://leanctx.com/docs/getting-started
|
||
- 工具参考:https://leanctx.com/docs/tools/
|
||
- CLI 参考:https://leanctx.com/docs/cli-reference/
|
||
- 什么是 LeanCTX:https://leanctx.com/what-is-leanctx/
|
||
- 对比(vs RTK、Context+、MemGPT):https://leanctx.com/compare/
|
||
- 定价与 Cloud(本地使用永久免费):https://leanctx.com/pricing/
|
||
- FAQ:[discord-faq.md](discord-faq.md)
|
||
- 功能目录(SSOT 快照):[LEANCTX_FEATURE_CATALOG.md](LEANCTX_FEATURE_CATALOG.md)
|
||
- 单体仓库指南:[docs/guides/monorepo.md](docs/guides/monorepo.md)
|
||
- 架构:[ARCHITECTURE.md](ARCHITECTURE.md)
|
||
- 愿景:[VISION.md](VISION.md)
|
||
|
||
## 隐私与安全
|
||
|
||
- **默认无遥测**
|
||
- **可选的匿名统计共享**(安装时选择加入)
|
||
- **可禁用的更新检查**(配置 `update_check_disabled = true` 或 `LEAN_CTX_NO_UPDATE_CHECK=1`)
|
||
- v3.5.16 中 **40+ 项安全加固修复**(路径遍历、注入、CSPRNG、CSP、资源限制 — [详情](CHANGELOG.md))
|
||
- **上下文治理基准(Context Governance Benchmark)自评**:对照 32 项控制的 [CGB v1.0-draft](https://github.com/yvgude/context-governance-benchmark) 规范评为 **C2 — Managed**,已声明差距 — [docs/compliance/cgb-self-assessment.md](docs/compliance/cgb-self-assessment.md)
|
||
- 本地运行;除非你显式启用云同步,否则代码不会离开你的机器
|
||
|
||
详见 [SECURITY.md](SECURITY.md)。
|
||
|
||
## 卸载
|
||
|
||
一条命令即可移除**一切** — 停止所有进程,然后删除 hooks、编辑器配置、规则、自启动项(LaunchAgent/systemd)、数据目录,**以及二进制文件本身**:
|
||
|
||
```bash
|
||
lean-ctx uninstall # full clean removal
|
||
lean-ctx uninstall --dry-run # preview every change, write nothing
|
||
lean-ctx uninstall --keep-config # keep MCP configs + rules (for reinstall)
|
||
lean-ctx-off # or just disable for the current shell session
|
||
```
|
||
|
||
PATH 上没有二进制文件(或你使用了 curl 安装器)?从安装器运行相同的移除流程:
|
||
|
||
```bash
|
||
curl -fsSL https://leanctx.com/install.sh | sh -s -- --uninstall
|
||
```
|
||
|
||
若通过包管理器安装,`uninstall` 会移除其写入的一切,并告知你完成删除二进制文件所需的一条命令:
|
||
|
||
```bash
|
||
brew uninstall lean-ctx # Homebrew
|
||
cargo uninstall lean-ctx # cargo install
|
||
npm uninstall -g lean-ctx-bin # npm
|
||
pi uninstall npm:pi-lean-ctx # Pi Coding Agent
|
||
```
|
||
|
||
## Star 历史
|
||
|
||
<a href="https://star-history.com/#yvgude/lean-ctx&Date">
|
||
<picture>
|
||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=yvgude/lean-ctx&type=Date&theme=dark" />
|
||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=yvgude/lean-ctx&type=Date" />
|
||
<img alt="Star 历史图表" src="https://api.star-history.com/svg?repos=yvgude/lean-ctx&type=Date" />
|
||
</picture>
|
||
</a>
|
||
|
||
## 贡献
|
||
|
||
请从 [CONTRIBUTING.md](CONTRIBUTING.md) 开始。适合新手的首个 PR:通过 [issue 模板](.github/ISSUE_TEMPLATE/compression_pattern.md) 提议新的 CLI 压缩模式(compression pattern)。
|
||
|
||
## 许可证
|
||
|
||
Apache License 2.0 — 详见 [LICENSE](LICENSE)。
|