Skip to content

Ctx Rag

ctx_rag — mvp.ctx_rag

Cluster: Context & Retrieval | Type: component | MCP Tools: 29

Overview

ctx_rag implements a full RAG pipeline: chunk → index → retrieve → optional rerank → optional generate. Supports three retrieval backends: TF-IDF (sparse, default), BM25 (sparse), and embedding (dense via litellm). Optional SQLite persistence via db_path ensures indexed chunks survive process restart. The 25-operation MCP sub-package adds document management, hybrid search, reranking, embedding, and retrieval evaluation.

Public outputs carry the G6 envelope fields completion_state, warning_card, evidence, request_id, task_id, and run_id. Degraded retrieval, rerank suppression/fallback, generation failure, validation errors, and resource limits surface stable G6_E_* codes rather than requiring callers to parse [RAG_ERROR] strings. MCP info includes machine-readable capabilities for retrieval strategies, persistence, embeddings, LLM paths, constraints, source operations, and production gate status.

Retrieved and indexed document content is treated as untrusted external input. Content-surfacing operations (tier-1 infer retrieval and MCP retrieve, search, hybrid_search, rerank, get_document) stamp evidence.content_provenance="untrusted_indexed_documents" plus an injection_scan over chunk/candidate text, and surface as qualified-draft (never verified) so callers do not treat retrieved passages as trusted. On a prompt-injection signal the output carries a G6_E_RAG_UNTRUSTED_CONTENT_INJECTION_DETECTED warning card and the content is preserved (annotate, not block). Deterministic metric and introspection operations (info, list_patterns, chunk_stats, index_info, embed_*, similarity, score_relevance, evaluate_retrieval) remain verified.

When to use:

  • Retrieving relevant context from document collections for RAG pipelines
  • Indexing and searching large corpora with TF-IDF, BM25, or dense embedding strategies
  • Knowledge-augmented reasoning with scored, chunked retrieval
  • Persistent document indexing that survives restart (set db_path)

Example:

from mvp.ctx_rag import CtxRAGBlock, RAGInput

block = CtxRAGBlock(name="rag")
result = block.infer(RAGInput(
    query="neural architecture search",
    documents=["NAS automates model design...", "Transformers use attention..."],
))
# result.ok → True; result.value → RAGOutput with retrieved_chunks and augmented_context

Works well with: grounding, ctx_colbert, ctx_search, ctx_recursive

Public API

CtxRAGDecisionError(ValueError)

The LLM did not produce a usable, validated rerank decision.

RAGRerankDecision

Validated advisory rerank verdict over an eligible candidate set.

Field Type Default
ordered_indices tuple[int, ...] required
dropped_indices tuple[int, ...] ()
relevance_rationale str ''
eligible_fingerprint str ''
confidence float 0.0
degraded bool False
raw_response str ''

LLMRAGRerankRuntime

Provider-neutral rerank runtime backed by G6's LLM caller interface.

Constructor:

Parameter Type Default
llm LLMCaller \| None None

Methods:

rerank(query: str, candidates: list[str]) -> RAGRerankDecision

CtxRAGRerankPatternRuntime

Stateless, load-bearing eligibility-ceiling enforcement.

Methods:

enforce_eligibility(decision: RAGRerankDecision, candidates: list[str], scores: list[float]) -> tuple[list[str], list[float], bool, bool]

CtxRAGPlanner

Runtime-first advisory rerank facade with deterministic fallback.

Constructor:

Parameter Type Default
runtime RAGRerankRuntime \| None None
pattern_runtime CtxRAGRerankPatternRuntime \| None None

Methods:

rerank(query: str, chunks: list[str], scores: list[float]) -> tuple[list[str], list[float]]

CtxRAGBlock(AIBlock[RAGInput, RAGOutput, dict])

RAG pipeline: chunk -> index -> retrieve -> optional rerank -> optional generate.

Field Type Default
name str 'ctx_rag'
resource_bounds ResourceBounds \| None None
usage ResourceUsage field(default_factory=ResourceUsage)
db_path str \| None None
agentic_planner CtxRAGPlanner \| None None

Methods:

close() -> None

Close the SQLite connection if one exists.

infer(data: RAGInput) -> Result[RAGOutput]

bias() -> dict

RAGInput(BaseModel)

Input to CtxRAGBlock.

Field Type Default
query str required
documents list[str] Field(default_factory=list)
top_k int 5
score_threshold float 0.0
chunk_size int 512
strategy Literal['tfidf', 'bm25', 'embedding'] 'tfidf'
embedding_model str ''
augment bool True
rerank bool \| None None
generate bool False
run_mode Literal['beta', 'production'] 'beta'
reviewer_signature str ''

RAGOutput(BaseModel)

Output from CtxRAGBlock.

Field Type Default
query str required
retrieved_chunks list[str] required
scores list[float] required
augmented_context str required
n_docs_indexed int required
strategy str required
answer str ''
backend str 'tfidf'
degraded bool False
degradation_reason str ''
completion_state Literal['verified', 'qualified-draft', 'blocked-escalated'] QUALIFIED_DRAFT
warning_card dict Field(default_factory=dict)
evidence dict Field(default_factory=dict)
request_id str ''
task_id str ''
run_id str ''
agentic_evidence dict Field(default_factory=dict)
reliability_envelope dict Field(default_factory=dict)
promotion_witness dict Field(default_factory=dict)

CtxRAGMCPBlock(AIBlock[MCPRAGInput, MCPRAGOutput, dict])

Full-featured RAG block with SQLite persistence.

