Skip to content

Shadow mode

Español: es/SHADOW.md

Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.

Measure the impact of a candidate policy on your real workload before you roll it out. Shadow mode is off by default: nothing changes unless you build a ShadowGuard.

from sqlguardllm import Guard, Policy
from sqlguardllm.shadow import ShadowGuard

guard = ShadowGuard(Guard(active_policy), candidate=candidate_policy)
rows = guard.wrap(run_query, context_fn)("SELECT ...")   # or guard.check(sql, context)
print(guard.report().to_dict())

What it guarantees

  • Only the active guard decides. check, enforce and wrap return exactly what the active Guard returns, and the SQL that runs is the active guard's rewritten SQL. The candidate runs after the active decision, never authorizes a query, never changes the SQL and never triggers a second execution.
  • Candidate errors are counted, not raised. If the candidate fails, the report counts it under error with the exception class name (never its message). An active-guard error propagates as usual and nothing is recorded.
  • No data leaves through the report. It holds counters, stable violation codes, exception class names, policy fingerprints and SQL shape fingerprints (sha256 of the SQL with literals replaced by ?). It has no SQL, literals, context values, identity, table or column names, or message text, so it cannot reveal anything the active policy hides. The optional on_event callback receives the same fields per query.

The report

Outcome Meaning
would_allow the candidate allows the query (with the same SQL the active guard would run, or the active guard denied it)
would_rewrite both allow, but the candidate would run different SQL (another row cap, filter or mask)
would_deny the candidate denies it; candidate_codes counts the violation codes
error the candidate could not be evaluated; error_types counts the exception classes

changes compares with the active decision: unchanged, rewritten, newly_denied (the active guard allows, the candidate denies: the ones to review; newly_denied_codes says why), newly_allowed and error. shapes lists the SQL shape fingerprints behind each change, so you can find the queries without ever logging them.

From the command line

sqlguardllm shadow --policy active.yaml --candidate candidate.yaml queries.sql
sqlguardllm shadow --policy active.yaml --candidate candidate.yaml traffic.jsonl --format json

queries.sql has one statement per line (blank lines and lines starting with -- are skipped); a .jsonl file has {"sql": "...", "context": {...}} per line. --context gives a default context. The output is counts and codes only. Exit code 0 on success, 2 on usage or policy errors.

Runnable example

examples/shadow_demo.py compares two policies on synthetic traffic against an in-memory SQLite database and shows that only the queries the active policy allows run:

uv run python examples/shadow_demo.py

Notes

  • The candidate also gets the context you pass; build its policy as you would for production (row filters need the same context keys, or they show up as missing_context).
  • A candidate Guard with its own audit still writes audit events; pass a Policy for a silent comparison.
  • The comparison doubles the guard's CPU time per query (the guard is local and fast; no database round trip is added).