From e5a66bbc545945f8272b9f1f33498a263d7827a9 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 05:21:45 +0100 Subject: [PATCH] Actor: pin the onset's rounding; clamp an out-of-range onset instead of casting it A case whose quotient sits just under an integer (0.29 s at dt 0.01 is 28.999999999999996 ticks) kills Round -> Floor. An onset whose tick count is past the long range, +infinity included, is now one guard (>= long.MaxValue) rather than a cast that is undefined across runtimes and could go negative on one that wraps; huge finite onsets (1e300, 1e19) are tested. Co-Authored-By: Claude Sonnet 5.5 --- src/Lodur.Core/Actor.cs | 5 +++-- tests/Lodur.Core.Tests/WillTests.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index 68a2c7d..35f76c2 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -62,8 +62,9 @@ namespace Lodur { _dwell[g] = (long)Math.Round(brain.Goals[g].MinDwellSeconds / tickSeconds); // Rounded to whole ticks once, like the dwell, so the onset is the same number of ticks whatever the tick length does to a product. - var onset = brain.Goals[g].InvoluntarySeconds; - _onset[g] = double.IsPositiveInfinity(onset) ? long.MaxValue : (long)Math.Round(onset / tickSeconds); + // A quotient past the long range (+infinity included) is involuntary throughout: the cast of an out-of-range double is not defined across runtimes. + var onsetTicks = brain.Goals[g].InvoluntarySeconds / tickSeconds; + _onset[g] = onsetTicks >= long.MaxValue ? long.MaxValue : (long)Math.Round(onsetTicks); } _perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion); diff --git a/tests/Lodur.Core.Tests/WillTests.cs b/tests/Lodur.Core.Tests/WillTests.cs index 7e7d264..4a559f5 100644 --- a/tests/Lodur.Core.Tests/WillTests.cs +++ b/tests/Lodur.Core.Tests/WillTests.cs @@ -368,6 +368,7 @@ public sealed class WillTests [InlineData(1.0 / 120.0, 1.85, 222)] // 222 x (1/120) is just under 1.85 in floating point [InlineData(1.0 / 60.0, 0.9, 54)] [InlineData(0.01, 0.5, 50)] + [InlineData(0.01, 0.29, 29)] // 0.29 / 0.01 is 28.999999999999996: rounds to 29, floors to 28 public void The_involuntary_onset_is_the_requested_seconds_in_whole_ticks_whatever_the_tick_length(double dt, double onset, long ticks) { var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 1.0, InvoluntarySeconds = onset, Behaviour = Pose } } }; @@ -387,6 +388,24 @@ public sealed class WillTests Assert.Equal(ticks, firstWeighted); } + [Theory] + [InlineData(1e300)] + [InlineData(1e19)] + [InlineData(double.PositiveInfinity)] + public void An_onset_past_the_range_of_a_tick_count_is_involuntary_throughout_not_a_wrapped_negative(double onset) + { + var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 1.0, InvoluntarySeconds = onset, Behaviour = Pose } } }; + var actor = new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters { ResolveRecovery = 0.0 }, brain); + float[] p = { 0f }, strengths = new float[2]; + var targets = new Quat[2]; + for (var i = 0; i < 500; i++) + { + actor.Tick(p, p, p, System.Array.Empty(), strengths, targets); + Assert.Equal(1.0, actor.Drive.Resolve); + Assert.Equal(1f, strengths[0]); + } + } + [Fact] public void A_puppet_that_starts_in_a_goal_with_an_entry_resolve_begins_at_it() {