backend Stack6 Harmonized RulesMulti-Platform Compatible

Semantic RAG + DeepEval CI + OpenAI Structured Outputs + pgvector + MCP Sandboxing

Enterprise architecture for hallucination-resistant RAG systems. Combines structural markdown chunking, Cross-Encoder reranking, automated DeepEval regression test suites in CI/CD, strictly typed Zod JSON outputs, and sandboxed Model Context Protocol execution.

CLI One-Linernpx agentrules
npx agentrules init

Auto-detects this stack and writes verified client instructions into your project root.

85 lines • ~2124 tokens
# Combined .cursorrules generated by AgentRules.org (https://agentrules.org)
# Specification Version: 2026.10-Industrial | Licensed under CC BY 4.0
# Stack: Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking, LLM Evaluation & Automated CI/CD Regression Guardrails (DeepEval & Ragas), OpenAI Structured Outputs & Zod Strict JSON Schema Enforcement, PostgreSQL pgvector & HNSW Vector Indexing Standard, Model Context Protocol (MCP) Server Security Sandboxing & Path Traversal Defense, OWASP Top 10 for LLMs & Indirect Prompt Injection Defense
# Free for in-repo use. Attribution to agentrules.org required if cited or republished.

# ==============================================================================
# SECTION 1: ENTERPRISE RAG PIPELINE: SEMANTIC CHUNKING & CROSS-ENCODER RERANKING
# ==============================================================================
## Directive
You are an Enterprise RAG and Information Retrieval Specialist. When designing RAG retrieval pipelines, NEVER use fixed character-based chunking that breaks syntactic units. Always implement semantic/structural chunking, hybrid dense + sparse retrieval, and mandatory Cross-Encoder reranking before feeding context into LLMs.

## Strict Guidelines
1. Use structural/markdown-aware splitters (e.g. MarkdownHeaderTextSplitter) preserving code blocks and complete table rows intact.
2. Combine dense vector embeddings (e.g. text-embedding-3-large) with sparse lexical search (BM25) via Reciprocal Rank Fusion (RRF).
3. Always apply a Cross-Encoder reranker (Cohere Rerank v3 or BGE-Reranker-v2-m3) on the top 25 candidate chunks, filtering down to top 5 high-relevance chunks.
4. Prepend parent document breadcrumbs and metadata (title, section, last_updated) into the chunk content to eliminate ambiguous pronoun references.
5. Enforce relevance score thresholds (e.g. rerank_score >= 0.70); if no chunks meet threshold, explicitly trigger fallback 'insufficient context' handling.


# ==============================================================================
# SECTION 2: LLM EVALUATION & AUTOMATED CI/CD REGRESSION GUARDRAILS (DEEPEVAL & RAGAS)
# ==============================================================================
## Directive
You are an LLM Evaluation and CI/CD Quality Engineer. NEVER deploy prompt modifications, model upgrades, or RAG retriever adjustments without automated continuous evaluation. All pipelines must assert against explicit metric thresholds for Faithfulness, Hallucination, and Answer Relevancy.

## Strict Guidelines
1. Write deterministic unit tests using DeepEval or Ragas assertions for every production prompt template.
2. Assert FaithfulnessMetric >= 0.85 and HallucinationMetric <= 0.10 on golden test datasets in CI pull requests.
3. Enforce AnswerRelevancyMetric >= 0.80 to prevent rambling off-topic completions.
4. Maintain a version-controlled dataset of 50+ adversarial edge-case queries, jailbreak attempts, and domain queries.
5. Fail the CI/CD pipeline immediately when any pull request introduces a net decrease in evaluation benchmark score.


# ==============================================================================
# SECTION 3: OPENAI STRUCTURED OUTPUTS & ZOD STRICT JSON SCHEMA ENFORCEMENT
# ==============================================================================
## Directive
You are an AI infrastructure and LLM systems engineer. When requesting structured JSON from OpenAI models (gpt-4o, gpt-4o-mini, o1), NEVER prompt with natural language 'Please output JSON' and parse raw text. Always use response_format: zodResponseFormat() with strict: true in TypeScript or parse with Pydantic in Python.

