Files
nexus/wiki/concepts/上下文刷新.md
2026-04-23 04:02:48 +08:00

75 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "上下文刷新Memory Flush"
type: concept
tags: [openclaw, memory, context-window, compaction]
sources: [养龙虾5天血泪史]
last_updated: 2026-04-23
---
## Definition
上下文刷新是 OpenClaw 的压缩前内存保护机制——在压缩器运行前,自动触发静默回合,提示 Agent 将重要上下文写入磁盘,确保关键信息在压缩后仍然可用。
## How It Works
```
对话累积 → 接近 Context Window → Memory Flush 触发 → Agent 写入 memory/YYYY-MM-DD.md → 压缩运行 → 摘要丢失但重要内容存活
```
### 配置示例
```json
{
"compaction": {
"memoryFlush": {
"enabled": true,
"softThresholdTokens": 4000
}
}
}
```
### 参数说明
| 参数 | 说明 |
|------|------|
| `enabled` | 是否启用内存刷新 |
| `softThresholdTokens` | 触发刷新的 token 阈值 |
### 阈值设置原则
- **小上下文模型**8K-32K4000-8000 合适
- **大上下文模型**128K-200K需要更高阈值避免过早触发导致上下文碎片化
- **公式**Context Window × 0.2 ~ 0.4
## The Gap: Memory Flush Only Runs Once Per Compression Cycle
> "内存刷新每个压缩周期只触发一次。如果会话足够长,有两三次压缩,只有第一次得到刷新处理。之后的一切都处于风险中。"
### 补充方案Context Pruning
```json
{
"contextPruning": {
"mode": "cache-ttl",
"ttl": "6h",
"keepLastAssistants": 3
}
}
```
- 在 6 小时后积极修剪旧上下文
- 同时保留最后 3 个助手响应
- 与 Memory Flush 结合:早期将重要内容写入磁盘,旧上下文在溢出前被清理
## Key Insight
> "如果在磁盘上,它能在压缩中存活。如果只在对话中,它就有风险。"
## Connections
- [[上下文压缩]] ← Memory Flush 防止上下文压缩的信息丢失
- [[Context-Pruning]] ← 与 Memory Flush 协同工作
- [[写入纪律]] ← Memory Flush 是写入纪律的技术实现
- [[交接协议]] ← 互补Memory Flush 处理压缩周期,交接协议处理模型切换
- [[养龙虾5天血泪史]] ← 主要来源