# 第六章：记忆与上下文子系统

## 章引言

记忆是智能体系统的第二大脑。没有记忆，Agent 无法学习，每次对话都是从零开始。记忆系统决定了智能体是否能够：

* 记住用户的偏好和背景
* 在多个会话间保持一致性
* 学习和改进其行为
* 高效地处理长时对话（通过压缩而非简单截断）

本章将深入 Harness 的记忆设计，对比 Claude Code 和 OpenClaw 的不同方法。

## 核心设计问题

1. **多层架构**：工作记忆（当前会话）、短期记忆（会话间）、长期记忆（持久化）如何划分？
2. **可写入性**：Agent 不仅读取记忆，还要能更新和创建记忆
3. **组装能力**：如何从多个来源的记忆组装出完整的上下文？
4. **整合机制**：长对话中，何时触发记忆整合（压缩/清理）？

## 两个参考系统

**Claude Code 的三层模型**：

* 对话历史：当前会话的完整消息
* CLAUDE.md：用户反馈、项目信息、参考资料
* autoDream 系统：三门触发、四阶段整合(Orient→Gather→Consolidate→Prune)

**OpenClaw 的双层模型**：

* MEMORY.md：永久性记忆（关键事实、学习、历史）
* 每日日志：当前会话的详细记录
* memory\_search 语义检索：找到相关的历史信息
* 自动刷写：70% 上下文触发记忆整合

## 本章结构

* 6.1：多层记忆架构
* 6.2：可写入式记忆
* 6.3：上下文组装引擎
* 6.4：记忆整合与压缩
* 6.5：MiniHarness 记忆实现
* 本章小结

记忆系统是智能体系统中最复杂但也最关键的子系统之一。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/harness_engineering_guide/di-er-bu-fen-harness-he-xin-zi-xi-tong/06_memory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
