Skip to main content
Glama
README.md
<div align="center">

![Conceptual illustration of 大罗洞观 (CanonBase): a dark-obsidian and antique-brass celestial armillary observatory radiating amber and cyan causal filaments above a black reflecting pool.](assets/hero.jpg)

# 大罗洞观 / CanonBase

**SQLite-backed knowledge base, causal impact graph, and FastMCP gateway for multi-agent teams that cannot afford silent overwrites or accidental leaks.**

[简体中文](README.zh-CN.md) · **English** · [Quickstart](docs/QUICKSTART.md) · [Architecture](docs/ARCHITECTURE.md) · [Relay Deploy](docs/DEPLOY-RELAY.md) · [Releases](https://github.com/WeiBeta/CanonBase-public/releases)

![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-6c63ff)
![Python 3.13+](https://img.shields.io/badge/Python-3.13+-14b8a6)
![FastMCP Streamable HTTP](https://img.shields.io/badge/FastMCP-Streamable_HTTP-d9a441)

</div>

## Why this exists

Team knowledge bases rot fast in loose Markdown folders, spreadsheets, and PDFs. One Agent reads too much context and misses a boundary rule; another edits a pricing figure without knowing which clauses or contracts depend on it; a third writes a customer-facing draft that accidentally leaks internal margins or hallucinates a policy it had no permission to read.

**CanonBase (大罗洞观)** turns one local **SQLite (WAL + FTS5)** file per project into a governed, Agent-native **FastMCP Streamable HTTP** engine. It needs no vector database or cloud lock-in, runs natively on Windows/Linux/macOS via Python 3.13 + `uv`, and keeps external asset folders strictly read-only.

## What you get

| Failure mode | What CanonBase provides |
| --- | --- |
| Agents burning context by reading whole folders | Char-budgeted `get_index_brief` + FTS5 with `jieba` search segmentation & custom dictionaries (2-char Chinese terms and hyphenated IDs like `OPS-012` match accurately) |
| Changing one figure and breaking downstream docs | Mandatory `scope` + public `fallback_wording` on `cb_params`, linked via `cb_edges` for upstream/downstream `trace_impact` |
| Mixing internal secrets with public drafts, or Agents hallucinating hidden rules | Inline `<!-- cb:roles=... -->...<!-- /cb -->` redaction stripped from reads/search snippets + explicit `🔒 STUB` notices for restricted documents |
| Soft linter warnings that still ship forbidden wording | Declarative YAML hard-block Linter enforced on write and via `lint_outgoing_draft` (`blocker_count > 0` hard-fails) |
| Parallel Agents overwriting each other silently | Per-project async write lock, `expected_revision` optimistic lock, `idempotency_key` deduplication, unique-anchor `patch_section`, keyframe snapshots, and `rollback_revision` |
| Exposing admin write endpoints when serving remote staff | Default-ON `block_remote_admin=true` (`403 REMOTE_WRITE_FORBIDDEN` over `X-CanonBase-Ingress: remote-relay` or non-loopback IPs) + WireGuard `/32` & `frp` TLS relay scripts |
| Primary desktop not online 24/7 for remote readers | Dual-mode `--node-mode replica_ro` cloud replica with quad-tuple `(write_epoch, rev_id, head_sha256, lease_holder)` handshake, zero-downtime `swap_repository()`, and offline `submit_handoff` inbox auto-sync |
| Non-technical staff & strict remote MCP clients (WorkBuddy / Codex / Gemini App) | 18 KB zero-Python native Windows `canonbase-bridge.exe` (`bundle-staff` ZIP, proxy-immune) + Gemini-clean JSON Schema sanitization, `ToolAnnotations`, and read-only Capability URLs (`/mcp/{id}/cap/{secret}`) |

## Architecture & isolation flow

```mermaid
flowchart LR
    L[Local Agents<br/>Antigravity / Codex / Gemini CLI] -->|stdio-bridge / loopback HTTP| GW
    R[Remote Read-Only Agents<br/>WireGuard /32] -->|VPS Nginx + remote-relay header| GW
    GW[FastMCP Gateway<br/>127.0.0.1:18765] -->|Bearer SHA-256 + RBAC + Rate Limit| RT[ProjectRuntime<br/>Per-Project AsyncLock]
    RT --> DB[(SQLite WAL + FTS5<br/>8 cb_* tables + optional plugin)]
    RT --> LINT[YAML Hard-Block Linter<br/>& Inline Redaction]
    RT --> WEB[Offline Static Web<br/>& UTF-8 ZIP Compiler]
```

- **Protocol-level RBAC**: Read-only tokens never even see write tools in `tools/list`; direct attempts return `TOOL_FORBIDDEN`, and cross-project calls return `403 PROJECT_MISMATCH`.
- **Read-only controlled assets**: Pure-Python (`python-docx`, `openpyxl`, `pymupdf`) incremental SHA-256 text mirrors with `frozen=1` tamper locks.
- **Hot backups**: SQLite Online Backup API + isolated `manifest.sha256` & `PRAGMA integrity_check` verification without blocking WAL reads/writes.

## Try the Demo KB in 60 seconds

```bash
git clone https://github.com/WeiBeta/CanonBase-public.git
cd CanonBase-public
uv sync --dev

# 1. Seed the Starport Ops Demo KB (`demo`) & build 3 role-isolated offline static sites
uv run python examples/demo-kb/seed_demo.py

# 2. Verify code style and standalone test suite
uv run ruff check .
uv run pytest -v

# 3. Generate a local Bearer token & launch the FastMCP gateway
uv run canonbase token-gen --prefix cb_demo
# Copy configs/policies/tokens.example.yaml -> configs/policies/tokens.local.yaml and paste token_sha256
uv run canonbase serve --host 127.0.0.1 --port 18765
```

Open `dist/web/demo-public/index.html` beside `dist/web/demo-admin_full/index.html` in any browser to see inline redaction stripping and anti-hallucination STUB rendering side by side. See [docs/QUICKSTART.md](docs/QUICKSTART.md) for full harness registration.

## 18 built-in MCP tools

| Category | Tools |
| --- | --- |
| **Discovery & Read** | `get_index_brief` · `search_knowledge` · `get_section` · `assemble_document` · `query_params` · `get_decisions` · `trace_impact` |
| **Controlled Assets** | `list_assets` · `get_asset_mirror` (paginated) |
| **Compliance Gate** | `lint_outgoing_draft` |
| **Governed Writes** | `patch_section` · `upsert_param` · `record_decision` · `link_edge` · `rollback_revision` |
| **Async Handoff** | `submit_handoff` · `list_handoff` · `approve_proposal` |

## Repository layout

| Path | Contents |
| --- | --- |
| [`src/canonbase/`](src/canonbase/__init__.py) | Core engine (SQLite WAL + FTS5, patch/redaction/backup/assets), YAML linter, FastMCP gateway, `stdio-bridge`, offline static Web compiler, and `CanonBasePlugin` protocol |
| [`configs/projects/`](configs/projects/demo.yaml) | Per-project YAML config (`demo.yaml`; add new KBs with zero Python code) |
| [`configs/policies/`](configs/policies/demo-roles.yaml) | RBAC role tool whitelists, declarative YAML linter rules, and Bearer token hash template |
| [`configs/harness/`](configs/harness/README.md) | Antigravity, Codex CLI, Gemini CLI, and remote client templates + 5-step copywriting skill |
| [`configs/deploy/`](configs/deploy/README.md) | Windows persistent daemon, daily Online Backup, firewall, Caddy TLS, WireGuard `/32` + `frp` TLS relay, and 24×7 cloud `replica_ro` sync scripts |
| [`tools/windows-bridge/`](tools/windows-bridge/README.md) | Zero-dependency C# source & build script for the ~18 KB native Windows `canonbase-bridge.exe` |
| [`examples/demo-kb/`](examples/demo-kb/seed_demo.py) | Turnkey Starport Ops Demo KB seed script and read-only sample assets |
| [`docs/`](docs/QUICKSTART.md) | [Quickstart](docs/QUICKSTART.md), [Architecture](docs/ARCHITECTURE.md), and [LAN / Relay Deployment](docs/DEPLOY-RELAY.md) |
| [`tests/`](tests/test_public_demo_e2e.py) | Standalone storage, rollback, RBAC/linter, 10-way concurrency, backup, and Demo E2E tests |

## Public edition and contributing

`CanonBase-public` is generated deterministically from a frozen commit of the private source repository with its own manifest (`PUBLIC-MANIFEST.json`) and zero private Git history. Private deployments, business data, operational logs, and credentials never enter this repository.

Issues and pull requests are welcome with a minimal sanitized reproduction and test coverage (`uv run ruff check . && uv run pytest -v`).

## License

Licensed under [GNU Affero General Public License v3.0 only (`AGPL-3.0-only`)](LICENSE).

*The cover image (`assets/hero.jpg`) is a conceptual illustration generated for this project; it is not a product screenshot or performance evidence.*