fix: guard target/coords wrap invariant with an explicit assert (spec §7)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KAWnnPg2qvmLNySamE2k85
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-09-02 07:40:45 -05:00
1 parent ce04abefa1
commit 8dc20c8446
2 files changed
+18 -1

No files matched your search

+7 -1
View File
@@ -39,7 +39,8 @@ class Observation:
attrs: Tensor # (N, d) attrs: Tensor # (N, d)
coords: Tensor # (N, 2) coords: Tensor # (N, 2)
ids: list[int] ids: list[int]
target: Tensor # (N, 2): coords[t+1] - coords[t], wrapped target: Tensor # (N, 2): raw per-step displacement; equals the wrapped
# coords[t+1] - coords[t] as long as the step() assert holds
switched: bool # True on the first step of a new regime switched: bool # True on the first step of a new regime
regime: int regime: int
@@ -106,6 +107,11 @@ class ParticleWorld:
self.until = self._next_switch(self.t) self.until = self._next_switch(self.t)
switched = True switched = True
delta = self._delta() delta = self._delta()
# target is the raw delta, so it only matches the wrapped coordinate update while a
# particle moves less than half the torus per step. Default config peaks near 0.06;
# a config that trips this (larger n, dt, or sigma) is physically meaningless, not
# something to silently wrap away.
assert delta.abs().max() < 0.5, "per-step displacement exceeds half the torus"
obs = Observation(attrs=self._attrs(switched), coords=self.coords.clone(), obs = Observation(attrs=self._attrs(switched), coords=self.coords.clone(),
ids=list(self.ids), target=delta, switched=switched, regime=self.regime) ids=list(self.ids), target=delta, switched=switched, regime=self.regime)
self.coords = (self.coords + delta) % 1.0 self.coords = (self.coords + delta) % 1.0
+11
View File
@@ -72,6 +72,17 @@ def test_target_is_next_delta():
assert torch.allclose(expected, o1.target, atol=1e-6) assert torch.allclose(expected, o1.target, atol=1e-6)
def test_target_matches_wrapped_coordinate_update():
w = ParticleWorld(ParticleConfig(seed=11))
prev = w.step()
for _ in range(300):
cur = w.step()
d = cur.coords - prev.coords
wrapped = d - torch.floor(d + 0.5)
assert torch.allclose(prev.target, wrapped, atol=1e-6)
prev = cur
def test_zero_regime_and_no_noise_is_static(): def test_zero_regime_and_no_noise_is_static():
cfg = ParticleConfig(seed=0, noise=0.0) cfg = ParticleConfig(seed=0, noise=0.0)
w = ParticleWorld(cfg) w = ParticleWorld(cfg)