|
| 1 | +--- |
| 2 | +name: compaction-resilience-guard |
| 3 | +version: "1.0" |
| 4 | +category: openclaw-native |
| 5 | +description: Monitors memory compaction for failures and enforces a three-level fallback chain — normal, aggressive, deterministic truncation — ensuring compaction always makes forward progress. |
| 6 | +stateful: true |
| 7 | +--- |
| 8 | + |
| 9 | +# Compaction Resilience Guard |
| 10 | + |
| 11 | +## What it does |
| 12 | + |
| 13 | +Memory compaction can fail silently: the LLM produces empty output, summaries that are *larger* than their input, or garbled text. When this happens, compaction stalls and context overflows. |
| 14 | + |
| 15 | +Compaction Resilience Guard enforces a three-level escalation chain inspired by [lossless-claw](https://github.com/Martian-Engineering/lossless-claw): |
| 16 | + |
| 17 | +| Level | Strategy | When used | |
| 18 | +|---|---|---| |
| 19 | +| L1 — Normal | Standard summarization prompt | First attempt | |
| 20 | +| L2 — Aggressive | Low temperature, reduced reasoning, shorter output target | After L1 failure | |
| 21 | +| L3 — Deterministic | Pure truncation: keep first N + last N lines, drop middle | After L2 failure | |
| 22 | + |
| 23 | +This ensures compaction **always makes progress** — even if the LLM is broken. |
| 24 | + |
| 25 | +## When to invoke |
| 26 | + |
| 27 | +- After any compaction event — validate the output |
| 28 | +- When context usage approaches 90% — compaction may be failing |
| 29 | +- When summaries seem unusually long or empty — detect inflation |
| 30 | +- As a pre-check before memory-dag-compactor runs |
| 31 | + |
| 32 | +## How to use |
| 33 | + |
| 34 | +```bash |
| 35 | +python3 guard.py --check # Validate recent compaction outputs |
| 36 | +python3 guard.py --check --file <summary.yaml> # Check a specific summary file |
| 37 | +python3 guard.py --simulate <text> # Run the 3-level chain on sample text |
| 38 | +python3 guard.py --report # Show failure/escalation history |
| 39 | +python3 guard.py --status # Last check summary |
| 40 | +python3 guard.py --format json # Machine-readable output |
| 41 | +``` |
| 42 | + |
| 43 | +## Failure detection |
| 44 | + |
| 45 | +The guard detects these compaction failures: |
| 46 | + |
| 47 | +| Failure | How detected | Action | |
| 48 | +|---|---|---| |
| 49 | +| Empty output | Summary length < 10 chars | Escalate to next level | |
| 50 | +| Inflation | Summary tokens > input tokens | Escalate to next level | |
| 51 | +| Garbled text | Entropy score > 5.0 (random chars) | Escalate to next level | |
| 52 | +| Repetition | Same 20+ char phrase repeated 3+ times | Escalate to next level | |
| 53 | +| Truncation marker | Contains `[FALLBACK]` or `[TRUNCATED]` | Record as L3 usage | |
| 54 | +| Stale | Summary unchanged from previous run | Flag for review | |
| 55 | + |
| 56 | +## Procedure |
| 57 | + |
| 58 | +**Step 1 — Check recent compaction outputs** |
| 59 | + |
| 60 | +```bash |
| 61 | +python3 guard.py --check |
| 62 | +``` |
| 63 | + |
| 64 | +Validates all summary nodes in memory-dag-compactor state. Reports failures by level and whether escalation was needed. |
| 65 | + |
| 66 | +**Step 2 — Simulate the fallback chain** |
| 67 | + |
| 68 | +```bash |
| 69 | +python3 guard.py --simulate "$(cat long-text.txt)" |
| 70 | +``` |
| 71 | + |
| 72 | +Runs the 3-level chain on sample text to test that each level produces valid output. |
| 73 | + |
| 74 | +**Step 3 — Review escalation history** |
| 75 | + |
| 76 | +```bash |
| 77 | +python3 guard.py --report |
| 78 | +``` |
| 79 | + |
| 80 | +Shows how often each level was used. High L2/L3 usage indicates the primary summarization prompt needs improvement. |
| 81 | + |
| 82 | +## State |
| 83 | + |
| 84 | +Failure counts, escalation history, and per-summary validation results stored in `~/.openclaw/skill-state/compaction-resilience-guard/state.yaml`. |
| 85 | + |
| 86 | +Fields: `last_check_at`, `level_usage`, `failures`, `check_history`. |
| 87 | + |
| 88 | +## Notes |
| 89 | + |
| 90 | +- Read-only monitoring — does not perform compaction itself |
| 91 | +- Works alongside memory-dag-compactor as a quality gate |
| 92 | +- Deterministic truncation (L3) preserves first 30% and last 20% of input, drops middle |
| 93 | +- Entropy is measured using Shannon entropy on character distribution |
| 94 | +- High L3 usage (>10% of compactions) suggests a systemic LLM issue |
0 commit comments