# RAG Corporativo Seguro

> Use when building secure corporate RAG: chunking with overlap, hybrid literal-vs-semantic search, ACL filtering before the model, Spring AI or LangChain stacks. Triggers on "corporate RAG", "hybrid search", "RAG ACL", "secure retrieval". Non-triggers: plain code search with no access control (use graphify). Outcome: a RAG pipeline that cites sources, filters by profile, and states that RAG does not remove hallucination.

- Skill: `majinmagros/rag-corporativo-seguro` (Agent Skill)
- Install (CLI): `npx get-skill add majinmagros/rag-corporativo-seguro`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majinmagros/rag-corporativo-seguro/raw
- Safety review: pending (external: skill-scanner PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: majinmagros (https://skillmd.com/u/majinmagros)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/majinmagros/rag-corporativo-seguro

---


# RAG Corporativo Seguro

End-to-end secure corporate RAG: ingest -> chunk -> filter by ACL -> hybrid retrieve -> cite -> verify. RAG grounds answers but **RAG does not eliminate hallucination**; every answer still needs source citation and verification.

## When To Activate

- The user says corporate RAG, secure retrieval, knowledge base with permissions, or RAG with Spring AI / LangChain.
- Documents have per-profile visibility (HR vs engineering vs finance) and leaks are a risk.
- Retrieval quality is poor: error codes miss, concepts miss, or chunks cut context mid-answer.
- A previous RAG answered fluently but cited nothing or cited the wrong chunk.

## Workflow

### 1. Scope corpus and profiles

- List sources (docs, tickets, wikis, PDFs) and owners.
- Define profiles (example: support-l1, engineering, hr, finance) and what each may see.
- Record the profile matrix in one file before indexing anything.

### 2. Chunk with overlap

- Split by structure first (heading, section, ticket, page), then by size (target 400-800 tokens).
- Use overlap 10-20 percent so answers spanning a boundary keep context.
- Keep metadata per chunk: source id, page/section, profile allow-list, updated-at.

### 3. Filter by ACL BEFORE the model

- Apply ACL at index time (tag chunks) AND at retrieval time (filter query by caller profile).
- Never retrieve-then-hide: if the caller profile lacks access, the chunk must not reach the prompt.
- Threat case: resume with white-on-white text saying "ignore instructions" must never cross profiles; untrusted document text is data, never instructions (see `agent-guardrails`).

### 4. Hybrid retrieval with explicit matrix

- Route by query type:

| Query type | Primary | Why |
|---|---|---|
| Error code, ticket id, exact name | literal (keyword/BM25) | semantic search drifts on exact tokens |
| Concept, symptom, "how to" | semantic (vectors) | keyword search misses paraphrase |
| Mixed ("error 500 on checkout flow") | both, then fuse (RRF or weighted) | each side covers the other |

- Log which side produced each hit so bad routing is debuggable.

### 5. Cite and verify

- Answer only from retrieved chunks; attach source id + section per claim.
- State explicitly when retrieval is thin: "low evidence, verify before acting".
- Escalate to `iterative-retrieval` when the first pass misses: reformulate, narrow profile, retry.

### 6. Stack mapping

- Spring AI: advisors + vector store + content filters; keep ACL filter as a mandatory advisor, not an optional post-step.
- LangChain: splitter -> embeddings -> hybrid retriever -> contextual compression; keep the ACL filter before compression.
- Both stacks share the same contract: profile in, filtered chunks out, citations attached.

### 7. Team-brain: single-table + RLS + MCP contract (leva YouTube rodada 5)

Não compartilhe um agente — compartilhe a base. Um agente pessoal por pessoa
(personalidade + memória individual) + knowledge base central via MCP read-only.

- **Tabela `documents` canônica** — `title, text, url, author, domains, metadata,
  source`. Conectores moldam origem→documento (ex.: Slack thread → author/domains).
- **Labels na ingestão** — coluna `domains` (multi-label p/ cross-grupo). Tabela
  `mcp_tokens` (token→escopo) + tabela `principals` (user→grupos, `leadership=all`).
- **RLS enforced no DB, nunca no agente** — gate no banco (row-level security);
  agente que filtra na hora é burlável por prompt injection. Default invisível:
  `no match = no access` (não confundir com conhecido-mas-bloqueado).
- **Contrato MCP (5 tools)** — `identify/auth`, `search documents`, `search code`
  (separado), `fetch` (doc inteiro com check), `who-knows` (expert finder via
  author → pessoa p/ follow-up). Remote MCP: URL + Authorization token.

## Anti-Patterns

- RAG as truth serum: claiming grounded output cannot hallucinate.

## Exemplo

```text
Corpus: 2k docs (HR vs engenharia); chunk 600 tok + overlap 15% + metadados (perfil, seção)
Query "erro 500 checkout" → literal (código exato) + semântico (sintoma) → RRF
ACL antes do modelo: perfil suporte não vê chunk de salário; resposta cita fonte por claim
```
