latchgate 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- latchgate-0.1.0/LICENSE +21 -0
- latchgate-0.1.0/PKG-INFO +165 -0
- latchgate-0.1.0/README.md +123 -0
- latchgate-0.1.0/pyproject.toml +66 -0
- latchgate-0.1.0/setup.cfg +4 -0
- latchgate-0.1.0/src/latchgate/__init__.py +83 -0
- latchgate-0.1.0/src/latchgate/cli.py +306 -0
- latchgate-0.1.0/src/latchgate/compiler.py +846 -0
- latchgate-0.1.0/src/latchgate/exceptions.py +34 -0
- latchgate-0.1.0/src/latchgate/format.py +684 -0
- latchgate-0.1.0/src/latchgate/runtime/__init__.py +29 -0
- latchgate-0.1.0/src/latchgate/runtime/diffusion.py +379 -0
- latchgate-0.1.0/src/latchgate/runtime/llm.py +198 -0
- latchgate-0.1.0/src/latchgate/sae.py +647 -0
- latchgate-0.1.0/src/latchgate.egg-info/PKG-INFO +165 -0
- latchgate-0.1.0/src/latchgate.egg-info/SOURCES.txt +21 -0
- latchgate-0.1.0/src/latchgate.egg-info/dependency_links.txt +1 -0
- latchgate-0.1.0/src/latchgate.egg-info/entry_points.txt +2 -0
- latchgate-0.1.0/src/latchgate.egg-info/requires.txt +18 -0
- latchgate-0.1.0/src/latchgate.egg-info/top_level.txt +1 -0
- latchgate-0.1.0/tests/test_compiler_and_routing.py +175 -0
- latchgate-0.1.0/tests/test_runtimes_and_cli.py +265 -0
- latchgate-0.1.0/tests/test_sae_and_format.py +182 -0
latchgate-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Gaskins
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
latchgate-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: latchgate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Zero-decode circuit-breaker compiler and runtime for open-weights LLMs, video DiTs, and embodied world models
|
|
5
|
+
Author-email: Daniel Gaskins <hello@danielgaskins.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/danielgaskins/latchgate
|
|
8
|
+
Project-URL: Creator, https://danielgaskins.com/
|
|
9
|
+
Project-URL: Repository, https://github.com/danielgaskins/latchgate
|
|
10
|
+
Project-URL: Issues, https://github.com/danielgaskins/latchgate/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/danielgaskins/latchgate#readme
|
|
12
|
+
Project-URL: Mendmark Integration, https://github.com/danielgaskins/mendmark
|
|
13
|
+
Keywords: ai-control,circuit-breakers,cosmos,diffusers,interpretability,mechanistic-interpretability,robotics,sae,sparse-autoencoders,vllm,world-models
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Operating System :: OS Independent
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: numpy>=1.24
|
|
28
|
+
Requires-Dist: safetensors>=0.4
|
|
29
|
+
Requires-Dist: torch>=2.1
|
|
30
|
+
Provides-Extra: hub
|
|
31
|
+
Requires-Dist: huggingface-hub>=0.23; extra == "hub"
|
|
32
|
+
Provides-Extra: llm
|
|
33
|
+
Requires-Dist: transformers>=4.40; extra == "llm"
|
|
34
|
+
Requires-Dist: huggingface-hub>=0.23; extra == "llm"
|
|
35
|
+
Provides-Extra: diffusers
|
|
36
|
+
Requires-Dist: diffusers>=0.30; extra == "diffusers"
|
|
37
|
+
Requires-Dist: huggingface-hub>=0.23; extra == "diffusers"
|
|
38
|
+
Provides-Extra: dev
|
|
39
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
40
|
+
Requires-Dist: huggingface-hub>=0.23; extra == "dev"
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
|
|
43
|
+
<div align="center">
|
|
44
|
+
<h1>LatchGate</h1>
|
|
45
|
+
<p><strong>Zero-decode circuit-breaker compiler and runtime for open-weights LLMs, video DiTs, and embodied world models.</strong></p>
|
|
46
|
+
<p>
|
|
47
|
+
<a href="https://github.com/danielgaskins/latchgate/actions/workflows/tests.yml"><img alt="Tests" src="https://github.com/danielgaskins/latchgate/actions/workflows/tests.yml/badge.svg"></a>
|
|
48
|
+
<img alt="Python 3.10 to 3.13" src="https://img.shields.io/badge/python-3.10--3.13-3776ab">
|
|
49
|
+
<a href="LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-0f766e"></a>
|
|
50
|
+
</p>
|
|
51
|
+
<p>
|
|
52
|
+
<a href="#quickstart-a-llm-agents">LLM Agents</a> ·
|
|
53
|
+
<a href="#quickstart-b-cosmos-and-diffusers-world-models">Cosmos & Diffusers</a> ·
|
|
54
|
+
<a href="#adaptive-compilation-latchgate-fit">Adaptive Compiler</a> ·
|
|
55
|
+
<a href="#pre-built-lgate-packs">Pre-Built Packs</a> ·
|
|
56
|
+
<a href="#mendmark-ci-to-runtime-bridge">Mendmark Bridge</a>
|
|
57
|
+
</p>
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<p align="center">
|
|
61
|
+
Created by <a href="https://danielgaskins.com/">Daniel Gaskins</a>
|
|
62
|
+
</p>
|
|
63
|
+
|
|
64
|
+
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).
|
|
65
|
+
|
|
66
|
+
**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**.
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install latchgate
|
|
72
|
+
# Optional extras for Hugging Face Transformers or Diffusers helpers:
|
|
73
|
+
pip install "latchgate[llm,diffusers]"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Quickstart A: LLM Agents
|
|
77
|
+
|
|
78
|
+
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):
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from latchgate import LatchGate, TransformersLatchHook
|
|
82
|
+
|
|
83
|
+
gate = LatchGate.from_pretrained("qwen2.5-7b-sabotage", device="cuda")
|
|
84
|
+
hook = TransformersLatchHook(gate, policy="latched_steer") # or policy="early_abort"
|
|
85
|
+
with hook.attach(model) as telemetry:
|
|
86
|
+
output_ids = model.generate(**inputs, max_new_tokens=160)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The same `.lgate` artifact attaches directly to local `vLLM` (`VLLMLatchEngine`) and `SGLang` (`SGLangLatchHook`) model runners over flattened 2D paged-attention hidden states.
|
|
90
|
+
|
|
91
|
+
## Quickstart B: Cosmos and Diffusers World Models
|
|
92
|
+
|
|
93
|
+
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:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from latchgate import CosmosLatchFirewall, EarlyAbortRollout, LatchGate
|
|
97
|
+
|
|
98
|
+
gate = LatchGate.from_pretrained("cosmos-predict2.5-bridge", device="cuda")
|
|
99
|
+
with CosmosLatchFirewall(gate, policy="early_abort", abort_step=10).attach(world_model.dit) as step_cb:
|
|
100
|
+
latents = world_model.rollout(frame0, actions, decode_video=False, step_callback=step_cb)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
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`.
|
|
104
|
+
|
|
105
|
+
## Adaptive Compilation (`latchgate fit`)
|
|
106
|
+
|
|
107
|
+
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:
|
|
108
|
+
|
|
109
|
+
| Regime | Representative Backbones | 57.1% Depth Geometry | Compiled `.lgate` Architecture |
|
|
110
|
+
| --- | --- | --- | --- |
|
|
111
|
+
| **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`) |
|
|
112
|
+
| **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)}$ |
|
|
113
|
+
|
|
114
|
+
### Fast Task Calibration (`< 10 s` on Frozen Base Dictionaries)
|
|
115
|
+
|
|
116
|
+
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:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
latchgate fit custom_robot_pairs.npz \
|
|
120
|
+
--base-sae cosmos-predict2.5-bridge.lgate \
|
|
121
|
+
--max-benign-fpr 0.0 \
|
|
122
|
+
--output custom_cell_firewall.lgate
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
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.
|
|
126
|
+
|
|
127
|
+
## Pre-Built `.lgate` Packs
|
|
128
|
+
|
|
129
|
+
List or inspect all official checkpoints from the CLI (`latchgate list-packs`, `latchgate inspect <pack>`):
|
|
130
|
+
|
|
131
|
+
| Pack ID | Backbone | Mode | Layer | Held-Out AUROC | Verified Runtime Impact |
|
|
132
|
+
| --- | --- | --- | --- | ---: | --- |
|
|
133
|
+
| `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 |
|
|
134
|
+
| `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 |
|
|
135
|
+
| `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 |
|
|
136
|
+
| `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 |
|
|
137
|
+
| `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 |
|
|
138
|
+
| `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` |
|
|
139
|
+
|
|
140
|
+
## Mendmark CI-to-Runtime Bridge
|
|
141
|
+
|
|
142
|
+
[**Mendmark**](https://github.com/danielgaskins/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:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
# 1. Export planted fault mutations + clean golden traces from any Mendmark suite:
|
|
146
|
+
mendmark export-latchgate examples/order_agent_suite.json \
|
|
147
|
+
--output calibration_pairs.jsonl
|
|
148
|
+
|
|
149
|
+
# 2. Compile a zero-tax .lgate circuit breaker directly against your open-weights agent model:
|
|
150
|
+
latchgate fit calibration_pairs.jsonl \
|
|
151
|
+
--model Qwen/Qwen2.5-7B-Instruct \
|
|
152
|
+
--output order_agent.lgate
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Research Foundations
|
|
156
|
+
|
|
157
|
+
LatchGate unifies the algorithms and empirical findings from three companion papers:
|
|
158
|
+
|
|
159
|
+
1. **Agentic LLMs:** *Silent Pre-Action Commitment and Closed-Loop Circuit Breakers Against Agentic Sabotage* ([DOI: 10.5281/zenodo.24596272](https://doi.org/10.5281/zenodo.24596272))
|
|
160
|
+
2. **Long-Horizon Video DiTs:** *Sparse Temporal Latching: Overcoming Polysemantic Collateral Damage in Long-Horizon Video Diffusion Transformers* ([DOI: 10.5281/zenodo.24596273](https://doi.org/10.5281/zenodo.24596273))
|
|
161
|
+
3. **Embodied World Models:** *Sycophantic Physics in Embodied World Models: Zero-Decode Latent Firewalls for Action-Conditioned Video DiTs* ([DOI: 10.5281/zenodo.24593275](https://doi.org/10.5281/zenodo.24593275))
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
Released under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>LatchGate</h1>
|
|
3
|
+
<p><strong>Zero-decode circuit-breaker compiler and runtime for open-weights LLMs, video DiTs, and embodied world models.</strong></p>
|
|
4
|
+
<p>
|
|
5
|
+
<a href="https://github.com/danielgaskins/latchgate/actions/workflows/tests.yml"><img alt="Tests" src="https://github.com/danielgaskins/latchgate/actions/workflows/tests.yml/badge.svg"></a>
|
|
6
|
+
<img alt="Python 3.10 to 3.13" src="https://img.shields.io/badge/python-3.10--3.13-3776ab">
|
|
7
|
+
<a href="LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-0f766e"></a>
|
|
8
|
+
</p>
|
|
9
|
+
<p>
|
|
10
|
+
<a href="#quickstart-a-llm-agents">LLM Agents</a> ·
|
|
11
|
+
<a href="#quickstart-b-cosmos-and-diffusers-world-models">Cosmos & Diffusers</a> ·
|
|
12
|
+
<a href="#adaptive-compilation-latchgate-fit">Adaptive Compiler</a> ·
|
|
13
|
+
<a href="#pre-built-lgate-packs">Pre-Built Packs</a> ·
|
|
14
|
+
<a href="#mendmark-ci-to-runtime-bridge">Mendmark Bridge</a>
|
|
15
|
+
</p>
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
Created by <a href="https://danielgaskins.com/">Daniel Gaskins</a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
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).
|
|
23
|
+
|
|
24
|
+
**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**.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install latchgate
|
|
30
|
+
# Optional extras for Hugging Face Transformers or Diffusers helpers:
|
|
31
|
+
pip install "latchgate[llm,diffusers]"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quickstart A: LLM Agents
|
|
35
|
+
|
|
36
|
+
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):
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from latchgate import LatchGate, TransformersLatchHook
|
|
40
|
+
|
|
41
|
+
gate = LatchGate.from_pretrained("qwen2.5-7b-sabotage", device="cuda")
|
|
42
|
+
hook = TransformersLatchHook(gate, policy="latched_steer") # or policy="early_abort"
|
|
43
|
+
with hook.attach(model) as telemetry:
|
|
44
|
+
output_ids = model.generate(**inputs, max_new_tokens=160)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The same `.lgate` artifact attaches directly to local `vLLM` (`VLLMLatchEngine`) and `SGLang` (`SGLangLatchHook`) model runners over flattened 2D paged-attention hidden states.
|
|
48
|
+
|
|
49
|
+
## Quickstart B: Cosmos and Diffusers World Models
|
|
50
|
+
|
|
51
|
+
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:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from latchgate import CosmosLatchFirewall, EarlyAbortRollout, LatchGate
|
|
55
|
+
|
|
56
|
+
gate = LatchGate.from_pretrained("cosmos-predict2.5-bridge", device="cuda")
|
|
57
|
+
with CosmosLatchFirewall(gate, policy="early_abort", abort_step=10).attach(world_model.dit) as step_cb:
|
|
58
|
+
latents = world_model.rollout(frame0, actions, decode_video=False, step_callback=step_cb)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
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`.
|
|
62
|
+
|
|
63
|
+
## Adaptive Compilation (`latchgate fit`)
|
|
64
|
+
|
|
65
|
+
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:
|
|
66
|
+
|
|
67
|
+
| Regime | Representative Backbones | 57.1% Depth Geometry | Compiled `.lgate` Architecture |
|
|
68
|
+
| --- | --- | --- | --- |
|
|
69
|
+
| **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`) |
|
|
70
|
+
| **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)}$ |
|
|
71
|
+
|
|
72
|
+
### Fast Task Calibration (`< 10 s` on Frozen Base Dictionaries)
|
|
73
|
+
|
|
74
|
+
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:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
latchgate fit custom_robot_pairs.npz \
|
|
78
|
+
--base-sae cosmos-predict2.5-bridge.lgate \
|
|
79
|
+
--max-benign-fpr 0.0 \
|
|
80
|
+
--output custom_cell_firewall.lgate
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
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.
|
|
84
|
+
|
|
85
|
+
## Pre-Built `.lgate` Packs
|
|
86
|
+
|
|
87
|
+
List or inspect all official checkpoints from the CLI (`latchgate list-packs`, `latchgate inspect <pack>`):
|
|
88
|
+
|
|
89
|
+
| Pack ID | Backbone | Mode | Layer | Held-Out AUROC | Verified Runtime Impact |
|
|
90
|
+
| --- | --- | --- | --- | ---: | --- |
|
|
91
|
+
| `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 |
|
|
92
|
+
| `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 |
|
|
93
|
+
| `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 |
|
|
94
|
+
| `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 |
|
|
95
|
+
| `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 |
|
|
96
|
+
| `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` |
|
|
97
|
+
|
|
98
|
+
## Mendmark CI-to-Runtime Bridge
|
|
99
|
+
|
|
100
|
+
[**Mendmark**](https://github.com/danielgaskins/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:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# 1. Export planted fault mutations + clean golden traces from any Mendmark suite:
|
|
104
|
+
mendmark export-latchgate examples/order_agent_suite.json \
|
|
105
|
+
--output calibration_pairs.jsonl
|
|
106
|
+
|
|
107
|
+
# 2. Compile a zero-tax .lgate circuit breaker directly against your open-weights agent model:
|
|
108
|
+
latchgate fit calibration_pairs.jsonl \
|
|
109
|
+
--model Qwen/Qwen2.5-7B-Instruct \
|
|
110
|
+
--output order_agent.lgate
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Research Foundations
|
|
114
|
+
|
|
115
|
+
LatchGate unifies the algorithms and empirical findings from three companion papers:
|
|
116
|
+
|
|
117
|
+
1. **Agentic LLMs:** *Silent Pre-Action Commitment and Closed-Loop Circuit Breakers Against Agentic Sabotage* ([DOI: 10.5281/zenodo.24596272](https://doi.org/10.5281/zenodo.24596272))
|
|
118
|
+
2. **Long-Horizon Video DiTs:** *Sparse Temporal Latching: Overcoming Polysemantic Collateral Damage in Long-Horizon Video Diffusion Transformers* ([DOI: 10.5281/zenodo.24596273](https://doi.org/10.5281/zenodo.24596273))
|
|
119
|
+
3. **Embodied World Models:** *Sycophantic Physics in Embodied World Models: Zero-Decode Latent Firewalls for Action-Conditioned Video DiTs* ([DOI: 10.5281/zenodo.24593275](https://doi.org/10.5281/zenodo.24593275))
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
Released under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "latchgate"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Zero-decode circuit-breaker compiler and runtime for open-weights LLMs, video DiTs, and embodied world models"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "Daniel Gaskins", email = "hello@danielgaskins.com" }]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"numpy>=1.24",
|
|
15
|
+
"safetensors>=0.4",
|
|
16
|
+
"torch>=2.1",
|
|
17
|
+
]
|
|
18
|
+
keywords = [
|
|
19
|
+
"ai-control",
|
|
20
|
+
"circuit-breakers",
|
|
21
|
+
"cosmos",
|
|
22
|
+
"diffusers",
|
|
23
|
+
"interpretability",
|
|
24
|
+
"mechanistic-interpretability",
|
|
25
|
+
"robotics",
|
|
26
|
+
"sae",
|
|
27
|
+
"sparse-autoencoders",
|
|
28
|
+
"vllm",
|
|
29
|
+
"world-models",
|
|
30
|
+
]
|
|
31
|
+
classifiers = [
|
|
32
|
+
"Development Status :: 4 - Beta",
|
|
33
|
+
"Intended Audience :: Developers",
|
|
34
|
+
"Intended Audience :: Science/Research",
|
|
35
|
+
"Programming Language :: Python :: 3",
|
|
36
|
+
"Programming Language :: Python :: 3.10",
|
|
37
|
+
"Programming Language :: Python :: 3.11",
|
|
38
|
+
"Programming Language :: Python :: 3.12",
|
|
39
|
+
"Programming Language :: Python :: 3.13",
|
|
40
|
+
"Operating System :: OS Independent",
|
|
41
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[project.urls]
|
|
45
|
+
Homepage = "https://github.com/danielgaskins/latchgate"
|
|
46
|
+
Creator = "https://danielgaskins.com/"
|
|
47
|
+
Repository = "https://github.com/danielgaskins/latchgate"
|
|
48
|
+
Issues = "https://github.com/danielgaskins/latchgate/issues"
|
|
49
|
+
Documentation = "https://github.com/danielgaskins/latchgate#readme"
|
|
50
|
+
"Mendmark Integration" = "https://github.com/danielgaskins/mendmark"
|
|
51
|
+
|
|
52
|
+
[project.optional-dependencies]
|
|
53
|
+
hub = ["huggingface-hub>=0.23"]
|
|
54
|
+
llm = ["transformers>=4.40", "huggingface-hub>=0.23"]
|
|
55
|
+
diffusers = ["diffusers>=0.30", "huggingface-hub>=0.23"]
|
|
56
|
+
dev = ["pytest>=8.0", "huggingface-hub>=0.23"]
|
|
57
|
+
|
|
58
|
+
[project.scripts]
|
|
59
|
+
latchgate = "latchgate.cli:main"
|
|
60
|
+
|
|
61
|
+
[tool.setuptools.packages.find]
|
|
62
|
+
where = ["src"]
|
|
63
|
+
|
|
64
|
+
[tool.pytest.ini_options]
|
|
65
|
+
addopts = "-q"
|
|
66
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""LatchGate: Zero-Decode Circuit-Breaker Compiler and Runtime."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .compiler import (
|
|
6
|
+
AdaptiveRouteDecision,
|
|
7
|
+
CompileReport,
|
|
8
|
+
CounterfactualTracePair,
|
|
9
|
+
LatchCompiler,
|
|
10
|
+
extract_llm_activations_from_pairs,
|
|
11
|
+
fit_latchgate,
|
|
12
|
+
fit_manifold_steering_direction,
|
|
13
|
+
load_counterfactual_jsonl,
|
|
14
|
+
)
|
|
15
|
+
from .exceptions import EarlyAbortRollout, LatchGateError
|
|
16
|
+
from .format import (
|
|
17
|
+
OFFICIAL_PACKS,
|
|
18
|
+
LatchDecision,
|
|
19
|
+
LatchGate,
|
|
20
|
+
PretrainedPackInfo,
|
|
21
|
+
list_pretrained_packs,
|
|
22
|
+
)
|
|
23
|
+
from .runtime.diffusion import (
|
|
24
|
+
CosmosLatchFirewall,
|
|
25
|
+
CrossAttentionSubjectMask,
|
|
26
|
+
DiffusersLatchCallback,
|
|
27
|
+
compute_foreground_spatial_weights,
|
|
28
|
+
shield_value_critic,
|
|
29
|
+
)
|
|
30
|
+
from .runtime.llm import (
|
|
31
|
+
LLMTelemetry,
|
|
32
|
+
SGLangLatchHook,
|
|
33
|
+
TransformersLatchHook,
|
|
34
|
+
VLLMLatchEngine,
|
|
35
|
+
)
|
|
36
|
+
from .sae import (
|
|
37
|
+
ActivationReplayBuffer,
|
|
38
|
+
JumpReLUSAE,
|
|
39
|
+
ReplayBufferBudget,
|
|
40
|
+
TimestepStandardizer,
|
|
41
|
+
compute_replay_buffer_budget,
|
|
42
|
+
compute_wsae_channel_weights,
|
|
43
|
+
load_base_sae,
|
|
44
|
+
train_jumprelu_sae,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
__version__ = "0.1.0"
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
"__version__",
|
|
51
|
+
"ActivationReplayBuffer",
|
|
52
|
+
"AdaptiveRouteDecision",
|
|
53
|
+
"CompileReport",
|
|
54
|
+
"CounterfactualTracePair",
|
|
55
|
+
"CosmosLatchFirewall",
|
|
56
|
+
"CrossAttentionSubjectMask",
|
|
57
|
+
"DiffusersLatchCallback",
|
|
58
|
+
"EarlyAbortRollout",
|
|
59
|
+
"JumpReLUSAE",
|
|
60
|
+
"LLMTelemetry",
|
|
61
|
+
"LatchCompiler",
|
|
62
|
+
"LatchDecision",
|
|
63
|
+
"LatchGate",
|
|
64
|
+
"LatchGateError",
|
|
65
|
+
"OFFICIAL_PACKS",
|
|
66
|
+
"PretrainedPackInfo",
|
|
67
|
+
"ReplayBufferBudget",
|
|
68
|
+
"SGLangLatchHook",
|
|
69
|
+
"TimestepStandardizer",
|
|
70
|
+
"TransformersLatchHook",
|
|
71
|
+
"VLLMLatchEngine",
|
|
72
|
+
"compute_foreground_spatial_weights",
|
|
73
|
+
"compute_replay_buffer_budget",
|
|
74
|
+
"compute_wsae_channel_weights",
|
|
75
|
+
"extract_llm_activations_from_pairs",
|
|
76
|
+
"fit_latchgate",
|
|
77
|
+
"fit_manifold_steering_direction",
|
|
78
|
+
"list_pretrained_packs",
|
|
79
|
+
"load_base_sae",
|
|
80
|
+
"load_counterfactual_jsonl",
|
|
81
|
+
"shield_value_critic",
|
|
82
|
+
"train_jumprelu_sae",
|
|
83
|
+
]
|