Skill: 0range-x/session-rotate-80 (version 1.0.1)

--- name: session-rotate-80 description: Auto-create a new session when OpenClaw context usage reaches 80% without requiring Mem0 or file memory systems. Use when users want default OpenClaw to proactively rotate sessions and avoid context overflow in long chats. --- # Session Rotate 80 ## Overview Trigger a standard `[NEW_SESSION]` message when context usage reaches 80%. This skill is memory-system-agnostic and works in plain default OpenClaw setups. ## Workflow 1. Read current context usage from runtime status. 2. Run `scripts/context_guard.py <used_tokens> <max_tokens>`. 3. If threshold reached, output the new-session trigger and handoff hint. 4. Keep old session only for short handoff, then continue in new session. ## Command ```bash python scripts/context_guard.py <used_tokens> <max_tokens> --threshold 0.8 --channel boss ``` Example: ```bash python scripts/context_guard.py 220000 272000 --threshold 0.8 --channel boss ``` ## Expected Output At or above threshold: - `[ROTATE_NEEDED]` - `[NEW_SESSION] 上下文达到80%（used/max），自动切换新会话` - `[HANDOFF_HINT] ...` Below threshold: - `[ROTATE_NOT_NEEDED] ratio=x.xx < 0.800` ## Integration Hint (Heartbeat) In heartbeat flow, after reading context usage: 1. Call `context_guard.py`. 2. If `[ROTATE_NEEDED]`, emit `[NEW_SESSION]...` directly. 3. Stop handling new tasks in old session except handoff confirmation. ## scripts/ - `scripts/context_guard.py`: threshold detector and trigger emitter (no memory dependency).

path: README.md
content: # Session Rotate 80

不依赖记忆系统的 OpenClaw 会话轮换技能。

当上下文占用率达到 80% 时，自动输出新会话触发指令，避免长对话把上下文顶满。

## 特性

- ✅ 默认 OpenClaw 可用（无 Mem0、无文件记忆也能工作）
- ✅ 上下文占用率阈值触发（默认 80%）
- ✅ 标准化新会话触发消息（`[NEW_SESSION] ...`）
- ✅ 提供最小交接提示，减少会话切换后的信息断层

## 适用场景

- 你只想要“到 80% 就切新会话”的硬规则
- 你不准备搭建完整记忆系统
- 你在 Discord 长时间连续对话，想避免上下文溢出降质

## 快速开始

### 安装

```bash
clawhub install session-rotate-80
```

或手动：

```bash
git clone https://github.com/0range-x/session-rotate-80.git
```

### 运行

```bash
python scripts/context_guard.py <used_tokens> <max_tokens> --threshold 0.8 --channel boss
```

示例：

```bash
python scripts/context_guard.py 220000 272000 --threshold 0.8 --channel boss
```

## 输出说明

### 触发时

- `[ROTATE_NEEDED]`
- `[NEW_SESSION] 上下文达到80%（used/max），自动切换新会话`
- `[HANDOFF_HINT] 在旧会话保留3行交接：当前目标、已完成、下一步`

### 未触发时

- `[ROTATE_NOT_NEEDED] ratio=x.xx < 0.800`

## 接入建议（Heartbeat）

在你的心跳或状态检查流程里：

1. 读取当前上下文占用（used/max）
2. 调用 `context_guard.py`
3. 命中后直接发送 `[NEW_SESSION] ...`
4. 旧会话只做交接，不再承载新任务

## 与记忆系统关系

- 本技能可以独立工作
- 即使无记忆系统，也能稳定执行“80%自动切换”
- 若后续接入记忆系统，可在触发前加“自动交接摘要写入”获得更好连续性

## 实用指南

推荐搭配规则：

1. 阈值固定 80%
2. 切换后第一条消息粘贴交接三行
3. 单会话尽量只做一个主题
4. 2到3天主动切一次会话，即使没到 80%

## 作者

小橘（vulcanx_14970）

Twitter: [@maru_49940](https://x.com/maru_49940)

## 许可证

MIT


path: scripts/context_guard.py
content: #!/usr/bin/env python3
"""
context_guard.py

Default OpenClaw compatible session rotate trigger.
No memory system required.

Behavior:
- When used/max >= threshold (default 0.8), print a NEW_SESSION trigger line.
- By default, no cooldown, so every qualifying check can trigger.
"""

from __future__ import annotations

import argparse
import sys


def parse_args() -> argparse.Namespace:
    p = argparse.ArgumentParser(description="Trigger new session when context usage reaches threshold")
    p.add_argument("used_tokens", type=int, help="Current context used tokens")
    p.add_argument("max_tokens", type=int, help="Current context max tokens")
    p.add_argument("--threshold", type=float, default=0.8, help="Trigger threshold ratio, default 0.8")
    p.add_argument("--channel", default="boss", help="Channel label for message text")
    return p.parse_args()


def main() -> int:
    args = parse_args()

    if args.max_tokens <= 0:
        print("[ROTATE_ERROR] invalid max_tokens")
        return 1

    ratio = args.used_tokens / args.max_tokens
    if ratio < args.threshold:
        print(f"[ROTATE_NOT_NEEDED] ratio={ratio:.3f} < {args.threshold:.3f}")
        return 0

    pct = int(round(args.threshold * 100))
    print("[ROTATE_NEEDED]")
    print(f"[NEW_SESSION] 上下文达到{pct}%（{args.used_tokens}/{args.max_tokens}），自动切换新会话")
    print("[HANDOFF_HINT] 在旧会话保留3行交接：当前目标、已完成、下一步")
    return 0


if __name__ == "__main__":
    sys.exit(main())


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector:  ()