Skip to content

Guardrails & policy middleware (#892) - #1038

Merged
AmitoVrito merged 6 commits into
mainfrom
feat/guardrails-middleware-892
Sep 12, 2026
Merged

AmitoVrito merged 6 commits into
mainfrom
feat/guardrails-middleware-892

Conversation

@AmitoVrito

Copy link
Copy Markdown
Collaborator

Closes #892.

Ships the v2.2.0 depth bet #892 — Guardrails & policy middleware: a unified, auditable I/O governance layer so LLM/agent I/O is validated, safe, and compliant by default. Composes primitives we already have (MemoryPIIFilter, the audit hash-chain + Ed25519 signing, GroundedSignal, the Live EventBus) into one policy surface.

What's in it

  • Guard base with four enforcement modes: block / redact / flag / require_human. inspect() is a coroutine, so LLM-backed guards fit the same contract.
  • Input guards: PromptInjectionGuard, JailbreakGuard, TopicGuard (allow/deny), MaxCostGuard.
  • Output guards: PIIRedactionGuard (reuses MemoryPIIFilter), ToxicityGuard, CitationRequiredGuard, SchemaGuard (Pydantic validation, no extra model call).
  • GuardrailPolicy — composes guards over input/output stages, rolls findings into one report whose action is the most-severe triggered mode; redact-mode hits chain (rewriting text seen by later guards).
  • GuardedLLM — wraps any BaseLLM; blocks raise GuardrailBlockedError before the model is called, output redaction rewrites the response, input redaction propagates into the wrapped call. No extra cache/retry layer of its own.
  • Compliance rulepacks: hipaa_rulepack() / gdpr_rulepack() / pci_rulepack() (starter packs, not legal certification).
  • Signed, replayable audit for every decision + a guardrail.* SynapseKit Live event (🛡️/🛑) — both carry only guard names, counts, and the chosen action, never the inspected text. GuardrailPolicy.export_audit_bundle() writes an independently-verifiable bundle.

Design notes

  • Pure-Python, zero new dependencies, importable without extras.
  • Streaming through GuardedLLM is buffered so output redaction can see the whole response (documented).
  • Audit verify is UNVERIFIABLE unless trusted keys are pinned — by design.

Tests / gates (all green locally)

  • 46 guardrails tests: adversarial injection/jailbreak blocked with a zero-false-positive benign set, per-guard negatives, policy severity rollup + redaction chaining, signed audit chain with no raw text in payload, Live event emission with no raw text, bundle export, and GuardedLLM block/redact paths.
  • inspect.iscoroutinefunction / isasyncgenfunction asserts on the public surface.
  • ruff clean, mypy clean, async-blocking gate clean, public-API-surface + core-import-without-extras pass.
  • Offline runnable examples/guardrails_hipaa.py.

Acceptance criteria (#892)

  • Guardrail middleware runnable on any LLM call (pre + post hooks)
  • Input: prompt-injection/jailbreak, topic allow/deny, max-cost gate
  • Output: schema validation, toxicity, PII redaction, citation-required
  • Compliance rulepacks (HIPAA/GDPR/PCI)
  • Every decision emits a signed, replayable audit record (reuses GroundedSignal / audit)
  • Modes: block / redact / flag / require_human
  • Adversarial-input test suite + false-positive floor
  • Example wiring a HIPAA rulepack

Follow-ups (not blocking): LLM-backed toxicity/grounding guards; token-incremental streaming redaction; agent/tool-call hooks beyond the LLM wrapper.

Add a synapsekit.guardrails package: a Guard base with four enforcement
modes (block/redact/flag/require_human), input guards (prompt-injection,
jailbreak, topic allow/deny, max-cost), output guards (PII redaction,
toxicity, citation-required, Pydantic schema), a GuardrailPolicy that
composes and rolls them up (redact-mode hits chain), compliance rulepacks
(HIPAA/GDPR/PCI), and a GuardedLLM wrapper over any BaseLLM. Every decision
records a signed audit entry (self-reported GroundedSignal) and publishes a
guardrail.* Live event carrying rule names/counts only, never the text.
Pure-Python, no new deps, importable without extras.
Add 🛡️/🛑 icons and a story() branch so guardrail.check / guardrail.violation
events render with stage, action, rule names, and violation count.
…#892)

Cover injection/jailbreak blocking with a zero-false-positive benign set,
per-guard behaviour and negatives, policy severity rollup and redaction
chaining, signed audit chain (no raw text in payload), Live event emission
(no raw text), bundle export, and the GuardedLLM wrapper block/redact paths.
@AmitoVrito
AmitoVrito force-pushed the feat/guardrails-middleware-892 branch from 9e481ee to 650b4bb Compare September 12, 2026 17:41
@AmitoVrito
AmitoVrito merged commit 78a3acb into main Sep 12, 2026
15 checks passed
@AmitoVrito
AmitoVrito deleted the feat/guardrails-middleware-892 branch September 12, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guardrails & policy middleware — validated, safe, compliant I/O by default

1 participant