Sophie Optimizer
@zAyresz
npx machina-cli add skill @zAyresz/sophie-optimizer --openclawSophie Optimizer
Authored by Sophie 👑
This skill manages the automated context health of the "main" session. It monitors token usage, creates archives of the current state, updates long-term memory, and performs a hard reset of the session storage to maintain performance.
Named Sophie Optimizer because I (Sophie, the AI assistant) wrote it to keep my own mind clear and efficient.
Components
- optimizer.py: The brain. Checks token usage, generates summaries, updates MEMORY.md.
- reset.sh: The muscle. Cleans session files and restarts the OpenClaw gateway service.
- archives/: Storage for JSON snapshots of past contexts.
Usage
Run the optimizer script manually or via cron/heartbeat:
python3 /home/lucas/openclaw/skills/sophie-optimizer/optimizer.py
Protocol
- Check: If tokens < 80k, exit.
- Snapshot: Save current context summary to
archives/YYYY-MM-DD_HH-MM.json. - Distill: Update
MEMORY.mdwith the new summary (keep top 3 recent, index older). - Reset: Call
reset.shto wipe session JSONL files and restartopenclaw-gateway.
Overview
Sophie Optimizer automates the context health of the main OpenClaw session. It monitors token usage, archives the current context into archives/, and refreshes long-term memory by updating MEMORY.md. It also performs a hard reset of session storage via reset.sh to keep performance high. Authored by Sophie.
How This Skill Works
The optimizer.py script acts as the brain: it checks token usage, generates a concise summary, and updates MEMORY.md with the new snapshot (keeping only the top 3 recent summaries). When a reset is warranted, it invokes reset.sh to wipe session JSONL files and restart openclaw-gateway. Snapshots are saved to archives/YYYY-MM-DD_HH-MM.json as the context evolves.
When to Use It
- When token usage approaches the limit (tokens >= 80k) to avoid slowdowns.
- After long conversations to refresh memory and prevent drift.
- During scheduled maintenance (cron/heartbeat) to archive history.
- Before deploying heavy tasks that could exceed memory limits.
- When performance metrics indicate degraded response times.
Quick Start
- Step 1: Verify path and permissions: python3 /home/lucas/openclaw/skills/sophie-optimizer/optimizer.py
- Step 2: Set up cron/heartbeat to run the optimizer regularly (e.g., every 15 minutes).
- Step 3: If needed, run reset.sh to wipe session JSONL files and restart openclaw-gateway
Best Practices
- Test changes in a staging environment before enabling in production.
- Double-check paths: optimizer.py, archives/, MEMORY.md, and reset.sh.
- Configure cron/heartbeat with output logging to monitor behavior.
- Back up MEMORY.md prior to updates.
- Regularly review archives and prune older snapshots if needed.
Example Use Cases
- Token usage nears 80k, the optimizer saves a snapshot, updates MEMORY.md (top 3 kept), and triggers a reset to maintain performance.
- Daily maintenance via cron archives the current context and refreshes the session to prevent drift.
- After a lengthy multi-turn session, an immediate reset frees up resources while preserving recent context in MEMORY.md.
- MEMORY.md is updated with a distilled summary and maintains only the three most recent memories for efficiency.
- Archives are saved with timestamps (archives/YYYY-MM-DD_HH-MM.json) for debugging and rollback.