详情

首页手游攻略 第 9 篇:上下文管理实战 —— 五种压缩策略与 Context Rot 对抗

第 9 篇:上下文管理实战 —— 五种压缩策略与 Context Rot 对抗

佚名 2026-08-03 10:08:55

第 9 篇:上下文管理实战 —— 五种压缩策略与 Context Rot 对抗

{"type":"doc","content":[{"type":"paragraph","attrs":{"id":"08c681cd-575c-4668-b030-771d8ba8fd42","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"blockquote","attrs":{"id":"ab8d71e0-d4a7-4bd3-97ca-650da5f46a23","textAlign":"inherit","isHoverDragHandle":false},"content":[{"type":"paragraph","attrs":{"id":"7e161a3c-83ca-4376-8790-cb18c062645b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"系列定位"},{"type":"text","text":":面向有 3 年经验的后端开发者,从零构建 Agent Harness 工程能力体系 "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"上一篇回顾"},{"type":"text","text":":[第 8 篇]我们拆解了单 Agent vs 多 Agent 的拆分决策与编排模式,解决了"何时拆、怎么编排"的问题 "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"本篇核心问题"},{"type":"text","text":":上下文窗口是稀缺资源(即使 200K token 也会耗尽),如何让 Agent 在有限窗口内保持高效?"}]}]},{"type":"horizontalRule","attrs":{"id":"63eff922-49a0-4603-bf16-cf5e09839489","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"ea4f9493-ed8a-4907-a5db-ba4a586e6b62","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"一、场景切入:被"遗忘"的关键决策"}]},{"type":"paragraph","attrs":{"id":"cac19372-f638-4bc8-8257-f71d0ce19a45","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"你正在用 Agent 执行一个复杂的微服务重构任务。任务进行到第 50 轮时,发生了一件令人困惑的事——"}]},{"type":"paragraph","attrs":{"id":"ecfe8043-745c-4a54-a7f6-02145feda03f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Agent 在第 3 轮就确认了"所有服务间通信必须使用 gRPC 而非 REST"这一架构决策。但到了第 50 轮,它突然开始用 REST 风格编写新接口。你翻看上下文,发现那条关键决策被"淹没"在第 5 轮和第 10 轮之间大量的工具输出、代码片段和中间推理之中。Agent 并非"故意忽略"它——而是上下文已经膨胀到 18 万 token,那条决策恰好落在了注意力最薄弱的位置。"}]},{"type":"paragraph","attrs":{"id":"0d3d9569-0324-4185-b2ee-ee0b0f0b052d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"这就是 "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"Context Rot(上下文腐烂)"},{"type":"text","text":":随着对话变长,关键信息在上下文中的位置逐渐"腐烂",模型的注意力无法有效覆盖,导致性能持续衰减。"}]},{"type":"paragraph","attrs":{"id":"8eb261e8-437a-4e10-b99b-2c8cf6905433","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"这不是个例。Stanford/UC Berkeley 的研究系统性地证实了这一现象,而 Claude Code 团队在生产环境中用五级压缩策略与之对抗。本文将从问题本质出发,逐步拆解五种压缩策略、内容存活规则、生产级策略矩阵,最后用代码实现一个可用的上下文管理器。"}]},{"type":"horizontalRule","attrs":{"id":"75d44939-1f94-4c9d-9807-4b21001a0727","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"7e4d35e6-2679-40af-8f42-ecf8c0474d7f","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"二、Context Rot:上下文为什么会"腐烂""}]},{"type":"heading","attrs":{"id":"9b2d238c-0537-47b0-9512-d4ede986b832","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"2.1 "Lost in the Middle"——注意力机制的 U 型曲线"}]},{"type":"paragraph","attrs":{"id":"e84f3b2f-67c3-49db-87e3-a018a8b571f6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"2023 年,Nelson F. Liu 等人(Stanford、UC Berkeley、Samaya AI)发表了论文 "},{"type":"text","marks":[{"type":"italic"}],"text":"Lost in the Middle: How Language Models Use Long Contexts"},{"type":"text","text":",揭示了一个反直觉的现象:"}]},{"type":"blockquote","attrs":{"id":"b25ff02e-a98c-41e4-81e8-46d9c52f181a","textAlign":"inherit","isHoverDragHandle":false},"content":[{"type":"paragraph","attrs":{"id":"4070f50b-0281-4e27-b09c-7ee8fafa2da6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"模型对上下文开头和结尾的信息敏感度最高,对中间位置的信息注意力显著衰减——即使这些模型是专门为长上下文设计的。"}]}]},{"type":"paragraph","attrs":{"id":"f5c45de1-0ca5-47b3-b98d-e5256860e66c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"实验表明,当关键信息被放在长上下文的中间位置时,任务失败率可提高 30% 以上。性能曲线呈现明显的 "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"U 型"},{"type":"text","text":":两头高、中间低。"}]},{"type":"image","attrs":{"id":"9ecbcba4-5b34-4d44-8108-ea898267a711","src":"https://developer.qcloudimg.com/http-save/audit-12040675/f64d04071f58975a2c7a6d3bb240555e.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":810,"aspectRatio":"0.880435","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"paragraph","attrs":{"id":"08913ec1-55ff-478d-a286-ba51259dcd92","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":这就像缓存置换算法中的"最不经常使用"(LFU)——但这里不是你主动选择淘汰哪些数据,而是模型的注意力机制天然倾向于"忽略中间"。你无法控制 LLM 的注意力分布,但你可以控制信息在上下文中的"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"位置和存活时间"},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"id":"e7567a50-ff84-43b4-960b-47ce84589ced","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"2.2 LLM Summarization 的隐藏代价"}]},{"type":"paragraph","attrs":{"id":"733177be-0719-4850-9fca-6ac81d616b0b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"面对上下文膨胀,最直觉的方案是"让 LLM 自己做摘要"。但这条路有两个隐藏问题:"}]},{"type":"paragraph","attrs":{"id":"47816156-79e0-44a0-8db3-3052079425c5","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"问题一:掩盖自然停止信号。"},{"type":"text","text":" 当 LLM 对历史做 summarization 时,它会将冗长的工具输出压缩成简洁的叙述。但 Agent 的"停止信号"——即"任务已完成,可以输出最终答案"——往往隐藏在对话的细节中。Summarization 抹平了这些细节,使 Agent 丧失了判断"是否该停"的依据,导致推理轨迹不必要地延长 13-15%。不是更准确了,只是更"懒"了——在错误的方向上走得更远。"}]},{"type":"paragraph","attrs":{"id":"8ad9124d-23c0-4725-8ca8-bdc56c309de0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"问题二:摘要本身的失真。"},{"type":"text","text":" LLM 做 summary 时会"创造性"地重组信息,可能丢失关键因果关系。比如 Agent 在第 10 轮发现了一个 bug 的根因,但在 summary 中被压缩为"发现并修复了一些问题"——根因信息丢失了,后续遇到类似问题时无法参考。"}]},{"type":"heading","attrs":{"id":"a26c4af5-8d89-490c-8b49-27e44855e160","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"2.3 JetBrains 的反直觉发现:简单即是美"}]},{"type":"paragraph","attrs":{"id":"5611e28c-be19-4da2-92fa-144b4738f34e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"2025 年,JetBrains Research 在 NeurIPS DL4C Workshop 上发表了论文 "},{"type":"text","marks":[{"type":"italic"}],"text":"The Complexity Trap: Simple Observation Masking Is as Efficient as LLM Summarization for Agent Context Management"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"id":"ef705143-9933-4ae7-ae73-b7a3b72ad444","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"他们对比了两种上下文管理方案:"}]},{"type":"table","attrs":{"id":"f469efe6-e790-4d9b-bef8-f9773c7983be","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"bb896b34-808a-4950-a5e4-5c0f58358ea0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"方案"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"042c1f59-c7ea-4733-af8b-88598fe80cc9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"机制"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"3f7520ce-3d07-4e85-a305-799795419929","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"成本"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"b5b22e39-6861-4233-8b26-3a33c4e12788","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"效果"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"afc58a54-a306-4ba8-bf2d-b4709f762e3a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Observation Masking"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"01406c1b-d3f9-428f-8b36-3f7dfc114656","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"滑动窗口,直接清除旧的观察结果(工具输出)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"d775734c-9cd3-4db3-93cc-88b144cbe024","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"极低(无额外 API 调用)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"ad692b32-92e5-4688-a573-0557d159c5ac","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"与 LLM summarization 持平甚至更优"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"8b1470fd-51c1-40dc-8b4b-8cdd5fc16f8a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"LLM Summarization"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"5a3aab37-f93f-4b98-8a40-51ada8a05c39","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"用 LLM 对历史对话生成摘要"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"c1968f4e-af72-4977-89c4-4fd4fc345c08","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(每次压缩需一次 API 调用)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"8da53d78-a332-4f77-bf1a-3912d0ef045b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"效果好,但成本高 2 倍以上"}]}]}]}]},{"type":"paragraph","attrs":{"id":"067fffb2-50e4-414c-b027-097b983e5159","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"核心发现:"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"简单的 observation masking(直接清除旧工具输出)在 SWE-agent 基准测试上匹配甚至超越了 LLM summarization"},{"type":"text","text":",同时将成本降低了一半以上。"}]},{"type":"paragraph","attrs":{"id":"9085b487-5823-42e3-b0d5-8454b0aec73b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"这挑战了"越复杂越好"的直觉——有时候,最简单的方案也是最有效的。"}]},{"type":"paragraph","attrs":{"id":"ab94fe43-534d-48ac-b6aa-0976a1264665","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":这就像 JVM GC 策略的选择。你以为用了 G1 GC(复杂、精密)就一定比 Serial GC(简单、粗暴)好?在特定场景下,简单的策略反而更快、更稳定。上下文管理也是同理。"}]},{"type":"horizontalRule","attrs":{"id":"951c058d-9a50-409d-8bb1-ebd5c3c194dc","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"e6b82c4c-ae05-4055-ac17-9732e633b665","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"三、Claude Code 五种 Compaction 策略"}]},{"type":"paragraph","attrs":{"id":"4071210c-93cb-41ee-8830-90c544481437","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Claude Code 是目前上下文管理最成熟的工程实践之一。它没有押注单一策略,而是构建了一套"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"从廉价到昂贵的成本阶梯"},{"type":"text","text":"——总是先尝试最便宜的方案,迫不得已才用最贵的。"}]},{"type":"image","attrs":{"id":"aa8a9728-0f69-4867-93bb-7d5f6de1fb62","src":"https://developer.qcloudimg.com/http-save/audit-12040675/a9dcfda6e2128316c69b4bff02fb9f95.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":794,"aspectRatio":"0.582966","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"b6abdbb4-7db5-4a69-bd0e-177804296ff4","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"策略一:Snip —— 第一道防线"}]},{"type":"paragraph","attrs":{"id":"348ce15b-c22f-44f7-9a04-955b01ce517d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"机制"},{"type":"text","text":":直接删除超过窗口的旧消息段,不做任何摘要。"}]},{"type":"paragraph","attrs":{"id":"4eec2406-c71b-4cae-947b-dd4c3c1f5f9a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"定位"},{"type":"text","text":":最轻量的修剪操作。当上下文略超阈值时,Snip 会移除最旧的历史消息段,释放 token 空间。"}]},{"type":"paragraph","attrs":{"id":"3711e404-1569-4cc8-bc1a-7956b7792b0b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"特点"},{"type":"text","text":":零 API 调用、零信息保留——删了就是删了。但速度快,适合处理那些"反正也不会再看"的早期消息。"}]},{"type":"paragraph","attrs":{"id":"3fb63b01-b2b1-488b-b866-8d3589a2aca0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":Snip ≈ Eden 区的 Minor GC——快速清理年轻代中确定不再使用的对象,代价低。"}]},{"type":"heading","attrs":{"id":"462ed783-5001-461f-a09c-d1d195b009ef","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"策略二:Microcompact —— 懒加载式压缩"}]},{"type":"paragraph","attrs":{"id":"2b710ddf-9ab9-4082-bc72-d41249a55565","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"机制"},{"type":"text","text":":将旧的工具输出(文件内容、shell 输出、grep 结果等)存到磁盘,上下文中只保留一个摘要占位符(如 "},{"type":"text","marks":[{"type":"code"}],"text":"[Old tool result content cleared]"},{"type":"text","text":")。模型需要时可以重新读取原始内容。"}]},{"type":"paragraph","attrs":{"id":"ddb51a63-0519-4491-b3e0-a4688b373f3e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"定位"},{"type":"text","text":":这是最精妙的策略。它不是"删除信息",而是"信息降级"——从昂贵的上下文窗口降级到廉价的磁盘存储。"}]},{"type":"paragraph","attrs":{"id":"300e9497-e720-4819-9971-0b54ab80d921","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"实现细节"},{"type":"text","text":":"}]},{"type":"bulletList","attrs":{"id":"8d5f9f9d-84c2-437a-a282-7f6012d36b4c","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"fd1e2b2a-a527-4784-ac5c-6bfee6243805"},"content":[{"type":"paragraph","attrs":{"id":"1209098b-4422-4a34-a9cc-73c965c7ad33","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"只有特定工具的结果可以被 microcompact(Read、Bash、Grep、Glob、WebSearch 等)"}]}]},{"type":"listItem","attrs":{"id":"1b7cca7c-ee75-4e16-ba86-5e0b6120c5ee"},"content":[{"type":"paragraph","attrs":{"id":"e5b9c16a-9e11-4cbb-b8fd-c11ee56bb8aa","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"自定义 MCP 工具的结果不会被清除(因为无法预知其重要性)"}]}]},{"type":"listItem","attrs":{"id":"39474b43-3db1-4c94-a4b0-ba1db28dfe49"},"content":[{"type":"paragraph","attrs":{"id":"0c1ddb59-ce7e-4c4b-9305-b78bc8da5b07","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"保留最近 5 条工具结果的完整内容"}]}]},{"type":"listItem","attrs":{"id":"6774b9ec-634d-45ae-b169-4c5fff832d70"},"content":[{"type":"paragraph","attrs":{"id":"ab2217d1-9c21-45d2-b45b-b979f364b346","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"时间触发:如果距上次 assistant 消息超过 60 分钟(服务端缓存 TTL),触发 microcompact"}]}]}]},{"type":"paragraph","attrs":{"id":"f433c924-ddf3-465f-b851-5c600bac5f99","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":Microcompact ≈ 操作系统虚拟内存——内存不够时,把不活跃的页面换到磁盘(swap),需要时再换回来。上下文窗口是 RAM,磁盘存储是 swap space。"}]},{"type":"heading","attrs":{"id":"c87c1eea-a2c0-4a9b-8650-da23a5383d9a","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"策略三:Context Collapse —— 渐进折叠"}]},{"type":"paragraph","attrs":{"id":"e8293d4e-d21a-457e-b963-3d8abe4e909b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"机制"},{"type":"text","text":":对旧的对话段落做渐进式压缩,保留关键决策点,丢弃冗余的中间过程。"}]},{"type":"paragraph","attrs":{"id":"ba7b6f56-b854-4e68-a25c-00ae6bfda96e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"定位"},{"type":"text","text":":介于 Microcompact 和 Autocompact 之间的中间策略。比 Microcompact 保留更多信息(不只是工具输出,还包括对话段落),比 Autocompact 更轻量(不做全对话 summarization)。"}]},{"type":"paragraph","attrs":{"id":"15c2053e-44d6-49bd-adb9-c740d899b05c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"Claude Code 的实际实现"},{"type":"text","text":"(Session Memory Compact 变体):使用后台持续更新的 session memory 文件替代旧消息。不需要额外的 LLM 调用,零 API 成本。"}]},{"type":"paragraph","attrs":{"id":"80cc1cc9-55c1-4ab1-b572-a80fba4a1953","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":Context Collapse ≈ 日志轮转(log rotation)——保留最近的完整日志,将旧日志压缩归档,关键日志节点保留索引。"}]},{"type":"heading","attrs":{"id":"153ca39d-0ad5-4ca9-b2b7-43757616808e","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"策略四:Autocompact —— 全对话 Summarization"}]},{"type":"paragraph","attrs":{"id":"fd221c0d-5c20-4341-8b07-a6d710159ba8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"机制"},{"type":"text","text":":当 token 使用量达到有效窗口的约 95% 时(具体为距有效窗口上限仅剩 13K token),自动 fork 一个子 Agent 对整个对话生成结构化摘要。"}]},{"type":"paragraph","attrs":{"id":"08e807e7-0d04-4a94-8bf7-83ae778230a3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"这是 Claude Code 上下文管理的核心策略。"},{"type":"text","text":" 它的精妙之处在于摘要的结构化设计——不是简单的"把对话缩短",而是生成一个 "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"9 段式结构化摘要"},{"type":"text","text":":"}]},{"type":"table","attrs":{"id":"82fb3f7f-6797-4910-8d55-5a5bf21ace73","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"084ee7e8-2495-42ed-8102-a5479992e99b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"段落"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"8e258369-7503-46ca-965e-c4327ca5c76d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"内容"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"90cfcfcd-8c5c-4f1d-af38-5473d672bb63","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"为什么重要"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"10083cd0-acba-4dbc-92b8-d1e22ddf2ef6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"1. Primary Request"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"42832277-06e9-4955-a91f-b2cc658db893","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"用户的所有明确请求和意图"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"8a4496c8-627b-44e5-89ca-faa08334f500","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"防止任务目标漂移"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"9ba3518c-bbef-444b-91b1-1a7e52071630","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"2. Key Technical Concepts"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"4e3984fb-adf8-4744-b809-ab0ba858595a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"涉及的技术、框架、设计模式"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"4250bfc1-69da-4dfc-a6ad-496a8fb19e1c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"保持技术上下文连续性"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"8d10a408-a644-4027-b548-26c79f299c47","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"3. Files and Code Sections"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"87b294b0-18aa-4bb2-98aa-98cf772a9c2a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"检查/修改/创建的文件及代码片段"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"7398450c-50db-425e-899e-d6ca6258b296","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"代码变更可追溯"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"e3e3935b-630a-4fca-a7b8-4059bf39ac4e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"4. Errors and Fixes"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"09d70de6-59c6-44bc-8c6c-e9b87eb82761","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"遇到的每个错误及解决方式"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"6af0648f-aab2-44ae-99c2-3ad225047668","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"避免重复踩坑"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"bb056a5f-fc7d-42f5-a08d-b1c651e7cf0d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"5. Problem Solving"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"ebe9ec4c-e096-4925-a586-de9f4930f071","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"已解决和正在解决的问题"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"69d0a2bd-76ad-4c57-a15b-4c4586c36d9f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"推理链不中断"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"a9d6f709-78c4-4fa3-aebc-1d96e9d526a8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"6. All User Messages"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"bcd329d6-3d97-4f55-8d65-1ce715af767c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"逐条列出所有非工具结果的用户消息"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"e433dad1-6509-4d24-8570-f3871d91bb69","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"唯一无法从工具调用推断的信息"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"23fe2841-3f1b-4ff7-aec7-5e5727f34ad3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"7. Pending Tasks"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"ee1e426e-8091-4d8b-a842-6ecc69b44410","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"已分配但未完成的任务"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"3db0050c-b40a-4840-998a-289a69d8c058","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"防止遗漏"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"f2be2094-98af-483f-895c-ba1c84262370","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"8. Current Work"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"31f8123d-bde6-480d-846b-6ebbdbbaebc4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"压缩前正在做什么"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"fd55174c-9207-4852-adb3-b71152ba0d16","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"实现无缝续接"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"0b56b2d8-7567-4a4d-afee-919ba86ed877","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"9. Optional Next Step"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"3e091d9b-3446-4b3d-a3f5-8e6d589eabb7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"直接对齐最近用户请求的下一步"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"f8668b1a-06ea-4b1e-8b88-0417d6d65390","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"恢复执行方向"}]}]}]}]},{"type":"paragraph","attrs":{"id":"d78c4400-a7e9-4293-9d73-2f8d6c52b387","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"为什么第 6 段(All User Messages)至关重要"},{"type":"text","text":":工具调用记录能反映 Agent 做了什么,但无法反映用户想要什么。如果没有逐条保留用户消息,模型会"幻觉"出一个看似连贯但与实际不符的任务叙述。"}]},{"type":"paragraph","attrs":{"id":"f5793a38-34ff-4f44-a32e-e0c376eae7d3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"熔断器机制"},{"type":"text","text":":如果 Autocompact 连续失败 3 次(网络超时、压缩请求本身过长等),熔断器触发,停止重试。代码注释中坦言:在加入熔断器之前,一个失败模式每天全球浪费约 25 万次 API 调用。"}]},{"type":"paragraph","attrs":{"id":"1f414fd1-d249-4baf-82cb-9b1f0f8eb697","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":Autocompact ≈ Full GC(Mark-Compact)——遍历整个堆,标记存活对象,压缩整理。代价高,但效果最彻底。9 段式摘要 ≈ GC 后的存活对象集,只保留"可达"的关键信息。"}]},{"type":"heading","attrs":{"id":"4caebd8a-7c54-4fa8-bcb3-51adcd0a9a2d","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"策略五:Reactive Compact —— 最后的逃生舱"}]},{"type":"paragraph","attrs":{"id":"29afadb3-9bc5-47de-a54a-eacb46eddcc1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"机制"},{"type":"text","text":":当 API 返回 413(prompt too long)错误时触发。从对话尾部开始逐轮剥离,直到请求能在窗口内放下。"}]},{"type":"paragraph","attrs":{"id":"18d49ba7-ae37-4137-abef-fd3126d68a0d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"定位"},{"type":"text","text":":这是"所有其他策略都失败后"的应急方案。最激进、信息损失最大,但保证 Agent 不会因上下文过长而完全卡死。"}]},{"type":"paragraph","attrs":{"id":"652ef75e-9a2e-4fbc-8181-6e0216541654","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":Reactive Compact ≈ OOM Killer——当系统内存耗尽时,强制杀死进程以保全系统运行。不优雅,但必要。"}]},{"type":"heading","attrs":{"id":"2375b821-cea4-4fd8-acfe-6da700a2e4fa","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"五种策略对比总结"}]},{"type":"table","attrs":{"id":"d95cd1a3-3e99-48ce-9dac-1a03b3c865c8","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"03f5d3cc-9dc8-46a8-9efa-658fc9883ec8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"策略"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"1ec0592c-7566-45b1-9ab2-34771686a520","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"触发条件"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"d29798aa-576f-4c47-91e5-a056fb6cacb6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"API 成本"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"64053d5b-0936-479d-9d64-dab983070f98","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"信息保留"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"741dc54f-b09c-4ec0-a789-d910b7e34c29","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"后端类比"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"11fc18ab-5b04-4d36-9f96-36388b828a11","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Snip"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9b52e8c8-626e-4ecb-ace9-92db6c843b9e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"略超阈值"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"a9e231f0-f939-4e6f-afe3-f375ea5b3877","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"零"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"96418484-1a2a-4e96-8c1e-cbf539e4b751","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"最低(直接删除)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"2f498f5b-cc49-4090-a978-749ed7307858","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Minor GC"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"be72be94-38d5-4426-b3b0-b9451e94dd5a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Microcompact"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"936e43c0-44e0-4591-abbe-5f828c8d2461","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"工具输出过大"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"f41823e1-029b-4dc2-abe3-95cbef91a189","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"零"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"fb3f4696-2073-44c9-b8ff-4abf48adf5d8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中(降级到磁盘)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"b167cd37-8d22-48f6-94a8-449153fc762e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"虚拟内存换页"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"a4ce2201-9970-48c6-92ed-ac8c129ff9f7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Context Collapse"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"a3ba16ea-7d79-42ff-93ff-06c008f1a7d1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"渐进压力"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"43a1dd14-9409-48df-ba63-2bb31bdf854f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"零(session memory)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"589f266e-9e3c-41ef-836c-7ab4c4251c32","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"较高(保留关键段)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"cb39bde1-36d0-4ba9-b411-dc46e741260d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"日志轮转"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"be49661d-347e-4a0b-b4a9-ef42b955058b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Autocompact"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"ecdf506a-8018-41de-bd95-68f7458ac6ec","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"~95% 容量"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"8603314d-ce34-4d99-b4c2-cd543e3ec46a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"一次 API 调用"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0e6a0e7c-d043-4156-a198-21e3a050edb2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"最高(9 段摘要)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9ee782af-3f55-43ef-a87f-4d1c3b68bd5a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Full GC"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"36c626a8-0d16-47f4-a8c3-f0a87e755d59","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Reactive Compact"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"423ceb28-5ef6-472a-bc0d-5a9c36bcf13b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"API 413"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"f8424d5d-3269-4412-a6c6-ecec9400cfc5","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"零(纯裁剪)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"418470b9-4bb7-445c-b10a-07a81a514ec1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"极低(尾部剥离)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"4f0b0cf0-ae86-4c1e-98fb-b3093a675c8b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"OOM Killer"}]}]}]}]},{"type":"horizontalRule","attrs":{"id":"81534616-db29-408b-8686-6097742ec7a8","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"9adba7ac-7bda-43b1-b2c1-334594d196d5","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"四、压缩后内容存活规则"}]},{"type":"paragraph","attrs":{"id":"1646432b-9342-44b2-9c7e-63c425d94dd9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"压缩不是"随机删除",而是有原则的取舍。以下是 Claude Code 生产环境验证的存活规则:"}]},{"type":"heading","attrs":{"id":"bbdfa36a-2e8c-4fe7-bdaf-f15115c39899","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"一定保留"}]},{"type":"bulletList","attrs":{"id":"57b2c038-34b9-4627-96d3-c71d48505b99","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"090d5b37-97cc-4b01-b95a-4bfabfba6bbc"},"content":[{"type":"paragraph","attrs":{"id":"7f7f7b61-7589-4615-b79c-46ac57cb80fb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"用户原始请求"},{"type":"text","text":":任务目标不可丢失。Autocompact 的第 1 段和第 6 段专门保留这些。"}]}]},{"type":"listItem","attrs":{"id":"2b95b12d-873f-4f44-8be9-b15a2322dde9"},"content":[{"type":"paragraph","attrs":{"id":"27934b8c-1e81-438e-bb10-ff2b16bac9f7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"关键代码变更"},{"type":"text","text":":哪些文件被修改了、修改了什么。第 3 段保留文件名和代码片段。"}]}]},{"type":"listItem","attrs":{"id":"4edce241-0abb-4c24-b865-46818bcb9b73"},"content":[{"type":"paragraph","attrs":{"id":"054b9adb-0f27-485a-802e-b8454ee90a30","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"架构决策"},{"type":"text","text":":技术选型、设计模式、约束条件。第 2 段保留这些。"}]}]},{"type":"listItem","attrs":{"id":"1c115720-bec0-49a2-b20d-6eda46a15202"},"content":[{"type":"paragraph","attrs":{"id":"10605906-ef82-47a3-bab2-ebf70b8fa37d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"当前工作状态"},{"type":"text","text":":压缩前正在做什么。第 8 段确保无缝续接。"}]}]}]},{"type":"heading","attrs":{"id":"860bf75f-4e5c-435d-8517-5f6b7085b4ad","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"可以丢失"}]},{"type":"bulletList","attrs":{"id":"cb435898-b798-40b9-8986-2b5abeeb6083","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"372df615-d595-4c84-a051-f41996885b11"},"content":[{"type":"paragraph","attrs":{"id":"e25331ae-0959-48d1-a636-c86877d37d12","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"详细的早期指令"},{"type":"text","text":":如果已持久化到 CLAUDE.md(项目宪法文件),每轮会话自动重新注入,不受压缩影响。"}]}]},{"type":"listItem","attrs":{"id":"a5f6da7f-4c67-4aeb-bd03-35d01c1f231d"},"content":[{"type":"paragraph","attrs":{"id":"d325c04f-03f6-4f8e-b89b-a33e92e1da55","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"中间工具输出的原始数据"},{"type":"text","text":":文件读取的完整内容、grep 的完整结果——这些可以降级到磁盘(Microcompact)或直接清除(Observation Masking)。"}]}]},{"type":"listItem","attrs":{"id":"a104d37e-6bbf-4892-92ac-6c5af3b020be"},"content":[{"type":"paragraph","attrs":{"id":"fa575eb9-7800-4a6a-85e5-2a4075b0c446","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"冗余的推理过程"},{"type":"text","text":":Agent 的中间思考链,如果结论已记录,过程可以丢弃。"}]}]}]},{"type":"heading","attrs":{"id":"38cf51fa-109e-4dc8-b859-375ed7a9fdd5","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"缓解策略:持久化到 CLAUDE.md"}]},{"type":"paragraph","attrs":{"id":"b2b07b62-f1c7-4c2d-b0fe-3a90e0fcb217","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"这是最关键的工程实践:"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"把不能丢的规则写进 CLAUDE.md,而不是依赖上下文记忆"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"id":"e9d7ed0c-a3d3-4d22-b0e1-c2195223b383","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"CLAUDE.md 在每轮会话开始时自动注入到 System Prompt 中,不受任何压缩策略影响。它相当于"每次 GC 后自动重建的根对象集"——无论上下文怎么压缩,这些规则永远存在。"}]},{"type":"paragraph","attrs":{"id":"48b97f46-d2ab-443a-8002-78aeeeddceed","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":CLAUDE.md ≈ 配置中心 环境变量。无论应用重启多少次,配置中心的规则总是会被重新加载。上下文压缩就像应用重启——CLAUDE.md 是"重启不变量"。"}]},{"type":"horizontalRule","attrs":{"id":"a3f22be8-f70c-4a74-813e-7a1e7262171d","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"e3943749-37db-4f2a-b6bf-e23586525942","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"五、生产级策略矩阵"}]},{"type":"paragraph","attrs":{"id":"89366b48-c9a4-4fa1-839b-11bf7d16bbc3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Claude Code 的五级策略是一套完整的体系,但生产环境中你不必照搬全部。以下是五种通用策略的选型矩阵:"}]},{"type":"image","attrs":{"id":"5b218a1e-420e-4e66-855c-059c6460d895","src":"https://developer.qcloudimg.com/http-save/audit-12040675/cf700bb9c3d5a78fa84e73e2f3409934.png","extension":"png","align":"center","alt":"","showAlt":false,"href":"","boxShadow":"","width":1012,"aspectRatio":"1.222789","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"table","attrs":{"id":"072bf149-6647-4667-b377-37eb3dffeba3","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"da3991cc-c302-4489-bbab-41ac160b182c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"策略"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"44e15f5f-29d2-4d74-b8bb-8e204a6cde8b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Token 节省"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"b302c683-7d69-49bf-866c-b5f9c5aa1056","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"信息保留"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"60904bf4-f196-4dfb-a8b3-dddbd313ac39","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"实现复杂度"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0a507c3d-f41c-4cfe-b374-94a69ba5da50","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"适用场景"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"d6bc9e38-b703-4e13-b456-0f7367e965e4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Compaction"},{"type":"text","text":"(全对话摘要)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9fa77ee4-0483-4d3b-8553-f90e563a9059","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(60-80%)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"e8fed190-f1ff-4276-922a-833b25c66156","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(结构化摘要)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"59aef5a5-3b9a-41e4-a17a-23e679142f53","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(需 LLM 调用)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"2e07c086-0599-46a1-a1f6-18defb92f259","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"长会话、复杂任务"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"41033650-1330-4aa3-b147-f579f961a54a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Observation Masking"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9404b543-7cc8-489f-b555-1594a418f1b6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中(40-60%)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0cacca8f-6d3a-488e-b54a-b5ad179dbd53","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中(丢失工具输出)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"65f7dcf2-0d91-487e-9c79-476ea57185ad","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"低(滑动窗口)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"82721b31-c33d-4890-9616-e16513fb7aaa","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"工具密集型任务"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"58b5f234-f4ee-49f4-8473-28fe2e4612ea","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"JIT Retrieval"},{"type":"text","text":"(即时检索)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0fce3043-9acf-4e5b-98cf-6d6fa9e344bf","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(按需加载)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"edf42e70-ae96-4dfc-b2cc-e3f2693c7fb1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(完整保留,用时才取)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"2a5560be-ef55-4d8f-b7f7-d7dcb89b52c6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中(需检索系统)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"94fe1137-0df8-451c-bb91-16d17f4aa9b2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"知识密集型任务"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"4f9ee5c4-28e7-4b11-91e8-8fa670680cbb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Tool Result Clearing"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"e9f4517c-d9dc-4f87-bda0-0d6198b2d6fb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中(30-50%)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"220d88ae-e8b3-4659-9a1e-d2b0a66e29ec","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"低(结果丢失)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"f67929be-146d-4f9c-affb-5faa9bfb42e5","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"极低(直接清除)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"cd3ef4c6-d2ed-48ab-a6da-0ef755ab02e1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"短期任务、快速原型"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"37d9085b-1f45-4c38-ba56-e28efedfe242","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Sub-agent Delegation"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"79afbe0f-5c80-493c-8769-5905fe71d5c3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(隔离上下文)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"31c4a847-3341-4c30-8041-22810ce38bad","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(各自独立)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"7571441f-64ff-4c6a-a0ff-657b133cb0e2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高(需编排框架)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"159a622c-4f75-4113-ac0c-37a254903629","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"多步骤并行任务"}]}]}]}]},{"type":"paragraph","attrs":{"id":"5bf07e58-6439-4809-9ea4-4c006fa77727","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"选型原则"},{"type":"text","text":":"}]},{"type":"orderedList","attrs":{"id":"4b773802-2797-4a6b-902d-1765b9196fea","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"693e45cd-aad5-430d-80d1-7e0337945b12"},"content":[{"type":"paragraph","attrs":{"id":"6c52463f-0464-47d9-a6cd-13aa99ea63b7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"先用最简单的(Observation Masking / Tool Result Clearing)"}]}]},{"type":"listItem","attrs":{"id":"d9475f36-32d3-4936-a0e3-691df5df0b1b"},"content":[{"type":"paragraph","attrs":{"id":"bcbfff0a-2c13-479b-9437-7ab08c13b199","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"不够再用 Microcompact(工具输出降级到磁盘)"}]}]},{"type":"listItem","attrs":{"id":"a9c6d4be-ce61-46be-884b-2f0436fcf9d4"},"content":[{"type":"paragraph","attrs":{"id":"8c801516-4a14-4cea-a239-8d6c54a3c01e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"长会话才上 Autocompact(全对话摘要)"}]}]},{"type":"listItem","attrs":{"id":"2242a9e5-a6af-4c89-83e8-16f5a5134e29"},"content":[{"type":"paragraph","attrs":{"id":"6f33ba50-87e4-4aef-ab53-5fb5f4760305","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"跨会话任务必须配 CLAUDE.md JIT Retrieval"}]}]},{"type":"listItem","attrs":{"id":"a949ec75-1c2b-4adc-af65-e0a23d977693"},"content":[{"type":"paragraph","attrs":{"id":"5e4c39cd-63d9-4244-8d52-4323f6907ee8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"多步骤任务考虑 Sub-agent Delegation(每个子 Agent 独立上下文)"}]}]}]},{"type":"horizontalRule","attrs":{"id":"8db2885c-2419-4b94-b850-1938ef7e5e01","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"f7fdc1f6-1cbd-48b0-acce-b7504126eab1","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"六、ACON:失败驱动的压缩优化"}]},{"type":"paragraph","attrs":{"id":"f941859a-dbce-4625-a6b3-e1d638f39999","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"前面讨论的策略都是"通用规则"——对所有任务一视同仁。但不同任务需要保留的信息类型不同。ACON(Agent Context OptimizatioN)研究提出了一种"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"失败驱动"},{"type":"text","text":"的压缩优化方法。"}]},{"type":"heading","attrs":{"id":"5335dce3-7396-4404-8525-6f3ab0df0136","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"核心思想"}]},{"type":"paragraph","attrs":{"id":"59f9e25f-b1c7-4c93-ac91-642b67b42cf8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"ACON 的洞察是:"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"与其猜测压缩时该保留什么,不如从失败中学习"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"id":"fbc7890a-ea32-409b-9fc5-c7881b8c177b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"工作流程:"}]},{"type":"orderedList","attrs":{"id":"b3415fcb-ef36-4ce3-83cb-ab7bf40941e5","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"5d5080a9-ae2a-4b23-96c4-17cc686830a5"},"content":[{"type":"paragraph","attrs":{"id":"14a42443-2145-490d-8a99-f8eed1f20528","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"收集失败对"},{"type":"text","text":":找到"完整上下文成功、压缩上下文失败"的任务对"}]}]},{"type":"listItem","attrs":{"id":"50eefbc3-8ffb-42b3-ba77-560d389910ca"},"content":[{"type":"paragraph","attrs":{"id":"bacf5cb0-b4ea-4980-ba46-3960149faf3a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"对比分析"},{"type":"text","text":":用强 LLM 分析压缩丢失了什么关键信息导致失败"}]}]},{"type":"listItem","attrs":{"id":"3c164b36-ad86-45aa-add4-752b556b3c32"},"content":[{"type":"paragraph","attrs":{"id":"6cf6b654-b7f5-4d04-889d-ac4e89bc4fa6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"更新压缩指南"},{"type":"text","text":":将"这类信息必须保留"的规则写入压缩 prompt"}]}]},{"type":"listItem","attrs":{"id":"87ad254b-bb4b-4f5f-9a62-9eb752a8c50a"},"content":[{"type":"paragraph","attrs":{"id":"6e6c2a9a-a7f7-4b85-8d5c-f9a13df5ab0e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"蒸馏小模型"},{"type":"text","text":":将优化后的压缩器蒸馏到小模型,降低运行成本"}]}]}]},{"type":"heading","attrs":{"id":"c34f649e-88c0-4151-a77f-37c88f0c88c0","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"实验结果"}]},{"type":"paragraph","attrs":{"id":"c35724c6-58b3-45b3-9194-9d6f04ee3aac","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"在 AppWorld、OfficeBench 和 Multi-objective QA 三个基准测试上:"}]},{"type":"table","attrs":{"id":"38cb2d22-8cb8-46c8-ba5b-433c81794324","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"d3927e69-e31f-467f-9a9d-64a79b8b86b9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"指标"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"444fbcfa-2af4-4795-9517-205d28caa3f2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"结果"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"6d101fb5-c63a-46b0-a0e4-e04f43424270","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Token 缩减"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"a3d99ad6-5560-42a4-89d1-353f262781d9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"26%-54%(峰值 token)"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"88941ada-4dfb-4d4f-a2c3-a28614c49506","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"准确率保持"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"e057e9e0-1d24-4735-9977-c9f39b94fca5","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"95% "}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"8d6be1de-7727-4214-9fd1-cd9b0f0d2a5b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"方法"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9abbba90-9412-481c-b9c3-ccab584ca512","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"无梯度优化(可直接应用于闭源模型)"}]}]}]}]},{"type":"paragraph","attrs":{"id":"1f7457e3-4448-4333-b88a-5063ffee7ace","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"后端类比"},{"type":"text","text":":ACON ≈ 自适应 GC 策略。传统 GC 用固定的分代策略,而自适应 GC(如 Java 的 ZGC)会根据运行时对象存活模式动态调整。ACON 根据任务失败模式动态调整压缩策略——不是"一刀切",而是"对症下药"。"}]},{"type":"horizontalRule","attrs":{"id":"a4b5cb60-2a9b-4dbf-a71c-fc0cb9d8ff20","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"72124351-c1fd-47b9-98c5-24cf508bf6bf","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"七、后端视角映射:上下文管理 ≈ 内存管理"}]},{"type":"paragraph","attrs":{"id":"a34ed970-0c3b-43f1-a19f-776dd3cc08e4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"如果你是后端工程师,理解 Agent 上下文管理最好的方式就是类比 JVM 内存管理。两者的核心挑战完全一致:"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"在有限的内存(上下文窗口)中,让进程(Agent)高效运行,同时管理好对象的分配、晋升和回收"},{"type":"text","text":"。"}]},{"type":"table","attrs":{"id":"2a9fed55-b05c-46ef-91ee-252bde6b230e","isHoverDragHandle":false},"content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600;"},"content":[{"type":"paragraph","attrs":{"id":"5024e6e3-1be4-4b2a-bb5c-493835003513","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Agent 上下文管理"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9906b271-ac35-44e9-ab41-757901b66a36","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"JVM 内存管理"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; font-weight: 600; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"9a4ab443-feca-4be8-a085-06e1a7a01028","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"共同原则"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"c3a60898-4890-4b5d-b3bb-652ce8411bb9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"上下文窗口(200K token)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"419025fd-2dbd-42fc-8253-3e7e602f4233","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"堆内存(如 4GB)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"f1ccb99b-6c12-4f0a-9cfe-9ed400cc833f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"有限资源,需要管理"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"0aa9d39f-9dee-42c0-b097-8f7dfa01896a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"消息列表"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"228dc3c5-5600-4e7f-9ca4-71db4a739fdd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"对象图"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"4d83e404-6523-47e4-ad95-41efadd4658c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"随时间增长,需要回收"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"fae022c3-3767-41b5-a388-79929ef57519","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Snip(快速剪除)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"af3fbdd0-325e-4461-9c67-a759a45aaebc","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Minor GC(Eden 区清理)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0ec3b407-9785-42a1-9e17-6d603be72788","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"快速、低代价、清理年轻代"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"1b427c44-e192-41c7-afcb-2d830b07f470","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Microcompact(工具输出降级到磁盘)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"3b68edec-4b9b-4b0d-b07a-e140154bd824","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"虚拟内存换页(swap)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"0e01c6e0-e4d6-4415-b528-62e008207d5f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"内存不够时,不活跃数据换到磁盘"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"6ca23e0f-ed6c-4465-9dc4-173503588460","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Context Collapse(渐进折叠)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"d43dcb4e-65e2-4786-b225-b20d5eb6afee","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"日志轮转 / Old GC"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"42080013-0d5d-438f-9e2e-a105f690c35a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"中等代价,压缩旧数据"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"2adf73f6-f56d-4128-b980-22dbab00abfe","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Autocompact(全对话摘要)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"774ca4ef-c545-413b-a4bb-960808823f90","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Full GC(Mark-Compact)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"bb970aa3-3509-4c6b-84c1-d9b815fca769","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"高代价、全量整理、暂停时间长"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"dc82e9d0-e4a4-4a5a-aae5-1f17189a306d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Reactive Compact(413 应急)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"24b1426a-ef91-4ddd-b80d-9e5ed1230c99","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"OOM Killer"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"027d84e1-5cd6-495c-9011-3b1e25030b5d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"最后手段,强制回收"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"4ffcb1a1-65fa-41b9-a0ce-53eb2b0c24b4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"CLAUDE.md(每轮重新注入)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"8c7a89ff-bd40-41c1-a6aa-e018e7b9fc4d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"配置中心 / 环境变量"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"81f15536-2833-4033-a3e3-2bb04de245c0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"重启不变量,GC 后自动恢复"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"265e17e4-b066-4932-aa07-0751d3b1e2eb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"JIT Retrieval(按需检索)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"a7a0cb69-05dd-4e1e-a3b8-f93e6c0e50ec","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"数据库懒加载"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"47f8d36f-7bec-40f2-b6a0-c81770b69d1f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"用的时候才查,不提前加载"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"68b83db4-26a6-4c60-88c1-ccdf9bb89ea2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"ACON(失败驱动优化)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"cb89e452-605a-49bd-8a33-52f6829946d1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"自适应 GC(ZGC / Shenandoah)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom: 0.555556px solid rgba(115, 115, 115, 0.12); line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"62da096d-82ad-45ff-869f-926e127d6532","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"根据运行时行为动态调整策略"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5;"},"content":[{"type":"paragraph","attrs":{"id":"85ac6889-ce34-462e-b52e-9bae13879ed2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":""Lost in the Middle""}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"fee32745-3617-4b13-92e3-ef2f03c56fb3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"缓存局部性原理"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1,"colwidth":null,"background":null,"color":null,"textAlign":null,"style":"padding: 8px 12px; border-bottom-width: medium; border-bottom-style: none; border-bottom-color: currentcolor; line-height: 1.5; border-left: 0.555556px solid rgba(115, 115, 115, 0.12);"},"content":[{"type":"paragraph","attrs":{"id":"6587c805-4d5e-4d7c-b84a-eb8cb845a739","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"位置影响访问效率"}]}]}]}]},{"type":"paragraph","attrs":{"id":"0e85a913-98e5-4536-999c-8de0641b69f7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"核心洞察"},{"type":"text","text":":后端工程师在内存管理上积累了数十年的经验——分代回收、晋升策略、懒加载、预读取、内存池化——这些几乎都可以一对一映射到 Agent 上下文管理。你不是在学一个全新的领域,而是在已有的知识树上嫁接新枝。"}]},{"type":"horizontalRule","attrs":{"id":"0313744f-5055-4372-a02d-755b8fa1475b","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"a381125b-6f9a-462d-b7f2-7be86bbe78a2","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"八、代码实战:带自动压缩策略的上下文管理器"}]},{"type":"paragraph","attrs":{"id":"912c2545-d5e0-4e34-9dba-49964f88afb6","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"下面用 Python 实现一个最小化但可用的上下文管理器,涵盖四种核心压缩策略:"}]},{"type":"paragraph","attrs":{"id":"c716885f-d5b2-474d-9897-c17d168a9bfd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"Python"}]},{"type":"codeBlock","attrs":{"id":"3c8d058e-05f4-434c-9f5f-ab6da2d2c5a2","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":""""n上下文管理器 —— 带自动压缩策略n模拟 Claude Code 的多级 Compaction 机制n"""nnimport jsonnimport hashlibnfrom dataclasses import dataclass, fieldnfrom enum import Enumnfrom typing import Optionalnnnclass CompactionStrategy(Enum):n"""压缩策略枚举"""nSNIP = "snip"# 快速剪除旧消息nMICROCOMPACT = "microcompact"# 工具输出降级为摘要nAUTOCOMPACT = "autocompact" # 全对话 summarizationnREACTIVE = "reactive"# 413 应急裁剪nnnclass MessageRole(Enum):nSYSTEM = "system"nUSER = "user"nASSISTANT = "assistant"nTOOL = "tool"nnn@dataclassnclass Message:n"""消息结构"""nrole: MessageRolencontent: strntokens: int = 0nis_tool_result: bool = Falsen# 标记是否为关键消息(用户请求、架构决策等),压缩时保留nis_critical: bool = Falsen# 工具输出的磁盘缓存路径(Microcompact 后填充)ndisk_path: Optional[str] = Nonenndef __post_init__(self):nif self.tokens == 0:n# 粗略估算:1 token ≈ 4 字符(中文约 2 字符/token)nself.tokens = max(1, len(self.content) // 3)nnnclass ContextManager:n"""n上下文管理器nn设计理念(对标 JVM 内存管理):n- 上下文窗口 = 堆内存n- 消息列表 = 对象图n- 压缩策略 = GC 策略(从轻量到重量)n- CLAUDE.md = 配置中心(每轮重新注入,不受 GC 影响)n"""nndef __init__(self, max_tokens: int = 200_000, claude_md: str = ""):nself.max_tokens = max_tokensn# 有效窗口 = 总窗口 - 预留摘要输出空间(对标 Claude Code 的 20K 预留)nself.effective_window = max_tokens - 20_000n# Autocompact 触发阈值(有效窗口的 95%)nself.autocompact_threshold = int(self.effective_window * 0.95)n# Snip 触发阈值(有效窗口的 80%)nself.snip_threshold = int(self.effective_window * 0.80)n# 熔断器:连续失败计数nself.consecutive_failures = 0nself.max_failures = 3n# CLAUDE.md 内容(每轮重新注入,相当于配置中心)nself.claude_md = claude_mdn# 消息列表nself.messages: list[Message] = []n# 压缩统计nself.compaction_stats = {n"snip": 0,n"microcompact": 0,n"autocompact": 0,n"reactive": 0,n}nndef add_message(self, role: MessageRole, content: str,nis_tool_result: bool = False, is_critical: bool = False):n"""添加消息"""nmsg = Message(nrole=role,ncontent=content,nis_tool_result=is_tool_result,nis_critical=is_critical,n)nself.messages.append(msg)n# 每次添加后检查是否需要压缩nself._check_and_compact()nndef get_total_tokens(self) -> int:n"""计算当前总 token 数"""nbase = sum(m.tokens for m in self.messages)nif self.claude_md:nbase = len(self.claude_md) // 3nreturn basenndef _check_and_compact(self):n"""检查并执行压缩(成本阶梯:从便宜到昂贵)"""ntotal = self.get_total_tokens()nn# 第一级:Snip(快速剪除)nif total > self.snip_threshold:nfreed = self._snip()nif freed > 0:nself.compaction_stats["snip"] = 1ntotal = self.get_total_tokens()nif total <= self.autocompact_threshold:nreturnnn# 第二级:Microcompact(工具输出降级到磁盘)nif total > self.snip_threshold:nfreed = self._microcompact()nif freed > 0:nself.compaction_stats["microcompact"] = 1ntotal = self.get_total_tokens()nif total <= self.autocompact_threshold:nreturnnn# 第三级:Autocompact(全对话摘要)nif total > self.autocompact_threshold:nif self.consecutive_failures < self.max_failures:nfreed = self._autocompact()nif freed > 0:nself.compaction_stats["autocompact"] = 1nself.consecutive_failures = 0nelse:nself.consecutive_failures = 1nndef _snip(self) -> int:n"""nSnip 策略:直接删除最旧的非关键消息n对标 JVM Minor GC —— 快速清理年轻代n"""nfreed = 0n# 从最旧的消息开始删除,跳过关键消息ni = 0nwhile i < len(self.messages) and self.get_total_tokens() > self.snip_threshold:nmsg = self.messages[i]nif not msg.is_critical and msg.role != MessageRole.SYSTEM:nfreed = msg.tokensnself.messages.pop(i)nelse:ni = 1nreturn freednndef _microcompact(self) -> int:n"""nMicrocompact 策略:工具输出内容存到磁盘,上下文保留摘要占位符n对标操作系统虚拟内存换页n"""nfreed = 0n# 保留最近 5 条工具结果ntool_indices = [i for i, m in enumerate(self.messages) if m.is_tool_result]nkeep_recent = 5nnfor idx in tool_indices[:-keep_recent] if len(tool_indices) > keep_recent else []:nmsg = self.messages[idx]nif msg.disk_path is None and "[已压缩]" not in msg.content:n# 模拟存到磁盘ndisk_key = hashlib.md5(nmsg.content.encode()n).hexdigest()[:8]nmsg.disk_path = f"/tmp/agent_ctx/{disk_key}.json"nnoriginal_tokens = msg.tokensn# 替换为摘要占位符nmsg.content = f"[已压缩: 工具输出已存储到 {msg.disk_path}]"nmsg.tokens = max(1, len(msg.content) // 3)nfreed = original_tokens - msg.tokensnnreturn freednndef _autocompact(self) -> int:n"""nAutocompact 策略:全对话 summarizationn对标 JVM Full GC —— Mark-Compactnn生产环境中这里会 fork 子 Agent 生成 9 段式摘要n本示例用简化版模拟n"""nif len(self.messages) < 10:nreturn 0nnbefore_tokens = self.get_total_tokens()nn# 构建结构化摘要(简化版 9 段式)nsummary_parts = []nsummary_parts.append("## Primary Request")n# 保留所有关键用户消息nuser_msgs = [m for m in self.messagesn if m.role == MessageRole.USER and m.is_critical]nsummary_parts.append("n".join(m.content[:200] for m in user_msgs))nnsummary_parts.append("n## Key Decisions")n# 保留所有标记为关键的消息ncritical_msgs = [m for m in self.messages if m.is_critical]nsummary_parts.append("n".join(m.content[:200] for m in critical_msgs))nnsummary_parts.append("n## Current Work")n# 保留最近 3 条消息的摘要nrecent = self.messages[-3:]nsummary_parts.append("n".join(m.content[:200] for m in recent))nnsummary_text = "nn".join(summary_parts)nn# 重建消息列表:System CLAUDE.md 摘要 最近消息nsystem_msgs = [m for m in self.messages if m.role == MessageRole.SYSTEM]nrecent_msgs = self.messages[-5:]# 保留最近 5 条nnself.messages = system_msgs [nMessage(nrole=MessageRole.SYSTEM,ncontent=f"[Context Summary]n{summary_text}",nis_critical=True,n)n] recent_msgsnnafter_tokens = self.get_total_tokens()nreturn before_tokens - after_tokensnndef reactive_compact(self):n"""nReactive Compact:API 413 时的应急裁剪n对标 OOM Killer —— 强制回收,保系统运行n"""nbefore = len(self.messages)n# 从尾部逐轮剥离,只保留 System 最近 2 条nsystem_msgs = [m for m in self.messages if m.role == MessageRole.SYSTEM]nrecent = self.messages[-2:]nself.messages = system_msgs recentnself.compaction_stats["reactive"] = 1nreturn before - len(self.messages)nndef get_context_for_llm(self) -> list[dict]:n"""获取发送给 LLM 的上下文(含 CLAUDE.md 注入)"""ncontext = []n# CLAUDE.md 每轮注入(配置中心模式)nif self.claude_md:ncontext.append({n"role": "system",n"content": self.claude_md,n})nfor msg in self.messages:ncontext.append({n"role": msg.role.value,n"content": msg.content,n})nreturn contextnndef get_stats(self) -> dict:n"""获取压缩统计"""nreturn {n"total_tokens": self.get_total_tokens(),n"max_tokens": self.max_tokens,n"usage_rate": f"{self.get_total_tokens() / self.max_tokens:.1%}",n"message_count": len(self.messages),n"compaction_stats": self.compaction_stats,n"consecutive_failures": self.consecutive_failures,n}nnn# ==================== 使用示例 ====================nnif __name__ == "__main__":n# 初始化上下文管理器n# CLAUDE.md 作为"项目宪法",每轮自动注入nclaude_md = """n## 项目规则n- 所有服务间通信使用 gRPCn- 代码注释使用中文n- 测试覆盖率必须 > 80%n"""nnctx = ContextManager(max_tokens=200_000, claude_md=claude_md)nn# 模拟 Agent 对话nctx.add_message(MessageRole.USER,n"重构用户服务,将 REST API 改为 gRPC",nis_critical=True)# 标记为关键消息nn# 模拟大量工具调用(文件读取等)nfor i in range(100):nctx.add_message(MessageRole.ASSISTANT,nf"正在分析第 {i} 个文件的结构...")nctx.add_message(MessageRole.TOOL,nf"文件 src/service_{i}.py 的内容:n"n "x" * 2000,# 模拟大文件内容nis_tool_result=True)nn# 添加更多关键决策nctx.add_message(MessageRole.ASSISTANT,n"架构决策:使用 Protocol Buffers 定义 gRPC 接口",nis_critical=True)nn# 继续模拟对话...nfor i in range(50):nctx.add_message(MessageRole.ASSISTANT,nf"正在实现第 {i} 个 gRPC 方法...")nctx.add_message(MessageRole.TOOL,nf"测试结果:PASSn" "y" * 500,nis_tool_result=True)nn# 打印统计nstats = ctx.get_stats()nprint("=" * 60)nprint("上下文管理器统计报告")nprint("=" * 60)nprint(f"当前 Token 使用: {stats['total_tokens']:,} / {stats['max_tokens']:,}")nprint(f"使用率: {stats['usage_rate']}")nprint(f"消息数量: {stats['message_count']}")nprint(f"n压缩策略触发统计:")nfor strategy, count in stats['compaction_stats'].items():nif count > 0:nprint(f"{strategy}: {count} 次")nprint(f"n熔断器状态: {stats['consecutive_failures']}/{ctx.max_failures}")nn# 验证关键信息是否保留ncontext = ctx.get_context_for_llm()nhas_grpc_decision = any("gRPC" in m["content"] for m in context)nprint(f"n关键决策保留检查:")nprint(f"'gRPC' 决策是否保留: {'是' if has_grpc_decision else '否'}")nprint(f"CLAUDE.md 是否注入: {'是' if any('gRPC' in m['content'] for m in context[:1]) else '否'}")"}]},{"type":"heading","attrs":{"id":"1e73c1b0-c1d8-4ac9-9e02-f0c40b1a1edd","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"代码设计要点"}]},{"type":"orderedList","attrs":{"id":"8a7c05d7-7483-4cf1-acdc-a85ac1c21045","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"fc421ebf-29e8-4944-8fb5-e5f6b570f77c"},"content":[{"type":"paragraph","attrs":{"id":"de06f055-3f9d-43a4-b60a-0ae18cfef8ab","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"成本阶梯"},{"type":"text","text":":"},{"type":"text","marks":[{"type":"code"}],"text":"_check_and_compact()"},{"type":"text","text":" 方法严格按 Snip → Microcompact → Autocompact 顺序触发,总是先试最便宜的"}]}]},{"type":"listItem","attrs":{"id":"35ec761a-05f6-4ae8-ada8-0d91aaba717c"},"content":[{"type":"paragraph","attrs":{"id":"6a2b70df-b086-42c0-94ca-2be5eac34b12","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"熔断器"},{"type":"text","text":":连续失败 3 次后停止 Autocompact,避免雪崩"}]}]},{"type":"listItem","attrs":{"id":"1c347149-83c6-4206-aaa4-84130b6914e0"},"content":[{"type":"paragraph","attrs":{"id":"b4df4a81-7bc6-44c6-9f5f-eee3480c169e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"CLAUDE.md 注入"},{"type":"text","text":":"},{"type":"text","marks":[{"type":"code"}],"text":"get_context_for_llm()"},{"type":"text","text":" 每次调用都会注入 CLAUDE.md,不受压缩影响"}]}]},{"type":"listItem","attrs":{"id":"b82e62d5-6ee6-40b2-8e68-d4bd1c62bd74"},"content":[{"type":"paragraph","attrs":{"id":"1aa30949-aedb-4e5d-ab22-8738bcd64b25","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"关键消息保护"},{"type":"text","text":":"},{"type":"text","marks":[{"type":"code"}],"text":"is_critical"},{"type":"text","text":" 标记的消息在 Snip 时被跳过"}]}]},{"type":"listItem","attrs":{"id":"4435bbf3-2779-4915-8a1f-57697e85ecf7"},"content":[{"type":"paragraph","attrs":{"id":"9ab780c5-7f7c-49ad-ba5c-95e97e036f72","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"工具输出降级"},{"type":"text","text":":Microcompact 将工具输出替换为占位符,原始内容存到磁盘"}]}]}]},{"type":"horizontalRule","attrs":{"id":"115a639d-13de-4679-a03e-4b5cff19a3a5","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"b44a6c2b-b818-4b47-b259-de199c071b01","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"九、总结与下篇预告"}]},{"type":"heading","attrs":{"id":"caeda1f1-3962-4e64-af3b-ca76bdf1af9a","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"本文核心要点"}]},{"type":"paragraph","attrs":{"id":"fba82be9-bc3d-42c4-8502-48cc2f0a0be9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"上下文管理是 Agent Harness 的"内存管理"——它决定了 Agent 能在多长的任务中保持高效。我们通过五个层次建立了完整的认知闭环:"}]},{"type":"paragraph","attrs":{"id":"43786406-93e8-45d7-b0fb-c60d28ae458d","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"Plain Text"}]},{"type":"codeBlock","attrs":{"id":"640f7241-d3f7-4dce-a8e7-b3d3e787b418","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":"问题定义 → Context Rot 是什么?为什么 200K 窗口也会不够用?n↓n根因分析 → Lost in the Middle Summarization 的隐藏代价n↓n工程方案 → Claude Code 五级压缩策略(成本阶梯)n↓n最佳实践 → 内容存活规则 CLAUDE.md 持久化n↓n前沿探索 → ACON 失败驱动优化 JetBrains 简单即是美n↓n后端映射 → 上下文管理 ≈ JVM 内存管理(一对一映射)n↓n代码落地 → 带自动压缩策略的上下文管理器"}]},{"type":"paragraph","attrs":{"id":"1404c445-2508-49ee-a3d2-e4b421605362","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"一句话总结"},{"type":"text","text":":上下文管理不是"删掉旧消息",而是"像 GC 管理内存一样管理注意力"——分代回收、懒加载、按需检索、持久化不变量,后端工程师已有的内存管理经验可以直接迁移。"}]},{"type":"heading","attrs":{"id":"d4485b48-3e5b-46b5-8808-b128719ac84a","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"下篇预告"}]},{"type":"paragraph","attrs":{"id":"44fdb409-93ee-4d13-b103-7aa2a9e76e7c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"下一篇我们将进入"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}},{"type":"bold"}],"text":"状态管理"},{"type":"text","text":"的主题——当 Agent 执行到一半崩溃了,如何从断点恢复?如何回溯调试哪一步出了问题?我们将拆解 LangGraph 的图检查点机制、Claude Code 的"Git 作为状态机"创新做法,以及 Ralph Loop 的跨会话状态传递。如果说上下文管理是"内存管理",那么状态管理就是"事务与持久化"——同样是你熟悉的后端战场。"}]},{"type":"horizontalRule","attrs":{"id":"76d53a52-aaa3-4cc0-b985-b99a07cd6ab9","isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"f5c0c090-f016-45de-bf8f-a65c9d596813","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"参考文献"}]},{"type":"orderedList","attrs":{"id":"efd4c17e-2e81-42ee-9068-866c2073d052","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"5cdccf1f-dfaa-46d7-a9d5-e948cc1a670c"},"content":[{"type":"paragraph","attrs":{"id":"f340f529-ac0b-427d-8653-06bc3254ce73","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Liu, N.F. et al. "Lost in the Middle: How Language Models Use Long Contexts." arXiv:2307.03172, 2023. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://arxiv.org/abs/2307.03172","target":"_blank","rel":"noopener noreferrer","class":"markdown-link"}}],"text":"https://arxiv.org/abs/2307.03172"}]}]},{"type":"listItem","attrs":{"id":"0b0cc162-aa29-4ab2-9746-2cff109b0a5e"},"content":[{"type":"paragraph","attrs":{"id":"ce2a42ef-81b0-4782-b4c7-480a8345dea7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":""Context Compaction — Claude Code 源码解析." "}]},{"type":"paragraph","attrs":{"id":"f0ba0941-3c40-4b76-8d1a-a221c375ad23","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.markdown.engineering/learn-claude-code/15-context-compaction","target":"_blank","rel":"noopener noreferrer","class":"markdown-link"}}],"text":"https://www.markdown.engineering/learn-claude-code/15-context-compaction"}]}]},{"type":"listItem","attrs":{"id":"5521768d-a8dc-4ba1-b12f-a9311bd4aa35"},"content":[{"type":"paragraph","attrs":{"id":"4971e4df-ca5b-4ad6-9bf6-4021938f82f0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Lindenbauer, T. et al. "The Complexity Trap: Simple Observation Masking Is as Efficient as LLM Summarization for Agent Context Management." NeurIPS DL4C Workshop, 2025. "}]},{"type":"paragraph","attrs":{"id":"6e327258-9ccc-42b2-8cde-b7d1d2803784","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://arxiv.org/abs/2508.21433","target":"_blank","rel":"noopener noreferrer","class":"markdown-link"}}],"text":"https://arxiv.org/abs/2508.21433"}]}]},{"type":"listItem","attrs":{"id":"c3a76eda-4120-4bf8-b6cf-29dd4d354841"},"content":[{"type":"paragraph","attrs":{"id":"392bc9cc-13cd-49a9-81bf-b3ff8bd6ec28","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"JetBrains Research. "Cutting Through the Noise: Smarter Context Management for LLM-Powered Agents." 2025. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://blog.jetbrains.com/research/2025/12/efficient-context-management/","target":"_blank","rel":"noopener noreferrer","class":"markdown-link"}}],"text":"https://blog.jetbrains.com/research/2025/12/efficient-context-management/"}]}]},{"type":"listItem","attrs":{"id":"41a3ff3f-29f7-435a-9004-fded05a5ed50"},"content":[{"type":"paragraph","attrs":{"id":"ed2d768d-17e1-4cde-9b74-273ae61edf8f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":""ACON: Optimizing Context Compression for Long-Horizon LLM Agents." arXiv:2510.00615, 2025. "}]},{"type":"paragraph","attrs":{"id":"932ab88b-ee8b-47e5-b3e1-5c5338d5ccdd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://arxiv.org/abs/2510.00615","target":"_blank","rel":"noopener noreferrer","class":"markdown-link"}}],"text":"https://arxiv.org/abs/2510.00615"}]}]},{"type":"listItem","attrs":{"id":"98c4d825-4860-4585-8f97-3ee0f14ff513"},"content":[{"type":"paragraph","attrs":{"id":"fae09b58-5b21-4182-9afc-88479de218bb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Young, J. "Effective Harnesses for Long-Running Agents." Anthropic Engineering, 2025.11"}]}]},{"type":"listItem","attrs":{"id":"89f31f06-5edf-45fc-a3e2-051f375ac27b"},"content":[{"type":"paragraph","attrs":{"id":"ba8e1558-3729-41a4-985b-d64f35960688","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":""Dive into Claude Code: The Design Space of Today's and Future AI Agent Systems." arXiv:2604.14228"}]}]}]},{"type":"paragraph","attrs":{"id":"70296e59-daba-451f-a41b-3d8e6e1028c9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}}]}","createTime":1785679692,"ext":{"closeTextLink":0,"comment_ban":0,"description":"","focusRead":0},"favNum":0,"html":"","isOriginal":0,"likeNum":0,

点击查看更多
推荐专题
热门阅读