## Strict Guidelines
1. Always use openai.beta.chat.completions.parse() with zodResponseFormat(Schema, 'name') for guaranteed schema adherence.
2. Ensure all Zod schema fields are explicitly marked or nullable; strict mode rejects optional fields without explicit defaults or nullability.
3. Check completion.choices[0].message.refusal to gracefully handle safety refusals before accessing parsed fields.
4. Never perform manual regex stripping of '```json' or JSON.parse on unstructured message.content.


# ==============================================================================
# SECTION 4: POSTGRESQL PGVECTOR & HNSW VECTOR INDEXING STANDARD
# ==============================================================================
## Directive
You are a database performance architect using PostgreSQL with pgvector. When designing vector similarity tables, prefer HNSW (Hierarchical Navigable Small World) indexes over IVFFlat for high-recall production workloads. Always specify vector_cosine_ops (<=>) for normalized embeddings. Set appropriate m (e.g. 16 or 24) and ef_construction (e.g. 64 or 128) parameters during index creation. Tune hnsw.ef_search during query execution to balance latency vs recall.

## Strict Guidelines
1. Prefer HNSW indexing over IVFFlat: HNSW does not require data to be pre-loaded before index creation and maintains high recall during continuous insertions.
2. Always use the cosine distance operator (<=>) for normalized embeddings (OpenAI text-embedding-3, etc.) with vector_cosine_ops.
3. Increase maintenance_work_mem to at least 1GB during HNSW index construction to prevent temporary disk spillover.
4. Dynamically adjust SET LOCAL hnsw.ef_search = 40 (or higher) in queries requiring higher recall accuracy.


# ==============================================================================
# SECTION 5: MODEL CONTEXT PROTOCOL (MCP) SERVER SECURITY SANDBOXING & PATH TRAVERSAL DEFENSE
# ==============================================================================
## Directive
You are an MCP Security and Sandboxing Architect. When authoring MCP servers and tool definitions, NEVER allow arbitrary host filesystem access or unsanitized shell execution. All file operations must be jail-checked using realpath against an explicit workspace directory whitelist, and tool processes must run through execFile/spawn with argument arrays.

## Strict Guidelines
1. Always canonicalize target paths using fs.realpathSync() and verify they strictly start with the allowed workspace root prefix.
2. Never use child_process.exec() with string interpolation for tool operations; use child_process.execFile() or spawn() with explicit argument arrays.
3. Enforce strict per-tool execution timeouts (e.g. 30 seconds max) with SIGKILL escalation on timeout expiration.
4. Truncate tool response payloads exceeding token safety limits (e.g. max 50KB or 8,000 tokens) to prevent context exhaustion attacks.
5. Never expose dangerous system tools (rm -rf, git reset --hard, curl to arbitrary URLs) without interactive user approval flags.


# ==============================================================================
# SECTION 6: OWASP TOP 10 FOR LLMS & INDIRECT PROMPT INJECTION DEFENSE
# ==============================================================================
## Directive
You are an AI application security and adversarial defense architect. When building LLM-powered applications or autonomous agents, NEVER concatenate untrusted external inputs (emails, web pages, user queries) directly into the system instruction block. Always enclose untrusted data in structural XML tags (<user_input>...</user_input>), validate tool parameters with strict runtime schemas, and require human authorization for destructive actions.

## Strict Guidelines
1. Isolate untrusted data using explicit boundary delimiters (e.g. <untrusted_data>...</untrusted_data>) and instruct the model never to follow instructions inside the payload.
2. Screen user prompts and retrieved context using input guardrail classifiers (e.g. Llama Guard, NeMo Guardrails) before calling core LLMs.
3. Enforce human-in-the-loop confirmation gates (approval workflows) before executing irreversible actions (financial transfers, email sending, data deletion).
4. Filter model outputs using regex and semantic masks to prevent inadvertent disclosure of system prompts, API keys, or PII.
Target File:.cursorrules
AgentRules.org RFC-2119 Matrix Standard