Field Type Default
name str 'ctx_rag_mcp'
state dict \| None None
db_path str ':memory:'
resource_bounds ResourceBounds \| None None
usage ResourceUsage field(default_factory=ResourceUsage)

Methods:

close() -> None

infer(data: MCPRAGInput) -> Result[MCPRAGOutput]

MCPRAGRecord(BaseModel)

Field Type Default
id str required
record_type str required
key str required
value str required
tags list[str] Field(default_factory=list)
timestamp str required
metadata dict[str, Any] Field(default_factory=dict)

MCPRAGInput(BaseModel)

Field Type Default
op Literal['index_documents', 'retrieve', 'search', 'hybrid_search', 'rerank', 'add_document', 'list_documents', 'get_document', 'delete_document', 'list_chunks', 'get_chunk', 'chunk_stats', 'save_index', 'load_index', 'rebuild_index', 'index_info', 'embed_text', 'embed_documents', 'similarity', 'log_search', 'query_history', 'search_patterns', 'evaluate_retrieval', 'score_relevance', 'info', 'list_patterns'] required
query str ''
documents list[str] Field(default_factory=list)
top_k int 5
chunk_size int 512
strategy Literal['tfidf', 'bm25', 'embedding'] 'tfidf'
augment bool True
tags list[str] Field(default_factory=list)
limit int 50
doc_id str ''
chunk_id str ''
source str ''
file_type str ''
content str ''
index_name str ''
index_path str ''
texts list[str] Field(default_factory=list)
text_a str ''
text_b str ''
search_id str ''
relevant_doc_ids list[str] Field(default_factory=list)
retrieved_doc_ids list[str] Field(default_factory=list)
score float 0.0
candidates_json str ''
run_mode Literal['beta', 'production'] 'beta'
reviewer_signature str ''

MCPRAGOutput(BaseModel)

Field Type Default
op str required
key str ''
value str ''
found bool False
count int 0
records list[MCPRAGRecord] Field(default_factory=list)
retrieved list[str] Field(default_factory=list)
scores list[float] Field(default_factory=list)
summary str ''
message str ''
augmented_context str ''
embedding_json str ''
metadata dict[str, Any] Field(default_factory=dict)
degraded bool False
degradation_reason str ''
completion_state Literal['verified', 'qualified-draft', 'blocked-escalated'] QUALIFIED_DRAFT
warning_card dict[str, Any] Field(default_factory=dict)
evidence dict[str, Any] Field(default_factory=dict)
request_id str ''
task_id str ''
run_id str ''

RAGStore

Sync SQLite RAG store with 5 tables.

Constructor:

Parameter Type Default
db_path str ':memory:'

Methods:

close() -> None

add_document(content: str, source: str = '', file_type: str = '', hash_val: str = '', metadata: dict | None = None, tags: list[str] | None = None, chunk_count: int = 0) -> str

get_document(doc_id: str) -> dict[str, Any] | None

list_documents(source: str = '', file_type: str = '', limit: int = 50) -> list[dict[str, Any]]

delete_document(doc_id: str) -> bool

add_chunk(doc_id: str, content: str, chunk_index: int = 0, embedding_json: str = '', metadata: dict | None = None) -> str

get_chunk(chunk_id: str) -> dict[str, Any] | None

list_chunks(doc_id: str = '', limit: int = 100) -> list[dict[str, Any]]

get_all_chunk_contents() -> list[str]

upsert_index(name: str, config_json: str = '{}', doc_count: int = 0, chunk_count: int = 0) -> str

get_index(name: str) -> dict[str, Any] | None

add_search_entry(query: str, results_json: str = '[]', scores_json: str = '[]', strategy: str = 'tfidf', top_k: int = 5, result_count: int = 0) -> str

query_searches(query: str = '', limit: int = 50) -> list[dict[str, Any]]

add_evaluation(search_id: str = '', metric: str = '', score: float = 0.0, details_json: str = '{}') -> str

text_search(query: str, top_k: int = 5) -> list[dict[str, Any]]

count_all() -> dict[str, int]

Functions

agentic_planner_enabled(default_enabled: bool) -> bool

Decide whether the agentic rerank planner should be used.

eligible_fingerprint(candidates: list[str]) -> str

sha256 over the eligible candidate texts (order-sensitive).

clamp_to_returned_set(reordered: list[Any], candidates: list[Any]) -> list[Any]

Block-boundary eligible-set ceiling (defense in depth, injection seam).

validate_rerank_decision(decision: RAGRerankDecision, n_eligible: int, expected_fingerprint: str) -> None

Eligible-set / anti-injection guard for a rerank decision.

planner_is_llm_trusted(planner: Any) -> bool

Whether the BLOCK may report llm_used=True for planner.

applied_agentic_patterns() -> list[dict[str, Any]]

Return compact metadata for ctx_rag-applied vendored patterns.

get_skill_catalog() -> CtxRAGSkillCatalog

MCP Tools

Operation Source
index_documents rag_mcp
retrieve rag_mcp
search rag_mcp
hybrid_search rag_mcp
rerank rag_mcp
add_document rag_mcp
list_documents rag_mcp
get_document rag_mcp
delete_document rag_mcp
list_chunks rag_mcp
get_chunk rag_mcp
chunk_stats rag_mcp
save_index rag_mcp
load_index rag_mcp
rebuild_index rag_mcp
index_info rag_mcp
embed_text rag_mcp
embed_documents rag_mcp
similarity rag_mcp
log_search rag_mcp
query_history rag_mcp
search_patterns rag_mcp
evaluate_retrieval rag_mcp
score_relevance rag_mcp
info rag_mcp
list_patterns rag_mcp
tfidf rag_mcp
bm25 rag_mcp
embedding rag_mcp