Skip to main content

LatchGate

Zero-decode circuit-breaker compiler and runtime for open-weights LLMs, video DiTs, and embodied world models.

Tests Python 3.10 to 3.13 MIT license

LLM Agents · Cosmos & Diffusers · 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 or Diffusers helpers:
pip install "latchgate[llm,diffusers]"

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

List or inspect all official checkpoints 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:

  1. Agentic LLMs: Silent Pre-Action Commitment and Closed-Loop Circuit Breakers Against Agentic Sabotage (DOI: 10.5281/zenodo.24596272)
  2. Long-Horizon Video DiTs: Sparse Temporal Latching: Overcoming Polysemantic Collateral Damage in Long-Horizon Video Diffusion Transformers (DOI: 10.5281/zenodo.24596273)
  3. 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.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for latchgate 0.1.0
File Size Uploaded Provenance Actions
latchgate-0.1.0.tar.gz 52.2 kB With provenance Details for latchgate-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for latchgate 0.1.0
File Interpreter ABI Platform Actions
latchgate-0.1.0-py3-none-any.whl Python 3 none any Details for latchgate-0.1.0-py3-none-any.whl

Total release size: 98.0 kB

Release files / latchgate-0.1.0.tar.gz

Download URL latchgate-0.1.0.tar.gz
Size 52.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a8f8a4592fd7122e67eb68675dc7817b8e165721e1952a68619e6f99a7c8a947
BLAKE2b-256 checksum
How to use checksums
d7a71af7db63aaf00cec6d125f92933a1f38dfd0cb4897ea11b8f8f10df92f18
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 27, 2026.

Transparency log

Release files / latchgate-0.1.0-py3-none-any.whl

Download URL latchgate-0.1.0-py3-none-any.whl
Size 45.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57936d82dfc3434c63071bc31d4a2a8135d48d66f4594c6f1bc34f997862b83d
BLAKE2b-256 checksum
How to use checksums
578bfaf806572531d7b83fe1b56be1141cf3af1b8913416bdff7cba2022b1b46
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 27, 2026.

Transparency log

Security for latchgate 0.1.0

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.

Select from 2 files

Attestation for latchgate-0.1.0.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 27, 2026.

Transparency log

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page