Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions skills/openclaw-native/memory-integrity-checker/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
name: memory-integrity-checker
version: "1.0"
category: openclaw-native
description: Validates memory summary DAGs for structural integrity — detects orphan nodes, circular references, token inflation, broken lineage, and stale summaries that corrupt the agent's memory.
stateful: true
cron: "0 3 * * 0"
---

# Memory Integrity Checker

## What it does

As memory DAGs grow through compaction, they can develop structural problems: orphan nodes with no parent, circular reference loops, summaries that inflated instead of compressing, broken lineage chains, and stale nodes that should have been dissolved. These problems silently corrupt the agent's memory.

Memory Integrity Checker runs 8 structural checks on the DAG, generates a repair plan, and optionally auto-fixes safe issues.

Inspired by [lossless-claw](https://github.com/Martian-Engineering/lossless-claw)'s DAG integrity checking system, which detects and repairs corrupted summaries.

## When to invoke

- Automatically Sundays at 3am (cron) — weekly structural audit
- After a crash or unexpected shutdown — check for corruption
- When the agent's memory seems inconsistent — diagnose structural issues
- Before a major compaction or prune operation — ensure clean starting state

## Integrity checks (8 total)

| Check | What it detects | Severity |
|---|---|---|
| ORPHAN_NODE | Node with no parent and not a root | HIGH |
| CIRCULAR_REF | Circular parent-child loops in the DAG | CRITICAL |
| TOKEN_INFLATION | Summary has more tokens than its combined children | HIGH |
| BROKEN_LINEAGE | Edge references a node ID that doesn't exist | CRITICAL |
| STALE_ACTIVE | Active node older than 30 days with no children | MEDIUM |
| EMPTY_NODE | Node with empty or whitespace-only content | HIGH |
| DUPLICATE_EDGE | Same parent-child edge appears multiple times | LOW |
| DEPTH_MISMATCH | Node's depth doesn't match its position in the DAG | MEDIUM |

## How to use

```bash
python3 integrity.py --check # Run all 8 integrity checks
python3 integrity.py --check --fix # Auto-fix safe issues
python3 integrity.py --check --only ORPHAN_NODE # Run a specific check
python3 integrity.py --repair-plan # Generate repair plan without fixing
python3 integrity.py --status # Last check summary
python3 integrity.py --format json # Machine-readable output
```

## Procedure

**Step 1 — Run integrity checks**

```bash
python3 integrity.py --check
```

Runs all 8 checks and reports findings by severity.

**Step 2 — Review repair plan**

```bash
python3 integrity.py --repair-plan
```

For each finding, shows what the auto-fix would do:
- ORPHAN_NODE → reattach to nearest active root or deactivate
- DUPLICATE_EDGE → remove duplicates
- EMPTY_NODE → deactivate
- STALE_ACTIVE → deactivate

**Step 3 — Apply safe fixes**

```bash
python3 integrity.py --check --fix
```

Auto-fixes LOW and MEDIUM severity issues. HIGH and CRITICAL require manual review.

## State

Check results, finding history, and repair actions stored in `~/.openclaw/skill-state/memory-integrity-checker/state.yaml`.

Fields: `last_check_at`, `findings`, `check_history`, `repairs_applied`.

## Notes

- Reads from memory-dag-compactor's state file — does not maintain its own DAG
- Auto-fix only applies to LOW and MEDIUM severity issues
- CRITICAL issues (circular refs, broken lineage) require manual intervention
- Circular reference detection uses DFS with visited-set tracking
- Token inflation check compares parent tokens vs. sum of children tokens
33 changes: 33 additions & 0 deletions skills/openclaw-native/memory-integrity-checker/STATE_SCHEMA.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "1.0"
description: DAG integrity check results, findings, and repair history.
fields:
last_check_at:
type: datetime
findings:
type: list
description: Integrity issues found in the most recent check
items:
check: { type: string, description: "Check name (e.g. ORPHAN_NODE)" }
severity: { type: enum, values: [CRITICAL, HIGH, MEDIUM, LOW] }
node_id: { type: string }
detail: { type: string }
auto_fixable: { type: boolean }
check_history:
type: list
description: Rolling log of past checks (last 20)
items:
checked_at: { type: datetime }
nodes_checked: { type: integer }
findings: { type: integer }
critical: { type: integer }
high: { type: integer }
medium: { type: integer }
low: { type: integer }
repairs_applied:
type: list
description: History of auto-fix actions taken
items:
repaired_at: { type: datetime }
check: { type: string }
node_id: { type: string }
action: { type: string }
71 changes: 71 additions & 0 deletions skills/openclaw-native/memory-integrity-checker/example-state.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Example runtime state for memory-integrity-checker
last_check_at: "2026-03-16T03:00:12.000000"
findings:
- check: ORPHAN_NODE
severity: HIGH
node_id: s-d1-003
detail: "Depth 1 node has no parent — should be connected to a d0 parent"
auto_fixable: true
- check: STALE_ACTIVE
severity: MEDIUM
node_id: s-d0-002
detail: "Active node is 45 days old with no children"
auto_fixable: true
- check: DUPLICATE_EDGE
severity: LOW
node_id: "s-d2-001->s-d1-000"
detail: "Duplicate edge in DAG"
auto_fixable: true
check_history:
- checked_at: "2026-03-16T03:00:12.000000"
nodes_checked: 24
findings: 3
critical: 0
high: 1
medium: 1
low: 1
- checked_at: "2026-03-09T03:00:10.000000"
nodes_checked: 18
findings: 0
critical: 0
high: 0
medium: 0
low: 0
repairs_applied:
- repaired_at: "2026-03-16T03:00:12.000000"
check: DUPLICATE_EDGE
node_id: "s-d2-001->s-d1-000"
action: "Removed duplicate edges"
- repaired_at: "2026-03-16T03:00:12.000000"
check: STALE_ACTIVE
node_id: s-d0-002
action: "Deactivated stale node s-d0-002"
# ── Walkthrough ──────────────────────────────────────────────────────────────
# Cron runs Sundays at 3am: python3 integrity.py --check --fix
#
# Memory Integrity Check — 2026-03-16 03:00
# ───────────────────────────────────────────────────────
# Nodes checked: 24 | Edges: 18
# Findings: 3 (0 critical, 1 high, 1 medium, 1 low)
#
# ! [ HIGH] ORPHAN_NODE: s-d1-003
# Depth 1 node has no parent [auto-fixable]
# ~ [ MEDIUM] STALE_ACTIVE: s-d0-002
# Active node is 45 days old with no children [auto-fixable]
# . [ LOW] DUPLICATE_EDGE: s-d2-001->s-d1-000
# Duplicate edge in DAG [auto-fixable]
#
# Repairs applied: 2
# + Removed duplicate edges
# + Deactivated stale node s-d0-002
#
# Status: DEGRADED
#
# python3 integrity.py --repair-plan
#
# Repair Plan — 3 findings
# ───────────────────────────────────────────────────────
# Auto-fixable (3):
# ORPHAN_NODE on s-d1-003: Depth 1 node has no parent
# STALE_ACTIVE on s-d0-002: Active node is 45 days old
# DUPLICATE_EDGE on s-d2-001->s-d1-000: Duplicate edge
Loading
Loading