Sovereign RAG — Operations Skill
You are operating a PROVEN, battle-tested RAG platform. Do not improvise —
follow the recipe. The recipe encodes a week of debugging so you don't
repeat it.
Ground rules (non-negotiable)
- Use the project's pinned Python (the one with psycopg2/pgvector/dotenv
installed). The system default often lacks these. Check the project's
requirements.txt / .tool-versions / README and use that interpreter.
- Never print secrets. Passwords/tokens live in the project's untracked
.env (or secrets vault). Mask anything credential-like (first 3 chars +
length only). Never echo a full value into logs, chat, or files.
- Shell inline scripts break on quotes/asterisks — always write a short
temp script to the project's scratch dir and run it. Move it to
tests/ or scripts/ if it becomes a standard op.
- Use the TEST database for pentest/eval — never prod. The engine reads
two URLs: the app-role URL (prod/serving) and the pentest URL (test). If
the user says "test" or "eval", set BOTH the engine URL AND the app URL to
the test DB, or the engine silently hits prod.
- App role first. The engine must connect as a non-superuser app role
with FORCE ROW LEVEL SECURITY — never a superuser. Superusers bypass RLS
(lesson L1).
The recipe (READ before acting)
- State:
docs/01_STATE.md — what exists, architecture map, what's proven
- Environment:
docs/02_ENVIRONMENT.md — exact environment, DBs, .env layout
- Runbooks:
docs/03_RUNBOOKS.md — copy-paste daily ops
- LESSONS (read before changing code):
docs/04_LESSONS.md — the journal
- Enterprise gaps:
docs/05_ENTERPRISE_GAPS.md — the roadmap
- Cheatsheet:
docs/06_CHEATSHEET.md — one-page commands
If a recipe file is missing in this repo, the skills themselves below ARE the
recipe — the structure (ground rules → runbooks → lessons → gaps) is the
pattern.
Standard command flows
"Run the pentest" / "is it ready" / proof
python pa/pentest.py
- Expect
PASS: 31 WARN: 0 FAIL: 0 ERROR: 0, exit 0.
- Report overwrites
reports/PENTEST_REPORT.md.
- Any FAIL → read the lesson list below, don't guess.
RAGAS eval
python pa/ragas_eval.py --sample 10
- Expect Faithfulness ≈0.92, Context Precision/Recall 1.00, Hit@1 1.00.
Feed documents (ingest)
python ingest/raw_extractor.py --batch # binaries → text
python ingest/pgvector_feed.py --domain <d> --source <dir> # text → vectors
- De-dupe is by sha256 ledger / registry — re-running is safe and skips.
Provision a tenant
python tenant.py --name "Demo Clinic" --vertical klinik --plan "Ops"
- Creates
tenants/<domain>/config.json + demo corpus + smoke questions
- registry row. Default = TEST DB (safe).
--prod for real.
Internal retrieval eval
python eval_set.py
- Expect high pass-rate; client domains 100%.
Triage order for "something's wrong"
- Is the DB up? (
docker compose ps)
- Is the DB reachable AS the APP ROLE? (probe with app URL, not admin)
- Run the pentest — which test fails? Read the FAIL line.
- Wrong-but-passing retrieval → check eval cases + embedding cache (L6!).
- Read the lessons — your bug is probably already there.
The 10 lessons (if anything looks wrong, it's one of these)
- Superuser bypasses RLS — engine must run as a non-superuser app role +
FORCE RLS.
- Cached DB connection bleeds tenant GUC — re-pin the tenant on every call.
- Documents can inject prompts — synthesis context is DATA, not instructions.
- Empty context = hallucination — refuse before calling the LLM.
- Provider failover must retry at RUNTIME (primary→fallback chain), not
just at startup.
- Embedding cache can CORRUPT retrieval (near-duplicate hit) — exact-key
caching only; never text[:200].
- RLS policy errors if GUC unset — pin tenant on every per-tenant table
connection.
- jsonb needs
psycopg2.extras.Json, not a raw dict.
- App role needs
GRANT USAGE ON ALL SEQUENCES for SERIAL inserts +
per-table CRUD.
- Shell + inline scripts = broken; write a temp file.
What to do when a task isn't in the runbooks
- Read the enterprise-gaps roadmap — the unbuilt surround (SSO, user audit,
web UI, monitoring, load tests). If the request touches a gap, say which
one it maps to and build against the same RLS/answer-log patterns.
- When changing engine code, re-read the relevant lesson first. If you see
"flaky" behavior, assume L6 first — it's the scariest failure mode (silent
wrong answers), not the most visible.
Skill pattern: ground rules → runbooks → lessons → gaps. The value is not
the code; it's the debugging that the next operator doesn't have to repeat.
1---2name: sovereign-rag-ops3description: Operations for a PROVEN, battle-tested multi-tenant RAG platform. Use when the task is "run the pentest", "is the product ready", "verify retrieval", "provision a tenant", "feed documents", "evaluate the RAG engine", or any day-to-day operation on a tenant-isolated retrieval engine. Bakes in the PROVEN recipe (runbooks + 10 hard-won lessons) so the agent executes the stack correctly without re-deriving anything.4---56# Sovereign RAG — Operations Skill78You are operating a PROVEN, battle-tested RAG platform. Do not improvise —9follow the recipe. The recipe encodes a week of debugging so you don't10repeat it.1112## Ground rules (non-negotiable)13141. **Use the project's pinned Python** (the one with psycopg2/pgvector/dotenv15 installed). The system default often lacks these. Check the project's16 `requirements.txt` / `.tool-versions` / README and use that interpreter.172. **Never print secrets.** Passwords/tokens live in the project's untracked18 `.env` (or secrets vault). Mask anything credential-like (first 3 chars +19 length only). Never echo a full value into logs, chat, or files.203. **Shell inline scripts break on quotes/asterisks** — always write a short21 temp script to the project's scratch dir and run it. Move it to22 `tests/` or `scripts/` if it becomes a standard op.234. **Use the TEST database for pentest/eval — never prod.** The engine reads24 two URLs: the app-role URL (prod/serving) and the pentest URL (test). If25 the user says "test" or "eval", set BOTH the engine URL AND the app URL to26 the test DB, or the engine silently hits prod.275. **App role first.** The engine must connect as a non-superuser app role28 with FORCE ROW LEVEL SECURITY — never a superuser. Superusers bypass RLS29 (lesson L1).3031## The recipe (READ before acting)3233- **State:** `docs/01_STATE.md` — what exists, architecture map, what's proven34- **Environment:** `docs/02_ENVIRONMENT.md` — exact environment, DBs, .env layout35- **Runbooks:** `docs/03_RUNBOOKS.md` — copy-paste daily ops36- **LESSONS (read before changing code):** `docs/04_LESSONS.md` — the journal37- **Enterprise gaps:** `docs/05_ENTERPRISE_GAPS.md` — the roadmap38- **Cheatsheet:** `docs/06_CHEATSHEET.md` — one-page commands3940If a recipe file is missing in this repo, the skills themselves below ARE the41recipe — the structure (ground rules → runbooks → lessons → gaps) is the42pattern.4344## Standard command flows4546### "Run the pentest" / "is it ready" / proof47```bash48python pa/pentest.py49```50- Expect `PASS: 31 WARN: 0 FAIL: 0 ERROR: 0`, exit 0.51- Report overwrites `reports/PENTEST_REPORT.md`.52- Any FAIL → read the lesson list below, don't guess.5354### RAGAS eval55```bash56python pa/ragas_eval.py --sample 1057```58- Expect Faithfulness ≈0.92, Context Precision/Recall 1.00, Hit@1 1.00.5960### Feed documents (ingest)61```bash62python ingest/raw_extractor.py --batch # binaries → text63python ingest/pgvector_feed.py --domain <d> --source <dir> # text → vectors64```65- De-dupe is by sha256 ledger / registry — re-running is safe and skips.6667### Provision a tenant68```bash69python tenant.py --name "Demo Clinic" --vertical klinik --plan "Ops"70```71- Creates `tenants/<domain>/config.json` + demo corpus + smoke questions72 + registry row. Default = TEST DB (safe). `--prod` for real.7374### Internal retrieval eval75```bash76python eval_set.py77```78- Expect high pass-rate; client domains 100%.7980### Triage order for "something's wrong"811. Is the DB up? (`docker compose ps`)822. Is the DB reachable AS the APP ROLE? (probe with app URL, not admin)833. Run the pentest — which test fails? Read the FAIL line.844. Wrong-but-passing retrieval → check eval cases + embedding cache (L6!).855. Read the lessons — your bug is probably already there.8687## The 10 lessons (if anything looks wrong, it's one of these)88891. Superuser bypasses RLS — engine must run as a non-superuser app role +90 FORCE RLS.912. Cached DB connection bleeds tenant GUC — re-pin the tenant on every call.923. Documents can inject prompts — synthesis context is DATA, not instructions.934. Empty context = hallucination — refuse before calling the LLM.945. Provider failover must retry at RUNTIME (primary→fallback chain), not95 just at startup.966. Embedding cache can CORRUPT retrieval (near-duplicate hit) — exact-key97 caching only; never text[:200].987. RLS policy errors if GUC unset — pin tenant on every per-tenant table99 connection.1008. jsonb needs `psycopg2.extras.Json`, not a raw dict.1019. App role needs `GRANT USAGE ON ALL SEQUENCES` for SERIAL inserts +102 per-table CRUD.10310. Shell + inline scripts = broken; write a temp file.104105## What to do when a task isn't in the runbooks106107- Read the enterprise-gaps roadmap — the unbuilt surround (SSO, user audit,108 web UI, monitoring, load tests). If the request touches a gap, say which109 one it maps to and build against the same RLS/answer-log patterns.110- When changing engine code, re-read the relevant lesson first. If you see111 "flaky" behavior, assume L6 first — it's the scariest failure mode (silent112 wrong answers), not the most visible.113114---115*Skill pattern: ground rules → runbooks → lessons → gaps. The value is not116the code; it's the debugging that the next operator doesn't have to repeat.*