Session Health Monitor
简介
Session Health Monitor
原始描述
Context window health monitoring for OpenClaw agents — threshold warnings via Telegram, pre-compaction snapshots, and memory rotation.
通过你的 Agent 添加
安装目录:session-health-monitor/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: session-health-monitor description: Context window health monitoring with compaction detection, pre-compaction snapshots, and memory rotation for Claude Code sessions. allowed-tools:
- Bash
- Read
- Write version: 1.0.0 author: heinrichclawdster
Session Health Monitor
Monitor your Claude Code context window health, detect compactions, save critical facts before they're lost, and keep your memory directory clean.
Overview
Four capabilities, fully standalone (no OpenClaw required):
- StatusLine Display — Color-coded context window usage in your Claude Code status bar
- Compaction Detection — Infers when context was compacted by tracking usage drops
- Pre-Compaction Snapshots — Save key facts and decisions to daily memory files
- Memory Rotation — Archive old daily memory files to prevent clutter
Quick Setup
bash scripts/setup-statusline.sh
That's it. Restart Claude Code and the statusline appears.
For snapshot and rotation, the scripts work standalone — call them from your agent loops or manually.
Context Health Thresholds
| Level | Condition | Action |
|---|---|---|
| GREEN | <50% used AND 0 compactions | Normal operation |
| YELLOW | >=50% used OR >=1 compaction | Consider saving key facts |
| RED | >=75% used OR >=2 compactions | Save facts NOW, session ending |
StatusLine Display
The statusline shows a color-coded indicator:
42% Context | 0x compact # GREEN — all good
63% Context | 1x compact # YELLOW — getting warm
81% Context | 2x compact # RED — save facts immediately
Colors use ANSI codes compatible with Claude Code's terminal rendering.
Pre-Compaction Snapshot Protocol
When context reaches YELLOW or above, the agent SHOULD:
- Extract 3-5 key facts from the current session (decisions made, files changed, blockers found)
- Write them to
memory/YYYY-MM-DD.mdusingscripts/snapshot.sh - Include any unfinished work or next steps
- Do this BEFORE the session ends or context is compacted
Example snapshot content:
## Pre-Compaction Snapshot (14:32)
- Refactored auth module to use JWT instead of sessions (files: src/auth.ts, src/middleware.ts)
- Bug found in rate limiter: counter resets on deploy, not on TTL expiry
- Next: write tests for new auth flow, fix rate limiter reset logic
- Decision: using RS256 for JWT signing (user preference)
When to trigger:
- Context hits 50%+ for the first time in a session
- After any detected compaction
- Before ending a long session
- When the agent detects it has accumulated significant context
Scripts Reference
statusline.sh
Reads Claude Code statusline JSON from stdin and outputs formatted context info.
# Called automatically by Claude Code via settings.local.json
# Manual test:
echo '{"context_window":{"used_percentage":42},"session_id":"test-123"}' | bash scripts/statusline.sh
setup-statusline.sh
One-command installer. Copies the statusline script and patches Claude Code settings.
bash scripts/setup-statusline.sh
# Backs up settings.local.json before patching
# Requires: jq
context-check.sh
Standalone health check, useful in heartbeat loops or CI.
bash scripts/context-check.sh # Human-readable output
bash scripts/context-check.sh --json # Machine-readable JSON
echo '{"context_window":{"used_percentage":72}}' | bash scripts/context-check.sh
# Exit codes: 0=GREEN, 1=YELLOW, 2=RED
snapshot.sh
Save facts to daily memory file.
bash scripts/snapshot.sh "Fact one" "Fact two" "Fact three"
echo -e "Fact one\nFact two" | bash scripts/snapshot.sh -
rotate.sh
Archive old daily memory files.
bash scripts/rotate.sh # Archives files older than 3 days (default)
KEEP_DAYS=7 bash scripts/rotate.sh # Keep 7 days instead
Messaging Footer
When context is at YELLOW or above, agents should append a footer to outgoing messages:
---
63% Context Window | 1x compacted
This helps the user understand when a session is running long and may need a fresh start.
Configuration
All configuration is via environment variables with sensible defaults:
| Variable | Default | Description |
|---|---|---|
MEMORY_DIR |
Auto-detect (see below) | Where to write daily memory files |
KEEP_DAYS |
3 |
Days to keep before archiving |
HEALTH_GREEN_MAX |
50 |
Max % for GREEN status |
HEALTH_RED_MIN |
75 |
Min % for RED status |
COMPACTION_DROP |
30 |
% drop that indicates compaction |
Memory directory auto-detection order:
$MEMORY_DIRenvironment variable~/.openclaw/workspace/memory(if exists)~/.claude/memory(fallback)
Troubleshooting
jq not installed
# macOS
brew install jq
# Linux
sudo apt-get install jq
Statusline not updating
- Check the script is at
~/.claude/session-health-statusline.sh - Check
settings.local.jsonhas thestatusLinekey - Restart Claude Code
- Test manually:
echo '{}' | bash ~/.claude/session-health-statusline.sh
Reset compaction state
rm /tmp/session-health-*.json
Statusline shows "Context Window" with no percentage
Normal on first run — the statusline needs one data point from Claude Code before it can display usage. It will update on the next tick.
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。