clawdoc
Examine agent sessions. Diagnose failures. Prescribe fixes.
Invocation modes
/clawdoc (slash command — default: headline mode)
Produces a compact, tweetable health check:
🩻 clawdoc — 3 findings across 12 sessions (last 7 days)
💸 $47.20 spent — $31.60 was waste (67% recoverable)
🔴 Retry loop on exec burned $18.40 in one session
🟡 Opus running 34 heartbeats ($8.20 → $0.12 on Haiku)
🟡 SOUL.md is 9,200 tokens — 14% of your context window
Run: INLINECODE1
/clawdoc full or "give me a full diagnosis"
Runs all 14 pattern detectors and produces the complete diagnosis report with evidence and prescriptions.
/clawdoc brief or "clawdoc one-liner for daily brief"
Single-line summary for morning cron integration:
Yesterday: 8 sessions, $3.40, 1 warning (cron context growth on daily-report)
Run: INLINECODE4
Natural language triggers
Also activates when user says: "what went wrong", "why did that fail", "debug", "diagnose", "why was that so expensive", "where are my tokens going", "cost breakdown", "health check", "check my agent", "what's wrong", "examine"
Quick examination — most recent session
Find the most recent session file and run:
bash {baseDir}/scripts/examine.sh <session.jsonl>
This outputs a JSON summary with turns, cost, token counts, tool call frequency, and error count.
Single-session diagnosis
Run all 14 pattern detectors against a specific session file:
CODEBLOCK3
Diagnosis with prescriptions
Pipe diagnose output into prescribe for a formatted report with fix recommendations:
CODEBLOCK4
Cost breakdown
Show per-turn cost waterfall for a session:
CODEBLOCK5
Cross-session pattern recurrence
Analyze pattern recurrence across multiple sessions in a directory:
CODEBLOCK6
Full diagnosis
When the user wants a comprehensive diagnosis, run the scripts above and synthesize findings into this report format:
Diagnosis report format
CODEBLOCK7
Pattern reference
| # | Pattern | Severity | Key indicator |
|---|
| 1 | Infinite retry loop | 🔴 Critical | Same tool called 5+ times consecutively |
| 2 |
Non-retryable error retried | 🔴 High | Validation error → identical retry |
| 3 | Tool calls as text | 🔴 High | Tool names in assistant text, no toolCall blocks |
| 4 | Context window exhaustion | 🟡-🔴 | inputTokens > 70% of contextTokens |
| 5 | Sub-agent replay | 🟡 Medium | Duplicate completion messages in parent |
| 6 | Cost spike | 🟡-🔴 | Session cost > 2x rolling average |
| 7 | Skill selection miss | 🟢 Low | "command not found" after skill activation |
| 8 | Model routing waste | 🟡 Medium | Premium model on heartbeat/cron |
| 9 | Cron context accumulation | 🟡 Medium | Growing inputTokens across cron runs |
| 10 | Compaction damage | 🟡 Medium | Post-compaction tool call repetition |
| 11 | Workspace token overhead | 🟡 Medium | Baseline > 15% of context window |
| 12 | Task drift | 🟡 Medium | Post-compaction directory divergence or 10+ reads without edits |
| 13 | Unbounded walk | 🟠 High | Repeated unscoped find/grep -r flooding output |
| 14 | Tool misuse | 🟡 Medium | Same file read 3+ times without edit, or identical search repeated |
Self-improving-agent integration
To enable writing findings to .learnings/LEARNINGS.md, set CLAWDOC_LEARNINGS=1 before running prescribe:
CODEBLOCK8
Tips
- - Session JSONL files are the ground truth for all diagnostics
- Use
jq -s (slurp) for aggregations across all lines in a session file - Filter
message.content[] by type=="text" for readable content, type=="toolCall" for tool invocations - When prescribing config changes, always show the exact JSON path and value
技能名称: clawdoc
详细描述:
clawdoc
检查代理会话。诊断故障。给出修复方案。
调用模式
/clawdoc(斜杠命令 — 默认:标题模式)
生成简洁、可推文的健康检查报告:
🩻 clawdoc — 12个会话中发现3个问题(最近7天)
💸 已花费$47.20 — 其中$31.60为浪费(67%可回收)
🔴 执行重试循环在一次会话中烧掉$18.40
🟡 Opus运行了34次心跳($8.20 → 使用Haiku只需$0.12)
🟡 SOUL.md占用9,200个token — 占你上下文窗口的14%
运行:bash {baseDir}/scripts/headline.sh ~/.openclaw/agents/main/sessions
/clawdoc full 或 给我一份完整诊断
运行全部14个模式检测器,生成包含证据和修复方案的完整诊断报告。
/clawdoc brief 或 clawdoc每日简报一句话
用于早晨cron集成的单行摘要:
昨天:8个会话,$3.40,1个警告(daily-report的cron上下文增长)
运行:bash {baseDir}/scripts/headline.sh --brief ~/.openclaw/agents/main/sessions
自然语言触发
当用户说以下内容时也会激活:哪里出错了、为什么失败了、调试、诊断、为什么这么贵、我的token去哪了、成本明细、健康检查、检查我的代理、有什么问题、检查
快速检查 — 最近会话
找到最近的会话文件并运行:
bash {baseDir}/scripts/examine.sh
输出包含轮次、成本、token数量、工具调用频率和错误计数的JSON摘要。
单会话诊断
对特定会话文件运行全部14个模式检测器:
bash {baseDir}/scripts/diagnose.sh | jq .
带修复方案的诊断
将诊断输出通过管道传递给prescribe,生成带修复建议的格式化报告:
bash {baseDir}/scripts/diagnose.sh | bash {baseDir}/scripts/prescribe.sh
成本明细
显示会话的逐轮成本瀑布图:
bash {baseDir}/scripts/cost-waterfall.sh | jq .[0:5]
跨会话模式重复
分析目录中多个会话的模式重复情况:
bash {baseDir}/scripts/history.sh | jq .
完整诊断
当用户需要全面诊断时,运行上述脚本并将发现综合成以下报告格式:
诊断报告格式
🩻 诊断 — [日期]
患者摘要
- - 检查的会话数:N
- 时间段:[日期范围]
- 总花费:$X.XX
- 总token数:输入XXk / 输出XXk
发现
🔴 严重
[无限重试循环、上下文耗尽、工具作为文本失败]
每个发现包括:发生了什么、证据、预估成本影响、以及具体修复方案。
🟡 警告
[成本飙升、模型路由浪费、cron累积、压缩损坏、工作区开销]
🟢 健康
[运行良好的部分 — 高效会话、良好的模型路由]
修复方案(按成本影响排序)
- 1. [影响最大的修复,附带具体配置变更或命令]
- [第二高]
- [第三]
成本明细
[检查期间每日成本]
[成本最高的3个会话及其根本原因]
模式参考
| # | 模式 | 严重程度 | 关键指标 |
|---|
| 1 | 无限重试循环 | 🔴 严重 | 同一工具连续调用5次以上 |
| 2 |
重试不可重试的错误 | 🔴 高 | 验证错误 → 完全相同的重试 |
| 3 | 工具作为文本调用 | 🔴 高 | 助手文本中出现工具名称,无toolCall块 |
| 4 | 上下文窗口耗尽 | 🟡-🔴 | inputTokens > contextTokens的70% |
| 5 | 子代理重放 | 🟡 中 | 父代理中出现重复的完成消息 |
| 6 | 成本飙升 | 🟡-🔴 | 会话成本 > 滚动平均值的2倍 |
| 7 | 技能选择失误 | 🟢 低 | 技能激活后出现命令未找到 |
| 8 | 模型路由浪费 | 🟡 中 | 在心跳/cron上使用高级模型 |
| 9 | Cron上下文累积 | 🟡 中 | 跨cron运行的inputTokens持续增长 |
| 10 | 压缩损坏 | 🟡 中 | 压缩后工具调用重复 |
| 11 | 工作区token开销 | 🟡 中 | 基线占用上下文窗口超过15% |
| 12 | 任务漂移 | 🟡 中 | 压缩后目录偏离或10次以上读取无编辑 |
| 13 | 无边界遍历 | 🟠 高 | 重复的无范围find/grep -r导致输出泛滥 |
| 14 | 工具误用 | 🟡 中 | 同一文件读取3次以上无编辑,或重复相同搜索 |
自我改进代理集成
要启用将发现写入.learnings/LEARNINGS.md,请在运行prescribe前设置CLAWDOC_LEARNINGS=1:
CLAWDOC_LEARNINGS=1 bash {baseDir}/scripts/diagnose.sh | bash {baseDir}/scripts/prescribe.sh
提示
- - 会话JSONL文件是所有诊断的事实依据
- 使用jq -s(slurp)聚合会话文件中的所有行
- 通过type==text过滤message.content[]获取可读内容,通过type==toolCall获取工具调用
- 在给出配置变更建议时,始终显示确切的JSON路径和值