Context Cleaner Skill
Automatically manage session context size by compressing large contexts and cleaning up expired sessions, sub-agents, and temporary files.
Quick Reference
| Situation | Action |
|---|
| Session exceeds size limit | Compress early messages |
| Sub-agent idle > 30min | Terminate automatically |
| Old sessions (> 7 days) | Archive or delete |
| Large context (> 10K tokens) | Summarize and compress |
Setup
No special setup required. The skill automatically:
- Monitors session sizes - Checks if current session context is growing too large
- Terminates idle sub-agents - Stops sub-agents that haven't been used recently
- Archives old sessions - Moves completed sessions to archive
- Compresses context - Replaces verbose message chains with summaries
Usage
Automatic Operation
The skill runs automatically during heartbeat checks when context size exceeds thresholds.
Manual Trigger
Request cleanup anytime:
"Clean up context now"
"Remove expired sessions"
"Compress my context"
API Call
Use sessions_list and subagents tools internally. The skill orchestrates:
- List all sessions
- Check sub-agent status
- Terminate idle sub-agents
- Compress large contexts
- Archive old sessions
Operation Logic
Context Compression
When a session's context exceeds thresholds:
- Identify compressible regions - Early messages, completed task steps
- Generate summaries - Create concise summaries of message chains
- Replace verbose content - Keep only essential information
- Preserve key context - Maintain user preferences, decisions, ongoing tasks
Session Cleanup
Expired session criteria:
- Idle time: No activity for 7+ days
- Completed tasks: Sessions that finished long ago
- Orphaned sessions: No associated work
Sub-Agent Management
Termination criteria:
- Idle: No messages sent/received for 30+ minutes
- Failed tasks: Sub-agents that encountered repeated errors
- Manual request: User explicitly requests cleanup
Best Practices
- Preserve working sessions - Never clean active sessions
- Keep recent history - Maintain last 24-48 hours of messages
- Archive not delete - Move to archive before deleting
- Respect user context - Don't clean sessions with pending tasks
- Notify before major cleanup - Warn before deleting important sessions
Integration Points
Heartbeat Integration
Add to HEARTBEAT.md:
## 上下文管理
- [ ] 检查会话大小
- [ ] 清理过期子代理
- [ ] 压缩过大上下文
Cron Integration
Schedule regular cleanup:
# Weekly full cleanup
cron add -name "weekly-context-clean" -schedule "0 3 * * 0" -payload "Compress contexts and clean expired sessions"
Compression Strategies
Message Chaining
Convert:
User: Ask question
Model: Answer with detailed explanation
User: Follow-up
Model: More details
To:
User: Ask question
Model: ✅ Answered with 4-point explanation
User: Follow-up
Model: Continued discussion
Context Summarization
Summarize long conversation threads:
- Key decisions: User preferences, important conclusions
- Task progress: Completed items, pending actions
- Context notes: Relevant background information
Session Archiving
Archive strategy:
- Check completion status - Is session finished?
- Verify no active work - No pending tasks
- Check age - Created more than 7 days ago?
- Move to archive - Session archive directory
- Update metadata - Record completion details
File Structure
workspace/
├── .archive/ # Archived sessions
│ ├── 2026-04/
│ │ ├── session-uuid-1/
│ │ └── session-uuid-2/
│ └── 2026-05/
├── .cache/ # Temporary compression data
└── .cleanup/ # Cleanup logs and reports
└── cleanup-report.md
Safety Checks
Before any cleanup operation:
- Verify session status - Not actively being used
- Check pending tasks - No incomplete operations
- User preferences - Respect "keep forever" flags
- Recent activity - Last message within threshold
- Archive option - Offer archive before delete
Example Outputs
Cleanup Report
## Context Cleanup Report
**Time**: 2026-04-17 10:43
**Operations**:
- ✅ Compressed 3 large contexts (saved 15K tokens)
- ✅ Terminated 2 idle sub-agents
- ✅ Archived 1 old session (30 days)
- ⚠️ Skipped 1 active session (pending task)
**Impact**:
- Context size: Reduced by 25%
- Free tokens: ~50K available
- Active sessions: 5 (unchanged)
Related Skills
- self-improvement: Log learnings about context management
- taskflow: Manage multi-step cleanup tasks
- memory: Keep important context in long-term memory
Note: This skill works alongside existing OpenClaw tools without requiring special permissions.