← 返回
沟通协作 中文

Session Health Monitor

Context window health monitoring for OpenClaw agents — threshold warnings via Telegram, pre-compaction snapshots, and memory rotation.
OpenClaw 代理的上下文窗口健康监控 — 通过 Telegram 发送阈值警告、预压缩快照和内存轮换
assistantheinrich-prog
沟通协作 clawhub v1.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 850
下载
💾 10
安装
1
版本
#latest

概述

Session Health Monitor

Monitor your OpenClaw agent context window health, get warnings via Telegram when usage is high, save critical facts before compaction, and keep memory directories clean.

Overview

Four capabilities for OpenClaw agent sessions:

  1. Context Threshold Warnings — Agents append usage footer to Telegram messages and warn at configurable thresholds
  2. Compaction Detection — Track usage drops to infer when context was compacted
  3. Pre-Compaction Snapshots — Save key facts and decisions to daily memory files before they're lost
  4. Memory Rotation — Archive old daily memory files to prevent clutter

Quick Setup (OpenClaw)

1. Add shared skill reference

Add to your shared/INDEX.md:

| Context window health, compaction detection, pre-compaction snapshots | `skill-session-health.md` |

2. Create shared skill doc

Create shared/skill-session-health.md:

# Session Health Monitor

## Context Health Thresholds
| Level  | Condition                          | Action                        |
|--------|------------------------------------|-------------------------------|
| GREEN  | <50% used AND 0 compactions        | Normal operation              |
| YELLOW | >=50% used OR >=1 compaction       | Save key facts via snapshot   |
| RED    | >=75% used OR >=2 compactions      | Save facts NOW, session ending|

## Behavioral Rules
1. When context reaches YELLOW+, extract 3-5 key facts (decisions, files changed, blockers)
2. Run: `bash scripts/snapshot.sh "fact1" "fact2"`
3. Append footer to Telegram messages at YELLOW+: `X% Context Window | Nx compacted`
4. Do this BEFORE session ends or context gets compacted
5. After any detected compaction, immediately snapshot what you remember

3. Add heartbeat step

Add to your agent heartbeat/loop:

**Context health check**: Run `session_status` → always append context % to Telegram messages
as footer: `📊 X% Context Window`. If Context >50% OR Compactions >=1, add:
"⚠️ consider /restart after current task." If Context >75% OR Compactions >=2, flag as urgent.

Context Health Thresholds

LevelConditionAction
-------------------------------------------------------------------------------
GREEN<50% used AND 0 compactionsNormal operation
YELLOW>=50% used OR >=1 compactionConsider saving key facts
RED>=75% used OR >=2 compactionsSave facts NOW, session ending

Telegram Message Footer

Agents append a footer to every outgoing Telegram message:

📊 42% Context Window                          # GREEN — no extra warning
📊 63% Context Window | 1x compacted           # YELLOW — consider restart
⚠️ 📊 81% Context Window | 2x compacted        # RED — urgent, save facts

This keeps the user informed about session health without requiring manual checks.

Pre-Compaction Snapshot Protocol

When context reaches YELLOW or above, the agent SHOULD:

  1. Extract 3-5 key facts from the current session (decisions made, files changed, blockers found)
  2. Write them to memory/YYYY-MM-DD.md using scripts/snapshot.sh
  3. Include any unfinished work or next steps
  4. 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

context-check.sh

Standalone health check, useful in heartbeat loops.

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

Configuration

All configuration is via environment variables with sensible defaults:

VariableDefaultDescription
----------------------------------------------------------------------------------------------------------------
MEMORY_DIRAuto-detect (see below)Where to write daily memory files
KEEP_DAYS3Days to keep before archiving
HEALTH_GREEN_MAX50Max % for GREEN status
HEALTH_RED_MIN75Min % for RED status
COMPACTION_DROP30% drop that indicates compaction

Memory directory auto-detection order:

  1. $MEMORY_DIR environment variable
  2. ~/.openclaw/workspace/memory (if exists)
  3. ~/.claude/memory (fallback)

Troubleshooting

jq not installed

# macOS
brew install jq
# Linux
sudo apt-get install jq

Reset compaction state

rm /tmp/session-health-*.json

Agent not appending footer

  1. Check shared/INDEX.md references skill-session-health.md
  2. Check heartbeat includes the context health step
  3. Verify session_status tool is available to the agent

版本历史

共 1 个版本

  • v1.1.0 当前
    2026-03-29 12:20 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

communication-collaboration

Gmail

byungkyu
Gmail API 集成,托管 OAuth,支持读取、发送和管理邮件、线程、标签及草稿,适用于需要与 Gmail 交互的场景。
★ 72 📥 37,788
communication-collaboration

imap-smtp-email

gzlicanyi
使用IMAP/SMTP读取和发送邮件;检查新/未读邮件、获取内容、搜索邮箱、标记已读/未读、发送带附件的邮件。支持...
★ 114 📥 52,566
communication-collaboration

Slack

steipete
当需要通过 slack 工具从 Clawdbot 控制 Slack 时使用,包括在频道或私信中回复消息或置顶/取消置顶项目。
★ 157 📥 47,763