<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; Manus</title>
	<atom:link href="https://www.phppan.com/tag/manus/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>从 LangChain 和 Manus 两巨头上下文工程实战中学到的 5 点经验</title>
		<link>https://www.phppan.com/2025/10/langchain-manus-five-lessons/</link>
		<comments>https://www.phppan.com/2025/10/langchain-manus-five-lessons/#comments</comments>
		<pubDate>Sat, 25 Oct 2025 12:54:41 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[架构和远方]]></category>
		<category><![CDATA[AIAgent]]></category>
		<category><![CDATA[AIAgent架构]]></category>
		<category><![CDATA[Manus]]></category>

		<guid isPermaLink="false">https://www.phppan.com/?p=2426</guid>
		<description><![CDATA[最近仔细看了 LangChain 的 Lance 和 Manus 的 Pete 的视频，主题是构建 AI 智能 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p style="color: #424b5d;" data-tool="mdnice编辑器">最近仔细看了 LangChain 的 Lance 和 Manus 的 Pete 的视频，主题是构建 AI 智能体中的上下文工程。这次分享没有那些空泛的理论，全是在生产环境中验证过的实战经验。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">我结合自己的思考，整理了我认为 5 个最有价值的技术要点，每一个都直接影响着智能体的性能和可用性。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">1. 上下文爆炸是智能体最大的瓶颈</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Manus 统计显示，一个典型的智能体任务需要约 50 次工具调用。Anthropic 也提到，生产环境中的智能体对话可能持续数百轮。每次工具调用都会返回结果，这些结果会不断累积在消息历史中。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">问题在于，随着上下文长度增加，模型性能会明显下降。Anthropic 把这个现象称为&#8221;上下文腐烂&#8221;（context rot）。具体表现是模型开始重复输出、推理速度变慢、回答质量下降。大部分模型在 200k Token 左右就开始出现这些问题，远低于它们宣称的上限。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">这就形成了一个矛盾：智能体需要大量上下文来保持工作状态的连续性，但上下文太长又会导致性能下降。这是所有开发智能体的团队都会遇到的核心挑战。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Lance 在分享中展示了他们 Open Deep Research 项目的数据。这个开源项目在 Deep Research Bench 测试中排名前十，但即使是这样优秀的实现，也需要在三个阶段中不断地卸载和压缩上下文才能保持高效运行。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 则分享了 Manus 的经验。他们发现，如果不做任何优化，上下文会在几十轮对话后就达到模型的处理极限。更糟糕的是，用户往往在这时才开始进入任务的核心部分。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">解决这个问题没有捷径，必须从架构设计开始就考虑上下文管理。这也是为什么&#8221;上下文工程&#8221;这个概念在今年 5 月开始迅速流行的原因。它不是可选的优化项，而是构建生产级智能体的必要条件。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">2. 上下文工程的五大组成部分</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Lance 和 Pete 在分享中系统地总结了上下文工程的五个核心组成部分，这些部分相互关联，形成了一个完整的技术体系。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>上下文卸载</strong> 是把部分信息移出主消息历史，存储到外部系统中。最常见的是使用文件系统。当搜索工具返回大量结果时，不是把完整内容放在消息队列里，而是写入文件，只在上下文中保留文件路径。Claude Code、Manus、Open Deep Research 项目都大量使用这种方法。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>上下文缩减</strong> 包括压缩和摘要两种策略。压缩是去掉可以从外部重建的信息，摘要是对信息进行不可逆的精简。Claude 3.5 Sonnet 已经内置了修剪旧工具调用的功能。Cognition 在智能体交接时也会触发摘要。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>上下文检索</strong> 解决如何按需获取被卸载的信息。有两派做法：Cursor 使用索引加语义搜索，Claude Code 和 Manus 只用文件系统加 grep、glob 这样的简单工具。两种方法各有优劣，选择哪种取决于具体场景。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>上下文隔离</strong> 通过多智能体架构实现关注点分离。每个子智能体有自己的上下文窗口，避免信息混杂。Manus 的 Wide Agents、LangChain 的 Deep Agents、Claude 的多智能体研究员都采用了这种设计。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>上下文缓存</strong> 利用 KV 缓存等机制减少重复计算。Anthropic 等服务商提供的缓存功能对长上下文智能体至关重要，特别是在输入远长于输出的场景下。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">这五个部分不是独立的，而是相互影响的系统。卸载和检索让缩减更高效，稳定的检索让隔离变得安全。但隔离会减慢上下文传递，降低缩减频率。更多的隔离和缩减又会影响缓存效率。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 特别强调，这种相互依赖意味着你不能只优化某一个方面，必须从整体考虑。比如，如果你的检索机制不够稳定，就不能激进地进行上下文隔离，否则子智能体可能无法获取必要的信息。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">3. 上下文管理和数据库系统的对比</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">在分享中我个人的感觉是上下文工程的设计和数据库系统设计有比较多的相似性。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">上下文卸载和数据库索引有很多相似之处，但它们的设计目标和实现机制存在本质差异。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">展开来讲：<strong>相似性是都是为了解决容量与性能的矛盾</strong></p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库索引解决的是「如何在海量数据中快速定位」的问题，而上下文卸载解决的是「如何在有限窗口中访问无限信息」的问题。两者都是通过建立某种间接引用机制，让系统能够处理超出直接处理能力的数据量。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库有内存中的索引页、磁盘上的索引文件、以及实际的数据页。上下文工程也有类似的层次：活跃上下文（相当于内存中的热数据）、文件路径引用（相当于索引）、文件系统中的完整内容（相当于磁盘数据）。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库索引针对不同的查询模式有不同类型：B 树索引适合范围查询，哈希索引适合等值查询，全文索引适合文本搜索。上下文卸载也有类似的分类：代码文件用路径索引，搜索结果用语义向量索引，对话历史用时间戳索引。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库的 buffer pool 会缓存频繁访问的索引页和数据页。上下文工程中的 KV 缓存也会保留频繁引用的上下文片段。两者都使用 LRU 或类似算法决定淘汰策略。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关键差异：<strong>索引是查询优化，卸载是容量管理</strong></p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库索引的主要目标是加速查询，即使没有索引，全表扫描也能得到结果，只是慢一些。而上下文卸载是为了突破硬性限制，没有卸载机制，超出窗口的信息就完全无法访问。这是「优化」与「必需」的区别。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库索引是冗余信息，删除索引不会丢失数据，只会影响查询性能。但上下文卸载中，如果文件系统中的内容丢失，而上下文中只有路径引用，信息就永久丢失了。这就是为什么 Pete 强调卸载必须配合可靠的检索机制。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库索引需要随数据更新而维护，这是 ACID 事务的一部分。而上下文卸载通常是单向的：信息从上下文移到外部存储，很少需要反向更新。即使文件内容被修改，上下文中的引用通常保持不变。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">数据库可以选择性地为某些列建立索引，基于查询模式和数据分布做决策。而上下文卸载往往是强制的：当接近 Token 限制时，必须卸载某些内容，没有&#8221;不建索引&#8221;的选项。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">就像数据库有内存、磁盘、缓存的层次结构，上下文工程也有类似的分层：活跃上下文（内存）、文件系统（磁盘）、KV 缓存（缓存层）。数据库的查询优化对应着上下文检索，事务隔离对应着智能体隔离，数据压缩对应着上下文压缩。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 提到的&#8221;通过通信&#8221;和&#8221;通过共享内存&#8221;两种模式，实际上对应着数据库中的消息传递和共享存储两种架构。当子智能体需要完整历史记录时，使用共享上下文模式，这就像数据库中的共享存储架构。当任务相对独立时，使用通信模式，类似于分布式数据库的消息传递。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">智能体间的状态同步问题，本质上就是分布式系统的一致性问题。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Manus 的解决方案也借鉴了数据库的设计思想。他们使用结构化的模式（schema）来定义智能体间的通信接口，确保数据的一致性。他们的分层行为空间设计（函数调用、沙盒工具、软件包与API）类似于数据库的存储引擎分层。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">这种类比不仅仅是理论上的相似。在实践中，你可以直接应用数据库系统的很多优化技术。比如，使用 LRU 策略决定哪些上下文应该被压缩或摘要；使用预写日志（WAL）的思想，在摘要前先把完整上下文写入日志文件；使用索引来加速文件系统中的信息检索。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">我们可以借鉴数据库领域几十年的研究成果和工程实践，而不是从零开始摸索。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">3. 上下文工程的核心是做减法，而不是加法</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 在分享最后强调的这一点可能是最重要的：避免上下文过度工程（context over-engineering）。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">他提到，Manus 上线六七个月以来，最大的性能提升不是来自增加更复杂的上下文管理机制，而是来自简化架构、移除不必要的功能、对模型给予更多信任。他们已经重构了 Manus 五次，每次都是在做减法。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">比如，早期的 Manus 使用 todo.md 文件来管理任务列表，结果发现约三分之一的操作都在更新这个列表，浪费了大量 Token。后来他们改用更简单的结构化规划器，效果反而更好。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">另一个例子是工具选择。他们没有使用复杂的语义搜索来动态加载工具，而是固定使用 10-20 个原子功能，其他所有功能都通过沙盒中的命令行工具或 Python 脚本来实现。这种分层的行为空间设计，让系统既灵活又简单。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 还提到了一个评估架构的方法：在强弱模型之间切换测试。如果你的架构从弱模型切换到强模型后能获得明显提升，说明架构有较好的未来适应性。因为明年的弱模型可能就和今天的强模型一样好。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于是否要训练专门的模型，Pete 的观点也很明确：初创公司应该尽可能长时间地依赖通用模型和上下文工程。训练专门模型不仅成本高，而且在产品快速迭代的阶段，很可能在优化错误的指标。MCP（Model Context Protocol）的推出就是个例子，它彻底改变了 Manus 的设计，如果之前投入大量资源训练专门模型，这些投入就浪费了。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">他们现在也不使用开源模型，不是因为质量问题，而是成本问题。对于输入远长于输出的智能体应用，KV 缓存至关重要。大型云服务商有更好的分布式缓存基础设施，算下来使用他们的 API 反而更便宜。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">这些经验告诉我们：上下文工程的目标是让模型的工作变得更简单，而不是更复杂。不要为了优化而优化，每个设计决策都要基于实际的性能数据和用户反馈。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">4. 压缩与摘要的本质区别决定了使用策略</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 详细解释了压缩和摘要这两种看似相似实则截然不同的策略，这个区分对实际应用至关重要。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>压缩是可逆的</strong>。在 Manus 中，每个工具调用都有完整格式和紧凑格式。比如写文件操作，完整格式包含路径和内容，紧凑格式只保留路径。因为文件已经存在于文件系统中，通过路径可以随时恢复完整信息。这种可逆性至关重要，因为你永远不知道哪个历史操作会在后续变得重要。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>摘要是不可逆的</strong>。一旦执行摘要，原始信息就永久丢失了。因此必须非常谨慎。Manus 的做法是在摘要前将完整上下文转储为日志文件，作为最后的保险。而且摘要时要保留最新几次工具调用的完整信息，让模型知道当前的工作状态。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于阈值的确定，Pete 分享了具体数据：大多数模型在 200k Token 左右开始出现性能下降，他们将 128k-200k 设定为&#8221;腐烂前&#8221;阈值。这不是随意设定的，而是通过大量评估得出的。他们测试了不同长度下模型的重复率、推理速度、输出质量等指标。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">触发策略是渐进式的：</p>
<ul class="list-paddingleft-1" style="color: #000000;">
<li>
<section style="color: #424b5d;">接近 128k 时，开始压缩最旧的 50% 工具调用</section>
</li>
<li>
<section style="color: #424b5d;">压缩后检查实际释放的空间</section>
</li>
<li>
<section style="color: #424b5d;">如果压缩效果不明显（因为即使紧凑格式也占用空间），才考虑摘要</section>
</li>
<li>
<section style="color: #424b5d;">摘要时使用完整版本而非压缩版本的数据</section>
</li>
<li>
<section style="color: #424b5d;">始终保留最新的几次完整工具调用</section>
</li>
</ul>
<p style="color: #424b5d;" data-tool="mdnice编辑器">尽可能保留信息的完整性，只在必要时才接受信息损失。这就像数据压缩算法中的无损压缩和有损压缩，你总是先尝试无损方案。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">不同类型的内容有不同的压缩潜力。搜索结果、API 响应这类内容压缩效果好，因为大部分信息可以通过查询重新获取。而用户的指令、模型的推理过程压缩空间有限，因为这些信息往往都是必要的。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">5. 隔离策略的选择取决于任务特性而非直觉</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">多智能体隔离是个老话题，但 Pete 分享的经验推翻了很多常见做法。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">首先是<strong>反模式的识别</strong>。很多团队喜欢按人类角色划分智能体：设计师智能体、程序员智能体、经理智能体等。Pete 明确指出这是个陷阱。这种划分源于人类组织的局限性（个人能力和注意力有限），而不是 AI 系统的最优设计。Manus 只有极少的几个功能性智能体：通用执行器、规划器、知识管理器。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于隔离模式的选择，Pete 区分了两种场景：</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>通信模式</strong>适合结果导向的独立任务。主智能体发送明确指令，子智能体独立完成，返回结果。整个过程主智能体不关心细节，只要最终输出。比如&#8221;在代码库中搜索特定函数&#8221;、&#8221;将这段文本翻译成中文&#8221;。这种模式的优势是上下文干净、成本低、易于调试。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>共享上下文模式</strong>适合需要完整历史信息的复杂任务。子智能体能看到所有历史对话和工具调用，但有自己的系统提示和工具集。比如深度研究任务，最终报告需要参考大量中间搜索和笔记。这种模式成本高（需要预填充大量上下文，无法复用 KV 缓存），但对某些任务是必要的。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">判断使用哪种模式的关键是<strong>从结果推理过程的依赖性</strong>。如果最终输出强依赖于中间步骤的细节，就需要共享上下文。如果只需要最终结果，通信模式就足够了。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">Pete 还提到了一个实用技巧：&#8221;智能体即工具&#8221;（agent as tool）。从主智能体视角，调用子智能体就像调用普通工具，有明确的输入输出接口。这简化了系统设计，也让调试更容易。Manus 的&#8221;高级搜索&#8221;功能就是这样实现的，表面上是个工具，实际是个完整的子智能体工作流。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">他们解决通信问题的方法是强制使用结构化输出。主智能体调用子智能体前必须定义输出模式，子智能体通过专门的&#8221;提交结果&#8221;工具返回数据，系统用约束解码确保格式正确。这避免了自由格式通信带来的解析问题和信息丢失。</p>
<h1 style="color: #000000;" data-tool="mdnice编辑器"><span style="font-weight: bold; color: #e7642b;">6. 一些额外的技术洞察</span></h1>
<p style="color: #424b5d;" data-tool="mdnice编辑器">除了这五个核心经验，还有一些值得记录的技术细节。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>信息的局部性</strong>。使用基于行的格式意味着每行都是独立的信息单元，读取第 100 行不需要解析前 99 行。这对于大文件的随机访问至关重要。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器"><strong>结构化不等于复杂格式</strong>。Pete 强调，他们优先使用基于行（line-based）的纯文本格式，而不是 JSON 或 XML。原因很简单：模型可以用 grep 搜索特定行，用 sed 修改特定内容，用 head/tail 读取部分数据。这些都是模型已经掌握的基础工具。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于 Markdown 的使用要特别谨慎。虽然模型都接受过 Markdown 训练，但过度使用会带来副作用。某些模型会因此输出过多的项目符号和格式化标记，浪费 Token 还影响可读性。Manus 的经验是：在需要结构时用 Markdown，在存储数据时用纯文本。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于<strong>模型切换的评估方法</strong>。Pete 提到，他们会在强弱模型间切换来评估架构的未来适应性。如果一个架构在弱模型上也能工作（虽然效果差一些），说明它不是过度依赖特定模型能力的脆弱设计。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于<strong>工具数量的上限</strong>，他们的经验是不超过 30 个。这不是任意数字，而是基于大量测试。超过这个数量，模型开始频繁调用错误的工具，甚至调用不存在的工具。解决方案不是动态加载工具，而是设计分层的行为空间：少量原子工具 + 沙盒命令 + 代码执行。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">关于<strong>评估体系</strong>，Manus 的三层评估值得借鉴：用户评分是北极星指标，自动化测试覆盖可验证的任务，真人评估处理主观性强的输出。学术基准测试只是参考，不能作为主要依据。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">上下文工程不是一些零散的技巧，而是一个需要系统思考的工程领域。每个设计决策都会影响到其他部分，没有放之四海而皆准的最佳实践，只有基于具体场景的权衡取舍。</p>
<p style="color: #424b5d;" data-tool="mdnice编辑器">以上。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2025/10/langchain-manus-five-lessons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agent 核心策略：Manus、Gemini CLI 和 Claude Code 的上下文压缩策略和细节</title>
		<link>https://www.phppan.com/2025/09/agent-core-strategy-context-compression-strategy-and-details-for-manus-gemini-cli-and-claude-code/</link>
		<comments>https://www.phppan.com/2025/09/agent-core-strategy-context-compression-strategy-and-details-for-manus-gemini-cli-and-claude-code/#comments</comments>
		<pubDate>Sat, 06 Sep 2025 14:31:40 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[架构和远方]]></category>
		<category><![CDATA[AIAgent]]></category>
		<category><![CDATA[Claude Code]]></category>
		<category><![CDATA[Gemini CLI]]></category>
		<category><![CDATA[Manus]]></category>

		<guid isPermaLink="false">https://www.phppan.com/?p=2415</guid>
		<description><![CDATA[做 AI Agent 开发的都需要考虑上下文爆炸的问题，不仅仅是成本问题，还有性能问题。 许多团队选择了压缩策 [&#8230;]]]></description>
				<content:encoded><![CDATA[<section id="nice" style="color: #000000;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com">
<p data-tool="mdnice编辑器">做 AI Agent 开发的都需要考虑上下文爆炸的问题，不仅仅是成本问题，还有性能问题。</p>
<p data-tool="mdnice编辑器">许多团队选择了压缩策略。但过度激进的压缩不可避免地导致信息丢失。</p>
<p data-tool="mdnice编辑器">这背后的根本矛盾在于：<strong style="color: #0e88eb;">Agent 需要基于完整的历史状态来决策下一步行动，但我们无法预知当前的某个观察细节是否会在未来的某个关键时刻变得至关重要。</strong></p>
<p data-tool="mdnice编辑器">前面一篇文章讲了整个上下文的管理策略，今天着重聊一下上下文管理的压缩策略和细节。</p>
<p data-tool="mdnice编辑器">下面根据 Manus、Gemini CLI 和 Claude Code 这三个项目的源码来聊一下上下文的压缩。</p>
<p data-tool="mdnice编辑器">三个产品，有三个不同的选择，或者说设计哲学。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">Manus 的选择是：永不丢失。</strong> 他们认为，从逻辑角度看，任何不可逆的压缩都带有风险。所以他们选择了一条看似&#8221;笨拙&#8221;但实际上很聪明的路：把文件系统当作&#8221;终极上下文&#8221;。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">Claude Code 的选择是：极限压榨。</strong> 92% 的压缩触发阈值，这个数字相当激进。他们想要榨干上下文窗口的每一个 token，直到最后一刻才开始压缩。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">Gemini CLI 的选择是：稳健保守。</strong> 70% 就开始压缩，宁可频繁一点，也要保证系统的稳定性。</p>
<p data-tool="mdnice编辑器">这三种选择没有对错，只是适用场景不同。接下来我们逐个分析。</p>
<h1 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">1. Manus</span></h1>
<p data-tool="mdnice编辑器">Manus 的压缩最让我印象深刻的是其在可恢复性上的策略。</p>
<p data-tool="mdnice编辑器">Manus 团队认为：任何不可逆的压缩都带有风险。你永远不知道现在丢掉的信息是不是未来解决问题的关键。</p>
<p data-tool="mdnice编辑器">他们选择了不做真正的删除，而是将其外部化存储。</p>
<p data-tool="mdnice编辑器">传统做法是把所有观察结果都塞进上下文里。比如让 Agent 读一个网页，它会把整个 HTML 内容都存下来，可能就是上万个 token。Manus 不这么干。</p>
<p data-tool="mdnice编辑器">当 Agent 访问网页时，Manus 只在上下文中保留 URL 和简短的描述，完整的网页内容并不保存。需要重新查看网页内容时，通过保留的 URL 重新获取即可。这就像是你在笔记本上记录&#8221;参见第 23 页的图表&#8221;，而不是把整个图表重新画一遍。</p>
<p data-tool="mdnice编辑器">文档处理也是同样的逻辑。一个 100 页的 PDF 文档，Manus 不会把全部内容放进上下文，而是只记录文档路径、页数、最后访问的位置等元信息。当 Agent 需要查看具体内容时，再通过文件路径读取相应的页面。</p>
<p data-tool="mdnice编辑器">Manus 把文件系统视为&#8221;终极上下文&#8221;——一个容量几乎无限、天然持久化、Agent 可以直接操作的外部记忆系统。</p>
<p data-tool="mdnice编辑器">文件系统的层级结构天然适合组织信息。Agent 可以创建不同的目录来分类存储不同类型的信息：项目背景放一个文件夹，技术细节放另一个文件夹，错误日志单独存放。需要时按图索骥，而不是在一个巨大的上下文中大海捞针。</p>
<p data-tool="mdnice编辑器">这不是简单的存储。Manus 训练模型学会主动使用文件系统来管理自己的「记忆」。当发现重要信息时，Agent 会主动将其写入特定的文件中，而不是试图把所有东西都记在上下文里。就像一个经验丰富的研究员，知道什么该记在脑子里，什么该写在笔记本上，什么该归档保存。</p>
<p data-tool="mdnice编辑器">在 Manus 团队对外的文章开头，指出了为什么必须要有压缩策略：</p>
<p data-tool="mdnice编辑器">第一，<strong style="color: #0e88eb;">观察结果可能非常庞大</strong>。与网页或 PDF 等非结构化数据交互时，一次观察就可能产生数万个 token。如果不压缩，可能一两次操作就把上下文占满了。</p>
<p data-tool="mdnice编辑器">第二，<strong style="color: #0e88eb;">模型性能会下降</strong>。这是个很多人忽视的问题。即使模型声称支持 200k 的上下文窗口，但实际使用中，超过一定长度后，模型的注意力机制效率会显著下降，响应质量也会变差。这就像人的工作记忆一样，信息太多反而会降低处理效率。</p>
<p data-tool="mdnice编辑器">第三，<strong style="color: #0e88eb;">成本考虑</strong>。长输入意味着高成本，即使使用了前缀缓存等优化技术，成本依然可观。特别是在需要大量交互的场景下，成本会快速累积。</p>
<p data-tool="mdnice编辑器">在具体实现过程中，可恢复压缩有几个关键点：</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">保留最小必要信息</strong>。对于每个外部资源，只保留能够重新获取它的最小信息集。网页保留 URL，文档保留路径，API 响应保留请求参数。这些信息占用的空间极小，但足以在需要时恢复完整内容。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">智能的重新加载时机</strong>。不是每次提到某个资源就重新加载，而是根据上下文判断是否真的需要详细内容。如果只是确认文件存在，就不需要读取内容；如果要分析具体细节，才触发加载。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">缓存机制</strong>。虽然内容不在上下文中，但 Manus 会在本地维护一个缓存。最近访问过的资源会暂时保留，避免频繁的重复加载。这个缓存是独立于上下文的，不占用宝贵的 token 额度。</p>
<h1 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2. Claude Code</span></h1>
<p data-tool="mdnice编辑器">Claude Code 的策略完全是另一个极端——他们要把上下文用到极致。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.1 92% 的阈值</span></h2>
<p data-tool="mdnice编辑器">这个数字有一些讲究。留 8% 的缓冲区既保证了压缩过程有足够的时间完成，又避免了频繁触发压缩带来的性能开销。更重要的是，这个缓冲区给了系统一个「反悔」的机会——如果压缩质量不达标，还有空间执行降级策略。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-keyword" style="color: #c678dd;">const</span> COMPRESSION_CONFIG = {
  <span class="hljs-attr" style="color: #d19a66;">threshold</span>: <span class="hljs-number" style="color: #d19a66;">0.92</span>,           <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 92%阈值触发</span>
  <span class="hljs-attr" style="color: #d19a66;">triggerVariable</span>: <span class="hljs-string" style="color: #98c379;">"h11"</span>,    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// h11 = 0.92</span>
  <span class="hljs-attr" style="color: #d19a66;">compressionModel</span>: <span class="hljs-string" style="color: #98c379;">"J7()"</span>,  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 专用压缩模型</span>
  <span class="hljs-attr" style="color: #d19a66;">preserveStructure</span>: <span class="hljs-literal" style="color: #56b6c2;">true</span>    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 保持8段结构</span>
};
</code></pre>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.2 八段式结构化摘要</span></h2>
<p data-tool="mdnice编辑器">Claude Code 的压缩不是简单的截断或摘要，而是八段式结构。这个结构我们可以学习一下：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-keyword" style="color: #c678dd;">const</span> COMPRESSION_SECTIONS = [
  <span class="hljs-string" style="color: #98c379;">"1. Primary Request and Intent"</span>,    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 主要请求和意图</span>
  <span class="hljs-string" style="color: #98c379;">"2. Key Technical Concepts"</span>,        <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 关键技术概念</span>
  <span class="hljs-string" style="color: #98c379;">"3. Files and Code Sections"</span>,       <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 文件和代码段</span>
  <span class="hljs-string" style="color: #98c379;">"4. Errors and fixes"</span>,              <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 错误和修复</span>
  <span class="hljs-string" style="color: #98c379;">"5. Problem Solving"</span>,               <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 问题解决</span>
  <span class="hljs-string" style="color: #98c379;">"6. All user messages"</span>,             <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 所有用户消息</span>
  <span class="hljs-string" style="color: #98c379;">"7. Pending Tasks"</span>,                 <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 待处理任务</span>
  <span class="hljs-string" style="color: #98c379;">"8. Current Work"</span>                   <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 当前工作</span>
];
</code></pre>
<p data-tool="mdnice编辑器">每一段都有明确的目的和优先级。「主要请求和意图」确保 Agent 永远不会忘记用户最初想要什么；「关键技术概念」保留重要的技术决策和约束条件；「错误和修复」避免重复踩坑；「所有用户消息」则保证用户的原始表达不会丢失。</p>
<p data-tool="mdnice编辑器">这种结构的好处是，即使经过多次压缩，Agent 仍然能保持工作的连贯性。关键信息都在，只是细节被逐步抽象化了。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.3 专用压缩模型 J7</span></h2>
<p data-tool="mdnice编辑器">Claude Code 使用了一个专门的压缩模型 J7 来处理上下文压缩。这不是主模型，而是一个专门优化过的模型，它的任务就是理解长对话并生成高质量的结构化摘要。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-keyword" style="color: #c678dd;">async</span> <span class="hljs-function"><span class="hljs-keyword" style="color: #c678dd;">function</span> <span class="hljs-title" style="color: #61aeee;">contextCompression</span>(<span class="hljs-params">currentContext</span>) </span>{
  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 检查压缩条件</span>
  <span class="hljs-keyword" style="color: #c678dd;">if</span> (currentContext.tokenRatio &lt; h11) {
    <span class="hljs-keyword" style="color: #c678dd;">return</span> currentContext;  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 无需压缩</span>
  }
  
  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 调用专用压缩模型</span>
  <span class="hljs-keyword" style="color: #c678dd;">const</span> compressionPrompt = <span class="hljs-keyword" style="color: #c678dd;">await</span> AU2.generatePrompt(currentContext);
  <span class="hljs-keyword" style="color: #c678dd;">const</span> compressedSummary = <span class="hljs-keyword" style="color: #c678dd;">await</span> J7(compressionPrompt);
  
  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 构建新的上下文</span>
  <span class="hljs-keyword" style="color: #c678dd;">const</span> newContext = {
    <span class="hljs-attr" style="color: #d19a66;">summary</span>: compressedSummary,
    <span class="hljs-attr" style="color: #d19a66;">recentMessages</span>: currentContext.recent(<span class="hljs-number" style="color: #d19a66;">5</span>),  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 保留最近5条</span>
    <span class="hljs-attr" style="color: #d19a66;">currentTask</span>: currentContext.activeTask
  };
  
  <span class="hljs-keyword" style="color: #c678dd;">return</span> newContext;
}
</code></pre>
<p data-tool="mdnice编辑器">AU2 负责生成压缩提示词，它会分析当前上下文，提取关键信息，然后构造一个结构化的提示词给 J7。J7 处理后返回符合八段式结构的压缩摘要。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.4 上下文生命周期管理</span></h2>
<p data-tool="mdnice编辑器">Claude Code 把上下文当作有生命周期的实体来管理。这个设计理念很先进——上下文不是静态的数据，而是动态演化的有机体。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-class"><span class="hljs-keyword" style="color: #c678dd;">class</span> <span class="hljs-title" style="color: #e6c07b;">ContextManager</span> </span>{
  <span class="hljs-keyword" style="color: #c678dd;">constructor</span>() {
    <span class="hljs-keyword" style="color: #c678dd;">this</span>.compressionThreshold = <span class="hljs-number" style="color: #d19a66;">0.92</span>;  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// h11 = 0.92</span>
    <span class="hljs-keyword" style="color: #c678dd;">this</span>.compressionModel = <span class="hljs-string" style="color: #98c379;">"J7"</span>;      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 专用模型</span>
  }
  
  <span class="hljs-keyword" style="color: #c678dd;">async</span> manageContext(currentContext, newInput) {
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 1. 上下文更新</span>
    <span class="hljs-keyword" style="color: #c678dd;">const</span> updatedContext = <span class="hljs-keyword" style="color: #c678dd;">this</span>.appendToContext(currentContext, newInput);
    
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 2. 令牌使用量检查</span>
    <span class="hljs-keyword" style="color: #c678dd;">const</span> tokenUsage = <span class="hljs-keyword" style="color: #c678dd;">await</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.calculateTokenUsage(updatedContext);
    
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 3. 压缩触发判断</span>
    <span class="hljs-keyword" style="color: #c678dd;">if</span> (tokenUsage.ratio &gt;= <span class="hljs-keyword" style="color: #c678dd;">this</span>.compressionThreshold) {
      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 4. 八段式压缩执行</span>
      <span class="hljs-keyword" style="color: #c678dd;">const</span> compressionPrompt = <span class="hljs-keyword" style="color: #c678dd;">await</span> AU2.generateCompressionPrompt(updatedContext);
      <span class="hljs-keyword" style="color: #c678dd;">const</span> compressedSummary = <span class="hljs-keyword" style="color: #c678dd;">await</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.compressionModel.generate(compressionPrompt);
      
      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 5. 新上下文构建</span>
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.buildCompressedContext(compressedSummary, updatedContext);
    }
    
    <span class="hljs-keyword" style="color: #c678dd;">return</span> updatedContext;
  }
}
</code></pre>
<p data-tool="mdnice编辑器">整个流程是自动化的。每次有新的输入，系统都会评估是否需要压缩。压缩不是一次性的动作，而是持续的过程。随着对话的进行，早期的详细内容会逐渐被抽象化，但关键信息始终保留。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.5 优雅降级机制</span></h2>
<p data-tool="mdnice编辑器">当压缩失败时，系统不会死板地报错或者强行应用低质量的压缩结果，而是有一整套 Plan B、Plan C。这种&#8221;永不放弃&#8221;的设计理念，让系统在各种极端情况下都能稳定运行。</p>
<p data-tool="mdnice编辑器">降级策略包括：</p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">自适应重压缩</strong>：如果首次压缩质量不佳，会调整参数重试</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">混合模式保留</strong>：压缩旧内容，但完整保留最近的交互</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">保守截断</strong>：最坏情况下，至少保证系统能继续运行</section>
</li>
</ul>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.6 压缩后的信息恢复</span></h2>
<p data-tool="mdnice编辑器">虽然 Claude Code 的压缩是有损的，但它通过巧妙的设计最小化了信息损失的影响。压缩后的八段式摘要不是简单的文本，而是结构化的信息，包含了足够的上下文让 Agent 能够理解之前发生了什么，需要做什么。</p>
<p data-tool="mdnice编辑器">特别值得一提的是第 6 段&#8221;All user messages&#8221;。即使其他内容被压缩了，用户的所有消息都会以某种形式保留。这确保了用户的意图和需求不会在压缩过程中丢失。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">2.7 实践指南</span></h2>
<p data-tool="mdnice编辑器">Claude Code 在实践中还有一些最佳实践：</p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">定期使用 <code style="color: #0e8aeb;">/compact</code> 命令压缩长对话</strong>：用户可以主动触发压缩，不必等到自动触发</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">在上下文警告出现时及时处理</strong>：系统会在接近阈值时发出警告，用户应该及时响应</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">通过 <code style="color: #0e8aeb;">Claude.md</code> 文件保存重要信息</strong>：将关键信息外部化，减少上下文消耗</section>
</li>
</ul>
<h1 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3. Gemini CLI</span></h1>
<p data-tool="mdnice编辑器">Gemini CLI 选择了一条中庸之道，或者说是实用之道。Gemini CLI 项目开源了，这部分的说明会多一些。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.1 70/30？</span></h2>
<p data-tool="mdnice编辑器">Gemini CLI 选择了 70% 作为压缩触发点，30% 作为保留比例。这个比例我们也可以参考学习一下：</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">为什么是 70% 而不是 92%</strong></p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;">更早介入，避免紧急压缩导致的卡顿</section>
</li>
<li>
<section style="color: #010101;">给压缩过程留出充足的缓冲空间</section>
</li>
<li>
<section style="color: #010101;">适合轻量级应用场景，不追求极限性能</section>
</li>
</ul>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">30% 保留的合理性：</strong></p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;">刚好覆盖最近 5-10 轮对话</section>
</li>
<li>
<section style="color: #010101;">足够维持上下文连续性</section>
</li>
<li>
<section style="color: #010101;">不会让用户感觉&#8221;突然失忆&#8221;</section>
</li>
</ul>
<p data-tool="mdnice编辑器">共背后的逻辑是：宁可频繁一点地压缩，也要保证每次压缩都是从容的、高质量的。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.2 精选历史提取</span></h2>
<p data-tool="mdnice编辑器">Gemini CLI 有个独特的概念叫精选历史&#8221;。不是所有的历史都值得保留，系统会智能地筛选有效内容：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-function"><span class="hljs-keyword" style="color: #c678dd;">function</span> <span class="hljs-title" style="color: #61aeee;">extractCuratedHistory</span>(<span class="hljs-params">comprehensiveHistory: Content[]</span>): <span class="hljs-title" style="color: #61aeee;">Content</span>[] </span>{
  <span class="hljs-keyword" style="color: #c678dd;">if</span> (comprehensiveHistory === <span class="hljs-literal" style="color: #56b6c2;">undefined</span> || comprehensiveHistory.length === <span class="hljs-number" style="color: #d19a66;">0</span>) {
    <span class="hljs-keyword" style="color: #c678dd;">return</span> [];
  }
  <span class="hljs-keyword" style="color: #c678dd;">const</span> curatedHistory: Content[] = [];
  <span class="hljs-keyword" style="color: #c678dd;">const</span> length = comprehensiveHistory.length;
  <span class="hljs-keyword" style="color: #c678dd;">let</span> i = <span class="hljs-number" style="color: #d19a66;">0</span>;
  <span class="hljs-keyword" style="color: #c678dd;">while</span> (i &lt; length) {
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 用户轮次直接保留</span>
    <span class="hljs-keyword" style="color: #c678dd;">if</span> (comprehensiveHistory[i].role === <span class="hljs-string" style="color: #98c379;">'user'</span>) {
      curatedHistory.push(comprehensiveHistory[i]);
      i++;
    } <span class="hljs-keyword" style="color: #c678dd;">else</span> {
      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 处理模型轮次</span>
      <span class="hljs-keyword" style="color: #c678dd;">const</span> modelOutput: Content[] = [];
      <span class="hljs-keyword" style="color: #c678dd;">let</span> isValid = <span class="hljs-literal" style="color: #56b6c2;">true</span>;
      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 收集连续的模型轮次</span>
      <span class="hljs-keyword" style="color: #c678dd;">while</span> (i &lt; length &amp;&amp; comprehensiveHistory[i].role === <span class="hljs-string" style="color: #98c379;">'model'</span>) {
        modelOutput.push(comprehensiveHistory[i]);
        <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 检查内容有效性</span>
        <span class="hljs-keyword" style="color: #c678dd;">if</span> (isValid &amp;&amp; !isValidContent(comprehensiveHistory[i])) {
          isValid = <span class="hljs-literal" style="color: #56b6c2;">false</span>;
        }
        i++;
      }
      <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 只有当所有模型轮次都有效时才保留</span>
      <span class="hljs-keyword" style="color: #c678dd;">if</span> (isValid) {
        curatedHistory.push(...modelOutput);
      }
    }
  }
  <span class="hljs-keyword" style="color: #c678dd;">return</span> curatedHistory;
}
</code></pre>
<p data-tool="mdnice编辑器">这个策略的巧妙之处在于：</p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">用户输入全部保留</strong>：所有用户输入都被视为重要信息，无条件保留</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">模型轮次有条件保留</strong>：连续的模型轮次被视为一个整体进行评估</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">全有或全无的处理</strong>：要么全部保留，要么全部丢弃，避免了复杂的部分保留逻辑</section>
</li>
</ul>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.3 内容有效性判断</span></h2>
<p data-tool="mdnice编辑器">什么样的内容会被认为是无效的？Gemini CLI 有明确的标准：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-function"><span class="hljs-keyword" style="color: #c678dd;">function</span> <span class="hljs-title" style="color: #61aeee;">isValidContent</span>(<span class="hljs-params">content: Content</span>): <span class="hljs-title" style="color: #61aeee;">boolean</span> </span>{
  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 检查 parts 数组是否存在且非空</span>
  <span class="hljs-keyword" style="color: #c678dd;">if</span> (content.parts === <span class="hljs-literal" style="color: #56b6c2;">undefined</span> || content.parts.length === <span class="hljs-number" style="color: #d19a66;">0</span>) {
    <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">false</span>;
  }
  <span class="hljs-keyword" style="color: #c678dd;">for</span> (<span class="hljs-keyword" style="color: #c678dd;">const</span> part of content.parts) {
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 检查 part 是否为空</span>
    <span class="hljs-keyword" style="color: #c678dd;">if</span> (part === <span class="hljs-literal" style="color: #56b6c2;">undefined</span> || <span class="hljs-built_in" style="color: #e6c07b;">Object</span>.keys(part).length === <span class="hljs-number" style="color: #d19a66;">0</span>) {
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">false</span>;
    }
    <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 检查非思考类型的 part 是否有空文本</span>
    <span class="hljs-keyword" style="color: #c678dd;">if</span> (!part.thought &amp;&amp; part.text !== <span class="hljs-literal" style="color: #56b6c2;">undefined</span> &amp;&amp; part.text === <span class="hljs-string" style="color: #98c379;">''</span>) {
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">false</span>;
    }
  }
  <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">true</span>;
}
</code></pre>
<p data-tool="mdnice编辑器">无效内容包括：空响应、错误输出、中断的流式响应等。这种预过滤机制确保进入压缩流程的都是高质量的内容。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.4 五段式结构化摘要</span></h2>
<p data-tool="mdnice编辑器">相比 Claude Code 的八段式，Gemini CLI 的五段式更简洁，但涵盖了所有关键信息：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;">1. overall_goal - 用户的主要目标
2. key_knowledge - 重要技术知识和决策
3. file_system_state - 文件系统当前状态
4. recent_actions - 最近执行的重要操作
5. current_plan - 当前执行计划
</code></pre>
<p data-tool="mdnice编辑器">压缩时，系统会生成 XML 格式的结构化摘要。这种格式的好处是结构清晰，LLM 容易理解和生成，同时也便于后续的解析和处理。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.5 基于 Token 的智能压缩</span></h2>
<p data-tool="mdnice编辑器">Gemini CLI 的压缩不是简单的定时触发，而是基于精确的 token 计算：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-keyword" style="color: #c678dd;">async</span> tryCompressChat(
  prompt_id: <span class="hljs-built_in" style="color: #e6c07b;">string</span>,
  force: <span class="hljs-built_in" style="color: #e6c07b;">boolean</span> = <span class="hljs-literal" style="color: #56b6c2;">false</span>,
): <span class="hljs-built_in" style="color: #e6c07b;">Promise</span>&lt;ChatCompressionInfo | <span class="hljs-literal" style="color: #56b6c2;">null</span>&gt; {
  <span class="hljs-keyword" style="color: #c678dd;">const</span> curatedHistory = <span class="hljs-keyword" style="color: #c678dd;">this</span>.getChat().getHistory(<span class="hljs-literal" style="color: #56b6c2;">true</span>);

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 空历史不压缩</span>
  <span class="hljs-keyword" style="color: #c678dd;">if</span> (curatedHistory.length === <span class="hljs-number" style="color: #d19a66;">0</span>) {
    <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">null</span>;
  }

  <span class="hljs-keyword" style="color: #c678dd;">const</span> model = <span class="hljs-keyword" style="color: #c678dd;">this</span>.config.getModel();

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 计算当前历史的 token 数量</span>
  <span class="hljs-keyword" style="color: #c678dd;">const</span> { totalTokens: originalTokenCount } =
    <span class="hljs-keyword" style="color: #c678dd;">await</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.getContentGenerator().countTokens({
      model,
      contents: curatedHistory,
    });

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 获取压缩阈值配置</span>
  <span class="hljs-keyword" style="color: #c678dd;">const</span> contextPercentageThreshold =
    <span class="hljs-keyword" style="color: #c678dd;">this</span>.config.getChatCompression()?.contextPercentageThreshold;

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 如果未强制压缩且 token 数量低于阈值，则不压缩</span>
  <span class="hljs-keyword" style="color: #c678dd;">if</span> (!force) {
    <span class="hljs-keyword" style="color: #c678dd;">const</span> threshold =
      contextPercentageThreshold ?? COMPRESSION_TOKEN_THRESHOLD; <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 默认 0.7</span>
    <span class="hljs-keyword" style="color: #c678dd;">if</span> (originalTokenCount &lt; threshold * tokenLimit(model)) {
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-literal" style="color: #56b6c2;">null</span>;
    }
  }

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 计算压缩点，保留最后 30% 的历史</span>
  <span class="hljs-keyword" style="color: #c678dd;">let</span> compressBeforeIndex = findIndexAfterFraction(
    curatedHistory,
    <span class="hljs-number" style="color: #d19a66;">1</span> - COMPRESSION_PRESERVE_THRESHOLD, <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// COMPRESSION_PRESERVE_THRESHOLD = 0.3</span>
  );

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 确保压缩点在用户轮次开始处</span>
  <span class="hljs-keyword" style="color: #c678dd;">while</span> (
    compressBeforeIndex &lt; curatedHistory.length &amp;&amp;
    (curatedHistory[compressBeforeIndex]?.role === <span class="hljs-string" style="color: #98c379;">'model'</span> ||
      isFunctionResponse(curatedHistory[compressBeforeIndex]))
  ) {
    compressBeforeIndex++;
  }

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 分割历史为需要压缩和需要保留的部分</span>
  <span class="hljs-keyword" style="color: #c678dd;">const</span> historyToCompress = curatedHistory.slice(<span class="hljs-number" style="color: #d19a66;">0</span>, compressBeforeIndex);
  <span class="hljs-keyword" style="color: #c678dd;">const</span> historyToKeep = curatedHistory.slice(compressBeforeIndex);

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 使用 LLM 生成历史摘要</span>
  <span class="hljs-keyword" style="color: #c678dd;">this</span>.getChat().setHistory(historyToCompress);
  <span class="hljs-keyword" style="color: #c678dd;">const</span> { text: summary } = <span class="hljs-keyword" style="color: #c678dd;">await</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.getChat().sendMessage(
    {
      message: {
        text: <span class="hljs-string" style="color: #98c379;">'First, reason in your scratchpad. Then, generate the &lt;state_snapshot&gt;.'</span>,
      },
      config: {
        systemInstruction: { text: getCompressionPrompt() },
      },
    },
    prompt_id,
  );

  <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 创建新的聊天历史，包含摘要和保留的部分</span>
  <span class="hljs-keyword" style="color: #c678dd;">this</span>.chat = <span class="hljs-keyword" style="color: #c678dd;">await</span> <span class="hljs-keyword" style="color: #c678dd;">this</span>.startChat([
    {
      role: <span class="hljs-string" style="color: #98c379;">'user'</span>,
      parts: [{ text: summary }],
    },
    {
      role: <span class="hljs-string" style="color: #98c379;">'model'</span>,
      parts: [{ text: <span class="hljs-string" style="color: #98c379;">'Got it. Thanks for the additional context!'</span> }],
    },
    ...historyToKeep,
  ]);
}
</code></pre>
<p data-tool="mdnice编辑器">这个实现有几个细节值得注意：</p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">支持强制压缩</strong>：通过 force 参数，用户可以主动触发压缩</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">智能分割点选择</strong>：确保压缩点在用户轮次开始，避免打断对话逻辑</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">两阶段压缩</strong>：先生成摘要，再重建对话历史</section>
</li>
</ul>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.6 多层压缩机制</span></h2>
<p data-tool="mdnice编辑器">Gemini CLI 的压缩是分层进行的，每一层都有特定的目标：</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">第一层：内容过滤</strong>：过滤掉无效内容、thought 类型的部分，确保进入下一层的都是有价值的信息。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">第二层：内容整合</strong>：合并相邻的同类内容，比如连续的纯文本 Part 会被合并成一个，减少结构冗余。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">第三层：智能摘要</strong>：当 token 使用量超过阈值时，触发 LLM 生成结构化摘要。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">第四层：保护机制</strong>：确保关键信息不被压缩丢失，比如用户的最新指令、正在进行的任务等。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.7 模型适配的 Token 限制</span></h2>
<p data-tool="mdnice编辑器">不同的模型有不同的 token 限制，Gemini CLI 对此有精细的适配：</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="color: #abb2bf;"><span class="hljs-keyword" style="color: #c678dd;">export</span> <span class="hljs-function"><span class="hljs-keyword" style="color: #c678dd;">function</span> <span class="hljs-title" style="color: #61aeee;">tokenLimit</span>(<span class="hljs-params">model: Model</span>): <span class="hljs-title" style="color: #61aeee;">TokenCount</span> </span>{
  <span class="hljs-keyword" style="color: #c678dd;">switch</span> (model) {
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-1.5-pro'</span>:
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-number" style="color: #d19a66;">2</span>_097_152;
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-1.5-flash'</span>:
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-2.5-pro'</span>:
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-2.5-flash'</span>:
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-2.0-flash'</span>:
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-number" style="color: #d19a66;">1</span>_048_576;
    <span class="hljs-keyword" style="color: #c678dd;">case</span> <span class="hljs-string" style="color: #98c379;">'gemini-2.0-flash-preview-image-generation'</span>:
      <span class="hljs-keyword" style="color: #c678dd;">return</span> <span class="hljs-number" style="color: #d19a66;">32</span>_000;
    <span class="hljs-keyword" style="color: #c678dd;">default</span>:
      <span class="hljs-keyword" style="color: #c678dd;">return</span> DEFAULT_TOKEN_LIMIT; <span class="hljs-comment" style="font-style: italic; color: #5c6370;">// 1_048_576</span>
  }
}
</code></pre>
<p data-tool="mdnice编辑器">系统会根据使用的模型自动调整压缩策略。对于支持超长上下文的模型（如 gemini-1.5-pro 的 200 万 token），可以更宽松；对于受限的模型，会更积极地压缩。</p>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.8 历史记录的精细处理</span></h2>
<p data-tool="mdnice编辑器"><code style="color: #0e8aeb;">recordHistory</code> 方法负责记录和处理历史，实施了多个优化策略：</p>
<ol data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">避免重复</strong>：不会重复添加相同的用户输入</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">过滤思考过程</strong>：thought 类型的 Part 会被过滤掉，不进入最终历史</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">合并优化</strong>：相邻的模型轮次会被合并，相邻的纯文本也会合并</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">占位符策略</strong>：如果模型没有有效输出，会添加空的占位符保持结构完整</section>
</li>
</ol>
<h2 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">3.9 压缩的用户体验设计</span></h2>
<p data-tool="mdnice编辑器">Gemini CLI 特别注重压缩对用户体验的影响：</p>
<ul data-tool="mdnice编辑器">
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">无感压缩</strong>：70% 的阈值确保压缩发生在用户察觉之前</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">连续性保持</strong>：保留 30% 的最新历史，确保当前话题的连贯性</section>
</li>
<li>
<section style="color: #010101;"><strong style="color: #0e88eb;">透明反馈</strong>：压缩前后的 token 数量变化会被记录和报告</section>
</li>
</ul>
<h1 data-tool="mdnice编辑器"><span class="content" style="color: #0e8aeb;">4. 写在最后</span></h1>
<p data-tool="mdnice编辑器">研究完这三个项目的源码，我最大的感受是：<strong style="color: #0e88eb;">压缩策略的选择，本质上是对「什么是重要的」这个问题的回答。</strong> Manus 说「所有信息都可能重要，所以我不删除，只是暂时收起来」；Claude Code 说「结构化的摘要比原始细节更重要」；Gemini CLI 说「用户体验比技术指标更重要」。三种回答，三种哲学。</p>
<p data-tool="mdnice编辑器">这让我想起一句话：<strong style="color: #0e88eb;">在 AI 时代，真正稀缺的不是信息，而是注意力。</strong> 上下文压缩就是在教 AI 如何分配注意力——什么该记住，什么可以忘记，什么需要随时能找回来。</p>
<p data-tool="mdnice编辑器">这是人类智慧的核心能力之一。我们每天都在做类似的决策：重要的事情记在心里，次要的写在本子上，琐碎的存在手机里。Manus、Claude Code 和 Gemini CLI 只是用不同的方式在教 AI 做同样的事。</p>
<p data-tool="mdnice编辑器"><strong style="color: #0e88eb;">没有完美的压缩策略，只有最适合你场景的策略。</strong> 选择哪种策略不重要，重要的是理解它们背后的设计智慧，然后根据自己的需求做出明智的选择。</p>
<p data-tool="mdnice编辑器">以上。</p>
</section>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2025/09/agent-core-strategy-context-compression-strategy-and-details-for-manus-gemini-cli-and-claude-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
