Tools
Memory Sag
OpenClaw memory-sag plugin: evidence-backed long-term memory with shared Library, WASM jieba and SAG2 retrieval
Install
npm install
npm
README
# openclaw-memory-sag
Evidence-backed structured memory (`Vault`) and user-level shared external document knowledge base (`Library`) plugin for OpenClaw (`>= 2026.12.1`, Node `>= 22`). Zero native C/C++/Rust build dependencies — runs cleanly on Windows and Linux via `node:sqlite`, `node:worker_threads`, and WASM `jieba-wasm`.
## Key Features
- **Dual-Layer Storage**:
- **Structured Vault (`vault/`)**: Partitioned (`private` / `conversation`) Markdown Pages, Claims, and Relationships with 5-stage atomic publication, Local Git outbox, and reversible/tombstone lifecycle (`correct` / `withdraw` / supersede / merge).
- **Shared External Library (`library/`)**: User-level pool supporting `<= 48 KiB` chunked uploads and read-only `mountedRoots` for PDF, DOCX, PPTX, XLSX, EPUB, Markdown, HTML, TXT, JSON, YAML, and CSV, with Store/Agent ACL filtering.
- **SAG2 Multi-Hop Hybrid Retrieval (`Fast` / `Deep` / `Match`)**:
- Pure WASM `jieba` Chinese segmentation + English Porter stemming + isolated per-partition FTS5 (`temp.sag_query_fts_v1`).
- Unified Scorer supporting OpenAI/TEI/vLLM/Infinity `/v1/rerank` (`BAAI/bge-reranker-v2-m3`, `Alibaba-NLP/gte-reranker-modernbert-base`), Zleap `jev-http` (`/v1/systemone`), host `api.runtime.decisions`, and automatic `bm25-only` graceful degradation.
- **5-State Evidence Anchors**:
- Tracks cited Vault & Library spans across `intact`, `moved`, `changed`, `missing`, and `unverified`, preserving static quotes and warning badges when external files move or disappear.
- **39 Agent Tools**:
- 20 Phase 1 tools for retrieval, the shared Library, writes, evidence, and lifecycle audit; 19 Phase 2 tools for Explore, chronicle, lifecycle/structured operations, and Scribes conversations.
- **Native Bilingual Control UI (`zh-CN` / `en`)**:
- Read-only Vault inspector (all mutations route safely via Ask Agent), Library drag-drop upload + segmented preview + one-click **Crystallize to Knowledge Page**, Search trace explorer, local Explore graph, Scribes cards, live Settings editor (API keys masked write-only), Maintenance panel, and Session Activity sidebar.
## Phase 2 (v0.2.x)
Phase 2 aligns the plugin with much of the OpenClaw `knowledge` surface and adds a local Explore graph. No new dependencies were introduced.
- **Explore graph** — `sag_explore` returns similar Vault/Library documents with shared terms; `sag_explore_evidence` returns the co-occurring source passages for a selected graph edge. The Control UI renders a lightweight native Canvas force-directed graph (top 100 nodes on first load, no pagination).
- **Chronicle & temporal index** — `sag_timeline`, `sag_chronicle_day`, `sag_chronicle_since`, and `sag_chronicle_last_seen` group records by `occurred_at` / `recorded_at` with day boundaries and timezone handling.
- **Lifecycle & structured operations** — `sag_lifecycle` (`supersede` / `merge` / `unmerge` / `recall` / `verify`), `sag_create_claim`, `sag_create_relationship`, `sag_annotate`, `sag_extract_section`, `sag_publish_batch`, `sag_ontology`, `sag_relink`, `sag_reconcile_evidence`, and `sag_source_check`.
- **Scribes automation** — six Scribe definitions (Consolidation, Conversations, Memory, Mail, Slack, Granola) plus `sag_scribe_status`, `sag_scribe_plan_conversations`, and `sag_scribe_finish_conversation` for server-side conversation ingestion. External channels are capability-gated by the availability of host `sessions.*` / `cron.*` RPC and the corresponding plugins.
- **Nine Skills** — `sag-recall`, `sag-capture`, `sag-ingest`, `sag-entities`, `sag-meeting-ingest`, `sag-source-ingest`, `sag-claim-consolidation`, `sag-correct`, and `sag-automation`.
- **Nightly zero-LLM maintenance** — the orchestrator runs on `maintenance.schedule` (default `30 3 * * *`): Library rescan, Vault `relink`, anchor reconciliation, chronicle index calibration, and Explore summary refresh. `maintenance_status` / `maintenance_run` are Control UI operations, not Agent tools.
### Control UI pages
`Recent` · `Browse` (lifecycle / merged / superseded badges and `via` chains) · `Library` · `Search` · `Explore` (graph + list + evidence drawer) · `Scribes` (cards, start/pause/run-now/repair, run history) · `Settings` (Stores, Scoring & OCR, Vocabulary, Vault & Library status, Maintenance) · `Record/File Detail` (evidence anchors, ontology types, and entity timeline) · `Session Activity` sidebar.
> Phase 3 scope only (not in v0.2.0): Document Fold layered reading, OCR/VLM scanned-PDF enhancement, online-source Scribes (Notion/GitHub/RSS/etc.), and full 22-language i18n.
## Installation
```bash
openclaw plugins install openclaw-memory-sag
```
Or from a local checkout:
```powershell
npm install
npm run typecheck
npm run build
npm test
openclaw plugins install .
```
## Configuration (`openclaw.plugin.json` / `plugins.entries.memory-sag.config`)
```json5
{
operatorRead: true,
writesEnabled: true,
stores: {
// eng: { label: "Engineering", agents: ["main", "code_expert"] }
},
scoring: {
provider: "auto", // "auto" | "host-decisions" | "rerank-http" | "jev-http" | "bm25-only"
baseUrl: "http://127.0.0.1:8000",
path: "/v1/rerank",
apiKey: "${RERANK_API_KEY}",
model: "BAAI/bge-reranker-v2-m3",
allowPrivateNetwork: true,
timeoutMs: 15000,
maxConcurrency: 8,
batchTokenBudget: 24000
},
library: {
maxUploadBytes: 52428800,
chunkChars: 512,
overlapChars: 80,
allowedMountBases: [],
mountedRoots: [
// { id: "specs", path: "D:/docs/agv-specs", label: "AGV Specs", stores: [], excludeGlobs: ["**/draft/**"], enabled: true }
]
},
recallGate: {
mode: "off", // "off" | "shadow" | "live"
includeLibrary: false,
timeoutMs: 1500
},
maintenance: {
enabled: true,
schedule: "30 3 * * *",
timezone: "" // empty follows the Gateway host timezone
}
}
```
### Reranker & Zleap Jev Setup
1. **Local / Self-Hosted Reranker (`bge-reranker-v2-m3` / `gte-reranker` via TEI, Infinity, or vLLM)**:
- Set `scoring.provider: "rerank-http"` (or `"auto"`), `scoring.baseUrl: "http://127.0.0.1:8000"`, `scoring.path: "/v1/rerank"`, `scoring.model: "BAAI/bge-reranker-v2-m3"`.
- Use **Settings → Scoring & OCR → Test Connection** in the Control UI to verify connectivity without restarting the Gateway.
2. **Zleap `/v1/systemone` (`jev-http`)**:
- Set `scoring.provider: "jev-http"`, `scoring.baseUrl: "https://..."`, `scoring.path: "/v1/systemone"`, and write-only `scoring.apiKey`.
3. **Host Decisions Fallback**:
- Leave `scoring.baseUrl` empty with `scoring.provider: "auto"` to automatically use OpenClaw `api.runtime.decisions` (concurrency capped at `<= 4`), or fall back to local Chinese BM25 (`degraded: true`) when offline.
## Security Notes
- **SSRF & Secret Safety**: All outbound HTTP requests use DNS-pinning `undici` + `node:net.BlockList` (blocking IPv4/IPv6 private, loopback, CGNAT, link-local, and `::ffff:` mapped-IPv6 addresses when `allowPrivateNetwork: false`, with `<= 3` verified redirects). API keys are write-only and never echoed to Control UI DOM, logs, or URLs.
- **Mount & Workspace Guards**: `mountedRoots` are strictly read-only, verified via `realpath` against symlink escapes, hard-blocked on filesystem roots, home directories, system paths (`/etc`, `/proc`, `/sys`, `C:\Windows`), `$OPENCLAW_STATE_DIR`, `.git/`, `node_modules/`, and secret files (`.env*`, `*.pem`, `*.key`, `id_rsa*`, `credentials.json`, `auth.json`).
- **Control UI Sanitization & Read-Only Vault**: Markdown renders through `Marked` + `DOMPurify` with strict tag/attribute/URL whitelists. Vault records cannot be edited or deleted directly from the browser; all changes go through audited Agent tools via the Ask modal.
## Known Limitations
- `maintenance.lastRun` is kept in memory; there is no catch-up run after a Gateway restart or missed schedule.
- Automatic `relink` is capped at 20 pages per nightly pass.
- `sag_publish_batch` supports creates only; editing existing pages still requires `sag_save_page`.
- `sag_reconcile_evidence` records `expanded` in its report only; it does not physically migrate evidence anchors.
- Scribes depend on host `sessions.*` / `cron.*` RPC; Mail / Slack / Granola also require their external plugins and are shown as unavailable otherwise.
- The Detail timeline is entity-centric and does not provide a global timeline view.
- Explore loads the first 100 graph nodes with no pagination.
## Development & Verification
```powershell
npm run typecheck
npm run build
npm test
npm pack --dry-run
```
The test suite currently contains 35 files / 409 tests.
## License
MIT. See [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md) for license and attribution details, including modules adapted from OpenClaw `@openclaw/knowledge` `2026.12.1` and Zleap-AI `SAG-Wiki`.
tools
Comments
Sign in to leave a comment