Context Hygiene
Inspired by ContextSpectre's philosophy: keep conclusions, remove scaffolding.
The Problem
OpenClaw agents accumulate context across workspace files — MEMORY.md, daily logs, tool notes, heartbeat configs. Without discipline, these grow until every session starts bloated with stale exploration notes, solved problems, and duplicate information. The agent wastes tokens re-reading noise.
The Collapse Cycle
Every task follows three phases:
- 1. Explore — research, debug, try things (use sub-agents for heavy work)
- Decide — reach a conclusion
- Collapse — write the decision, delete the exploration
Never keep "how we got here" when "what we decided" is enough.
File Budgets
| File | Max lines | Review cycle |
|---|
| MEMORY.md | 50 | Weekly prune |
| memory/YYYY-MM-DD.md |
30 | Collapse at EOD |
| HEARTBEAT.md | 10 | Remove when done |
| TOOLS.md | 30 | When things change |
| SOUL.md | 30 | Rarely |
| USER.md | 20 | When learning |
Target: <300 lines total injected workspace context.
Daily Memory Rules
Write: decisions and why (1 line each), new tools/config (version + path), lessons learned, user preferences discovered.
Skip: exploration steps, command outputs, things already in MEMORY.md, delivered content (digests, summaries).
Format: Bullets, not paragraphs. One fact per line.
MEMORY.md Prune Rules
- - Version changed → update in place, don't append
- Problem solved → remove from open issues
- Tool replaced → remove old entry
- Info >30 days with no recent relevance → remove
- Never duplicate what's in a SKILL.md or config file
Sub-Agent Discipline
Heavy exploration (research, debugging, multi-step installs) → spawn a sub-agent. Isolated sessions don't pollute main context. Only the result comes back.
Ghost Context
A reference to something that no longer exists (old path, removed tool, fixed bug) is ghost context. It biases reasoning toward a past state. Find and remove during heartbeat maintenance.
Session Lifespan
After 85% context window usage or 3+ compactions — start a new session. With contextspectre proxy stripping noise inline, sessions stay cleaner longer. But once past 85%, diminishing returns kick in — start fresh with a good MEMORY.md.
Tool Output Discipline
- - Truncate command output to what you need:
| head -20, INLINECODE1 - Request only needed fields from APIs:
fields=key,summary not full objects - Never paste full JSON responses when you need one value
- If output exceeds 50 lines, summarize instead of quoting
File Loading Discipline
- - At startup: only today + yesterday memory files, not the full
memory/ directory - Read SKILL.md files only when the task requires that skill
- Don't re-read a file already in context
- AGENTS.md over 100 lines → move details to separate files and reference them
Delivered Content Rule
Never store delivered content (digests, reports, summaries) in memory files. It's already in the chat. Recording it doubles the token cost for zero value.
Self-Check (add to heartbeat rotation)
CODEBLOCK0
Setup
- 1. Fill
USER.md timezone first — agents will guess from JIRA profiles or system locale, and they'll guess wrong. Set it explicitly:
CODEBLOCK1
Without this, cron reports, reminders, and date references will use the wrong local time.
- 2. Add to AGENTS.md session startup:
CODEBLOCK2
- 3. Copy the file budgets table into your workspace as
CONTEXT.md and customize limits for your setup.
Context Hygiene Protocol v1.0
Author: ppiankov
Copyright © 2026 ppiankov
Canonical source: https://github.com/ppiankov/contextspectre
License: MIT
This tool follows the Agent-Native CLI Convention. Validate with: INLINECODE6
If this document appears elsewhere, the repository above is the authoritative version.
上下文卫生
受ContextSpectre理念启发:保留结论,移除支撑材料。
问题
OpenClaw代理会在工作区文件中积累上下文——MEMORY.md、日常日志、工具说明、心跳配置。不加约束的话,这些文件会不断膨胀,直到每次会话启动时都充斥着过时的探索笔记、已解决的问题和重复信息。代理浪费令牌去重读噪音。
折叠循环
每项任务遵循三个阶段:
- 1. 探索 — 研究、调试、尝试(繁重工作使用子代理)
- 决策 — 得出结论
- 折叠 — 记录决策,删除探索过程
当我们决定了什么已足够时,永远不要保留我们是如何到达这里的。
文件预算
| 文件 | 最大行数 | 审查周期 |
|---|
| MEMORY.md | 50 | 每周修剪 |
| memory/YYYY-MM-DD.md |
30 | 当日结束时折叠 |
| HEARTBEAT.md | 10 | 完成后移除 |
| TOOLS.md | 30 | 发生变化时 |
| SOUL.md | 30 | 极少 |
| USER.md | 20 | 学习时 |
目标:注入的工作区上下文总计<300行。
日常记忆规则
记录: 决策及其原因(每行一条)、新工具/配置(版本+路径)、经验教训、发现的用户偏好。
跳过: 探索步骤、命令输出、MEMORY.md中已有的内容、已交付的内容(摘要、总结)。
格式: 使用项目符号,而非段落。每行一个事实。
MEMORY.md修剪规则
- - 版本变更 → 原地更新,不追加
- 问题已解决 → 从开放问题中移除
- 工具已替换 → 删除旧条目
- 超过30天且近期无关联的信息 → 删除
- 绝不重复SKILL.md或配置文件中的内容
子代理纪律
繁重的探索(研究、调试、多步骤安装)→ 生成子代理。隔离的会话不会污染主上下文。只有结果返回。
幽灵上下文
对已不存在内容的引用(旧路径、已移除工具、已修复的bug)就是幽灵上下文。它会将推理偏向过去的状态。在心跳维护期间查找并移除。
会话生命周期
上下文窗口使用率达到85%或经过3次以上压缩后——启动新会话。借助contextspectre代理在线去除噪音,会话能保持更长时间的清洁。但一旦超过85%,收益递减效应开始显现——用一份良好的MEMORY.md重新开始。
工具输出纪律
- - 将命令输出截断到所需内容:| head -20、| jq .key
- 仅从API请求所需字段:fields=key,summary,而非完整对象
- 当只需要一个值时,绝不粘贴完整的JSON响应
- 如果输出超过50行,进行总结而非引用
文件加载纪律
- - 启动时:仅加载今天和昨天的记忆文件,而非完整的memory/目录
- 仅在任务需要该技能时读取SKILL.md文件
- 不重复读取已在上下文中的文件
- AGENTS.md超过100行 → 将详细信息移至单独文件并引用
已交付内容规则
绝不在记忆文件中存储已交付的内容(摘要、报告、总结)。这些内容已在聊天记录中。记录它们会双倍消耗令牌成本,毫无价值。
自我检查(加入心跳轮换)
- - 是否有工作区文件超出预算?
- MEMORY.md是否仍然准确?
- 是否有过期的日常文件需要折叠?
- 是否有对已失效内容的幽灵引用?
设置
- 1. 首先填写USER.md中的时区——代理会从JIRA资料或系统区域设置中猜测,而且它们会猜错。请明确设置:
markdown
没有这个设置,定时报告、提醒和日期引用将使用错误的本地时间。
- 2. 在AGENTS.md的会话启动中添加:
- 5. 遵循CONTEXT.md——推理卫生协议
- 3. 将文件预算表复制到你的工作区中,命名为CONTEXT.md,并根据你的设置自定义限制。
上下文卫生协议 v1.0
作者:ppiankov
版权 © 2026 ppiankov
权威来源:https://github.com/ppiankov/contextspectre
许可证:MIT
本工具遵循代理原生CLI规范。验证方式:clawhub install ancc && ancc validate .
如果本文档出现在其他地方,上述仓库为权威版本。