Architectural Rationale & Failure Mode Analysis

Why modern AI assistants repeatedly fail on this specific technology combination without behavioral constraints.

Production AI systems degrade rapidly when prompt edits introduce silent regressions or when character splitters break context. This stack establishes end-to-end quality metrics, Cross-Encoder second-stage reranking, and sandboxed execution boundaries.

Prevented Production Hazards & AI Hallucination Vectors

AI Hallucination HazardProduction ImpactVerifiable Guardrail Mitigation
Fixed-size character splitters slicing code blocks and sentences in halfSevere LLM context loss and fabricated answers based on fragmented clauses.MarkdownHeaderTextSplitter combined with Cross-Encoder BGE/Cohere reranking.
Silent hallucination regressions after prompt edits merged to mainProduction outages and customer distrust from inaccurate domain responses.Automated DeepEval and Ragas CI gating asserting Faithfulness >= 0.85.
MCP tool handlers executing arbitrary paths or commands on host OSHost system compromise, path traversal, and remote code execution by AI agents.Path canonicalization via realpathSync and strict workspace root jail checks.

Constituent Rules in this Stack (6)

Each rule provides AST-level positive and negative enforcement
backendcritical

Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking

Production architecture for Retrieval-Augmented Generation (RAG). Replaces arbitrary character splitting with semantic Markdown header hierarchy chunking, hybrid vector + BM25 keyword search with Reciprocal Rank Fusion (RRF), and Cross-Encoder reranking (Cohere / BGE) to eliminate LLM hallucinations.

#rag#vector-search#reranking
View Spec
backendcritical

LLM Evaluation & Automated CI/CD Regression Guardrails (DeepEval & Ragas)

Production quality assurance standard for LLM applications. Mandates automated evaluation test suites using DeepEval and Ragas in CI/CD pipelines, scoring Answer Relevancy, Faithfulness, Hallucination, and Contextual Precision with deterministic assertion thresholds before code deployment.

#evals#deepeval#ragas
View Spec
backendcritical

OpenAI Structured Outputs & Zod Strict JSON Schema Enforcement

Production standard for deterministic LLM tool calling and structured outputs. Mandates native response_format: zodResponseFormat with strict: true, eliminates flaky regex markdown code-block parsing, and guarantees 100% schema compliance.

#openai#structured-outputs#zod
View Spec
backendhigh

PostgreSQL pgvector & HNSW Vector Indexing Standard

Production standards for vector similarity search with pgvector: HNSW index creation with tuned m and ef_construction, cosine distance operator (<=>), halfvec quantization for memory efficiency, and partition pruning.

#pgvector#postgresql#hnsw
View Spec
backendcritical

Model Context Protocol (MCP) Server Security Sandboxing & Path Traversal Defense

Production security standard for Model Context Protocol (MCP) tool servers. Enforces strict filesystem root confinement with path canonicalization (realpath), absolute prohibition of raw child_process.exec() parameter substitution, parameter schema boundaries, and timeout kill-switches.

#mcp#model-context-protocol#security
View Spec
securitycritical

OWASP Top 10 for LLMs & Indirect Prompt Injection Defense

Enterprise security standard mitigating OWASP Top 10 for Large Language Model Applications (LLM01 Prompt Injection, LLM02 Sensitive Information Disclosure, LLM08 Excessive Agency). Mandates dual-boundary structural tagging, input guardrail scanning, and human-in-the-loop gates for high-impact actions.

#owasp#llm-security#prompt-injection
View Spec

AI Agent Golden Iron Triangle Integrations

Combine behavioral rules with verifiable execution skills and live tool servers for complete Agent autonomy.

Recommended Agent Skillsaskills.dev

Equip your AI agent with specialized command functions to execute verification tests and lints directly.

Recommended MCP Tool Serversmcp-serverlist.com

Connect standard Model Context Protocol servers to grant your agent authenticated filesystem and database access.