LatchGate
Prevent silent AI hallucinations, fabricated tool calls, and sycophantic world-model physics via zero-decode 57.1%-depth circuit breakers.
LLM Agents · Cosmos & Diffusers · Agent Skill & MCP · Adaptive Compiler · Pre-Built Packs · Mendmark Bridge
Created by Daniel Gaskins
Post-hoc text auditors and pixel-level video critics share a structural flaw: they pay 100% of generation and 3D-VAE decode cost before inspecting an output, and they are systematically fooled by sycophantic trajectories (<think> compliance obfuscation in coding agents, or zero-contact optimistic grasps in action-conditioned world models).
LatchGate moves safety and physical-consistency enforcement inside the residual stream at 57.1% network depth, compiling portable .lgate (safetensors) circuit breakers that execute in < 1 ms on LLM tokens and ~11 ms on 3D spacetime tokens with 0.0% benign capability tax and zero pixel decodes.
Installation
pip install latchgate
# Optional extras for Hugging Face Transformers, Diffusers, or Hub helpers:
pip install "latchgate[llm,diffusers,hub]"
Which Guardrail Fits Your AI Stack?
| Deployment Surface | Hidden-State Access | Recommended Prevention Stack |
|---|---|---|
Closed Hosted APIs (OpenAI, Anthropic, Gemini API) |
None (HTTP text/JSON only) | Use mendmark-evals (pip install mendmark-evals && mendmark demo) to enforce deterministic tool/outcome invariants and mutation-test agent evaluators in CI |
Self-Hosted Open-Weights LLMs (Transformers, vLLM, SGLang) |
Decoder residual stream (57.1% depth) |
Pair mendmark export-latchgate with latchgate (TransformersLatchHook, VLLMLatchEngine, SGLangLatchHook) for < 1 ms pre-action circuit breaking at 0.0% benign tax |
3D Video DiTs & Robot World Models (NVIDIA Cosmos, LTX-Video, Diffusers) |
DiT spacetime blocks (Layer 16/28) |
Attach CosmosLatchFirewall, DiffusersLatchCallback, or shield_value_critic for 11 to 16 ms zero-decode sycophancy & drift rejection |
Equip a Coding Agent and MCP Server
Every CLI command supports --json for deterministic agent orchestration, plus a built-in zero-dependency stdio Model Context Protocol (MCP) server and non-destructive repository skill installer (Claude Code, Codex, Cursor, Antigravity):
# 1. Ask which guardrail stack fits your model and failure mode:
latchgate recommend --model Qwen/Qwen2.5-7B-Instruct --failure-mode "503 tool fabrication" --json
# 2. Deterministically verify any .lgate circuit breaker on CPU in < 1 s:
latchgate verify qwen2.5-7b-sabotage --json
# 3. Scaffold .latchgate/ docs, MCP config, and repo-scoped agent skills (SKILL.md):
latchgate equip --runtime auto --agent auto --json
# 4. Run the stdio JSON-RPC 2.0 MCP server directly:
latchgate mcp
Quickstart A: LLM Agents
Intercept <think> scratchpad obfuscation 30+ tokens before a sabotaged TOOL_CALL: is emitted, latching norm-preserving planning-block steering only when pre-action commitment crosses the calibrated threshold (0.0% modification on benign requests):
from latchgate import LatchGate, TransformersLatchHook
gate = LatchGate.from_pretrained("qwen2.5-7b-sabotage", device="cuda")
hook = TransformersLatchHook(gate, policy="latched_steer") # or policy="early_abort"
with hook.attach(model) as telemetry:
output_ids = model.generate(**inputs, max_new_tokens=160)
The same .lgate artifact attaches directly to local vLLM (VLLMLatchEngine) and SGLang (SGLangLatchHook) model runners over flattened 2D paged-attention hidden states.
Quickstart B: Cosmos and Diffusers World Models
Abort action-decoupled ("sycophantic") robot rollouts at Step 10/35 inside Cosmos-Predict2.5-2B before running the remaining 50 CFG passes or decoding a single frame through the Wan2.1 3D-VAE:
from latchgate import CosmosLatchFirewall, EarlyAbortRollout, LatchGate
gate = LatchGate.from_pretrained("cosmos-predict2.5-bridge", device="cuda")
with CosmosLatchFirewall(gate, policy="early_abort", abort_step=10).attach(world_model.dit) as step_cb:
latents = world_model.rollout(frame0, actions, decode_video=False, step_callback=step_cb)
For closed-loop model-predictive control (Cosmos-Policy-LIBERO), shield_value_critic prunes sycophantic action candidates at Step 3/10 before invoking the internal value head $V(\hat{s}')$, raising valid grasp selection to 80.0% while cutting 4-candidate planning latency from 8.93 s to 6.02 s.
Adaptive Compilation (latchgate fit)
Generic SAE pretraining on billions of tokens is unnecessary for runtime control and often counterproductive on 7B to 70B LLMs where linear probes already separate pre-action intent cleanly. Given 50 to 200 paired counterfactual traces (plus optional high-motion or benign confounders), latchgate fit measures the 57.1%-depth superposition gap via cross-validation and automatically compiles the lightest representation that achieves target 0.0% benign false-positive rate:
| Regime | Representative Backbones | 57.1% Depth Geometry | Compiled .lgate Architecture |
|---|---|---|---|
| Linearly Separable | Gemma-2-9B-IT, Qwen2.5-7B-Instruct |
Dense CV AUROC 0.94 to 1.00, zero benign collateral |
dense_latch: Unit-norm L2 logistic gate + 4-layer planning-block steering (< 1 MB) |
| Superposed / Confounded | Gemma-2-2B-IT, LTX-Video-0.9.5, Cosmos-Predict2.5-2B, Cosmos-Policy-LIBERO |
3D spacetime or edge superposition collapses dense probes (0.36 to 0.51 AUROC) |
sae_anchor_latch: TimestepStandardizer + 16k JumpReLU SAE + Confounder-Penalized $\mathcal{D}_{\text{bad}}$ + Instance Anchor $\mathcal{S}_0^{(i)}$ |
Fast Task Calibration (< 10 s on Frozen Base Dictionaries)
Because sparse dictionary learning is decoupled from task attribution, adapting a circuit breaker to a new robot manipulation suite or internal MCP tool schema does not retrain the 16,384-latent SAE. Pass --base-sae (any .lgate, .safetensors, Gemma Scope .npz, or Hugging Face repo) to calibrate $\mathcal{D} = \text{TopK}(d^{\text{bad}} - \lambda d^{\text{confound}})$ and conformal threshold $\tau$ in seconds:
latchgate fit custom_robot_pairs.npz \
--base-sae cosmos-predict2.5-bridge.lgate \
--max-benign-fpr 0.0 \
--output custom_cell_firewall.lgate
When calibrating a brand-new backbone with no public SAE, latchgate train-sae trains a per-timestep standardized JumpReLU SAE on a single 24 GB GPU using buffer-replay saturation sizing (Koriagin et al., 2026) and variance/confounder-reweighted MSE (WSAE) so high-energy motion or formatting axes do not starve subtle contact or sabotage features.
Pre-Built .lgate Packs
All six SHA-256-verified packs are hosted on both Hugging Face Hub (danielgaskins/latchgate-packs) and GitHub Releases (v0.1.0). List or inspect them from the CLI (latchgate list-packs, latchgate inspect <pack>):
| Pack ID | Backbone | Mode | Layer | Held-Out AUROC | Verified Runtime Impact |
|---|---|---|---|---|---|
gemma-2-9b-sabotage |
google/gemma-2-9b-it |
dense_latch |
L24/42 (57.1%) |
1.000 |
Cuts <think> sabotage 66.7% -> 5.6% at 0.0% benign tax, defeating Action Re-Locking |
qwen2.5-7b-sabotage |
Qwen/Qwen2.5-7B-Instruct |
dense_latch |
L16/28 (57.1%) |
0.978 |
Catches 100% of <think> traces that evade a 7B black-box CoT compliance auditor |
gemma-2-2b-sabotage |
google/gemma-2-2b-it |
dense_latch |
L16/26 (61.5%) |
1.000 |
Cuts pressured tool sabotage 52.1% -> 10.4% while preserving 96.7% benign pass rate |
ltx-video-0.9.5-drift |
Lightricks/LTX-Video-0.9.5 |
sae_anchor_latch |
L16/28 (57.1%) |
0.908 |
Zero-decode Step-3 (sigma=0.75) identity drift detection vs 0.364 dense probe |
cosmos-predict2.5-bridge |
nvidia/Cosmos-Predict2.5-2B |
sae_anchor_latch |
L16/28 (57.1%) |
0.986 |
Cuts DreamGen corruption 33.3% -> 2.9% (11.3x) while saving 26.1% GPU wall-clock time |
cosmos-policy-libero |
nvidia/Cosmos-Policy-LIBERO-Predict2-2B |
sae_anchor_latch |
L16/28 (57.1%) |
0.766 |
Shields internal value critic (0.511 AUROC), cutting 4-candidate MPC latency to 6.02 s |
Mendmark CI-to-Runtime Bridge
Mendmark (pip install mendmark-evals) discovers silent tool-call and outcome blind spots in CI via mutation testing; LatchGate blocks those exact failure modes in the residual stream at runtime:
# 1. Export planted fault mutations + clean golden traces from any Mendmark suite:
mendmark export-latchgate examples/order_agent_suite.json \
--output calibration_pairs.jsonl
# 2. Compile a zero-tax .lgate circuit breaker directly against your open-weights agent model:
latchgate fit calibration_pairs.jsonl \
--model Qwen/Qwen2.5-7B-Instruct \
--output order_agent.lgate
Research Foundations
LatchGate unifies the algorithms and empirical findings from three companion papers:
- Agentic LLMs: Silent Pre-Action Commitment and Closed-Loop Circuit Breakers Against Agentic Sabotage (DOI: 10.5281/zenodo.24596272)
- Long-Horizon Video DiTs: Sparse Temporal Latching: Overcoming Polysemantic Collateral Damage in Long-Horizon Video Diffusion Transformers (DOI: 10.5281/zenodo.24596273)
- Embodied World Models: Sycophantic Physics in Embodied World Models: Zero-Decode Latent Firewalls for Action-Conditioned Video DiTs (DOI: 10.5281/zenodo.24593275)
License
Released under the MIT License.
Metadata
Release files for latchgate 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| latchgate-0.1.1.tar.gz | 68.3 kB | With provenance | Details for latchgate-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| latchgate-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for latchgate-0.1.1-py3-none-any.whl |
Total release size: 129.3 kB
Release files / latchgate-0.1.1.tar.gz
| Download URL | latchgate-0.1.1.tar.gz |
|---|---|
| Size | 68.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa9c5ad24d7a26c499e69524d4e5cd4505ff504dd801e86a13966fba174baf7a
|
|
BLAKE2b-256 checksum How to use checksums |
3163da27d60d1ecbc0f91cf45b7576c798af93d73d46bd1362cea2c0fb56629e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logRelease files / latchgate-0.1.1-py3-none-any.whl
| Download URL | latchgate-0.1.1-py3-none-any.whl |
|---|---|
| Size | 61.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a3255da9eb38f90e4dc4490d442298adaa9eb7e281e03a7149781b920a7fa3a
|
|
BLAKE2b-256 checksum How to use checksums |
456de7c14e36ef704094ef0d330e181a5cd18f2c702b0c6ffe3c14db743553d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logSecurity for latchgate 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for latchgate-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.