# Knowledge System (QMD-style)

Purpose: fast, structured memory retrieval with nightly consolidation.

## Layers

1. `rules/` — stable operating/security rules (highest priority)
2. `projects/` + `playbooks/` — active execution context
3. `entities/` — durable facts about people/systems
4. `daily/` — chronological working notes
5. `archive/` — cold storage, low-priority retrieval

## Retrieval Priority

`rules -> projects -> entities -> daily -> playbooks -> archive`

Use:

```bash
./scripts/memory/search.sh "your query"
```

Backend behavior:
- If `qmd` is installed, search uses qmd collections first.
- Otherwise it falls back to grep over markdown files.

## Index

Build/update index:

```bash
./scripts/memory/index.sh
```

## Nightly Consolidation

Run manually:

```bash
./scripts/memory/consolidate.sh
```

What it now does:
- Ensures today's `knowledge/daily/YYYY-MM-DD.md` note exists
- Generates **Candidate Promotions (auto)** from `memory/today + memory/yesterday`
- Includes open items from `knowledge/rules/corrections-log.md`
- Rebuilds index (`scripts/memory/index.sh`)
- Runs `scripts/memory/critical-facts-check.py` and appends results under **Critical Facts Retrieval Check (auto)** in daily notes
- Exits non-zero if critical facts checks fail (so cron can alert)

Cron example (2:00 AM local):

```cron
0 2 * * * cd /Users/openclaw/.openclaw/workspace && ./scripts/memory/consolidate.sh >> /tmp/openclaw/memory-consolidate.log 2>&1
```

## Guardrails added
- `knowledge/rules/channel-routing.md` — canonical Telegram thread routing policy + pre-send checklist
- `knowledge/rules/corrections-log.md` — high-impact correction tracker
- `knowledge/playbooks/memory-promotion-rubric.md` — promotion decision rubric
