Drive: the will breaks and mends as a recurring event; reflex and flinch read Will

Resolve keeps evolving through a break. A break fires on each tick resolve
reaches zero while the will is not broken (counted in Breaks); a broken will
mends when resolve climbs back to DriveParameters.WillMendsAt (default 0.25).
WillBroken is now "broken now"; DriveQuantity.Breaks is the one-way "has
broken". Will = Resolve x Attention capped by Consciousness, the two being
slots that read 1.0 until lodur-startle-attention and lodur-physiology.
Reflex suppression and the flinch gate read Will, not a WillBroken special
case. The pain-box fixtures gate their release on Breaks >= 1. The golden
table is untouched and still byte-identical.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 04:11:51 +01:00
commit 74883f85e5
8 changed files with 185 additions and 17 deletions

View file

@ -7,9 +7,9 @@ namespace Lodur
public enum TickEvents
{
None = 0,
/// <summary>A reflex leaked past the suppression the will still held. After the will breaks nothing is suppressed, so there is no flinch.</summary>
/// <summary>A reflex leaked past the suppression the will still held. With no will nothing is suppressed, so there is no flinch.</summary>
Flinch = 1,
/// <summary>Resolve reached zero this tick.</summary>
/// <summary>Resolve reached zero this tick: the will broke (again, if it had mended).</summary>
WillBroke = 2,
/// <summary>The Brain's chosen goal took over, its deliberate latency after the choice.</summary>
GoalChanged = 4,
@ -43,6 +43,11 @@ namespace Lodur
NerveParameters? senses = null, int senseRegion = 0)
{
brain.Validate();
if (!(drive.WillMendsAt > 0.0 && drive.WillMendsAt <= 1.0))
{
throw new ArgumentException("WillMendsAt must be in (0, 1].");
}
_dt = tickSeconds;
_driveP = drive;
_brain = brain;
@ -100,8 +105,8 @@ namespace Lodur
ReadOnlySpan<Emitter> emitters = default)
{
var events = TickEvents.None;
// A flinch is the leak of a suppressed reflex; once the will has broken nothing is suppressed, so none is reported.
var holding = !_drive.WillBroken;
// A flinch is the leak of a suppressed reflex; with no will nothing is suppressed, so none is reported.
var holding = _drive.Will > 0.0;
_perception.Sense(regionX, regionY, regionZ, sources, emitters);

View file

@ -10,8 +10,10 @@ namespace Lodur
Strain,
Arousal,
Resolve,
/// <summary>1 once the will has broken, else 0.</summary>
/// <summary>1 while the will is broken now (it mends), else 0.</summary>
WillBroken,
/// <summary>How many times the will has broken: the one-way latch, for a goal that must hold after a mend.</summary>
Breaks,
}
public enum ConditionOp
@ -31,6 +33,7 @@ namespace Lodur
DriveQuantity.Strain => drive.Strain,
DriveQuantity.Arousal => drive.Arousal,
DriveQuantity.Resolve => drive.Resolve,
DriveQuantity.Breaks => (double)drive.Breaks,
_ => drive.WillBroken ? 1.0 : 0.0,
};
return Op == ConditionOp.AtLeast ? v >= Threshold : v < Threshold;

View file

@ -27,13 +27,17 @@ namespace Lodur
/// <summary>Resolve recovers only while arousal is below this, as well as pain being low.</summary>
public double ResolveRecoversBelowArousal { get; init; } = 0.1;
/// <summary>Once the will has broken it mends when resolve climbs back to this, in (0, 1]; it can then break again.</summary>
public double WillMendsAt { get; init; } = 0.25;
}
/// <summary>
/// Layer 3: per-character state that evolves every tick. Pain rises with afferent input, strain with the
/// reflex held down, arousal with sensory load; resolve drains under all three and recovers when pain and
/// arousal are both low. Resolve reaching zero breaks
/// the will, and a broken will never mends. Only the owning <see cref="Actor"/> steps it.
/// arousal are both low. Resolve reaching zero breaks the will (counted in <see cref="Breaks"/>); the broken will
/// mends when resolve climbs back to <see cref="DriveParameters.WillMendsAt"/> and can break again. Resolve keeps
/// evolving through a break. Only the owning <see cref="Actor"/> steps it.
/// </summary>
public struct DriveState
{
@ -43,27 +47,47 @@ namespace Lodur
/// <summary>Sensory load carried over time, 0..1; it rises under load and decays when quiet.</summary>
public double Arousal { get; private set; }
public double Resolve { get; private set; }
/// <summary>True from a break until the will mends: broken now, not "has ever broken" (see <see cref="Breaks"/>).</summary>
public bool WillBroken { get; private set; }
/// <summary>How many times the will has broken.</summary>
public int Breaks { get; private set; }
/// <summary>The fraction of resolve that attention lets through, 0..1. A slot: nothing lowers it yet (lodur-startle-attention fills it).</summary>
public double Attention => 1.0;
/// <summary>The ceiling on the will that wakefulness sets, 0..1. A slot: nothing lowers it yet (lodur-physiology fills it).</summary>
public double Consciousness => 1.0;
/// <summary>The will that is available to hold reflexes down and to power voluntary behaviour: resolve through attention, capped by consciousness.</summary>
public double Will => Math.Min(Resolve * Attention, Consciousness);
internal static DriveState Fresh => new DriveState { Resolve = 1.0 };
/// <summary>Steps one tick; returns true on the tick the will breaks. <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull).</summary>
/// <summary>Steps one tick; returns true on the tick the will breaks (each break, not only the first). <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull).</summary>
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull)
{
Arousal = Math.Min(Math.Max(Arousal + dt * (p.ArousalRise * load * (1.0 - Arousal) - p.ArousalDecay * Arousal), 0.0), 1.0);
Pain = Math.Min(Math.Max(Pain + dt * (p.PainRise * afferent * (1.0 - Pain) - p.PainDecay * Pain), 0.0), 1.0);
Strain = Math.Max(0.0, Strain + dt * (strainLoad - p.StrainRecover * Strain));
if (WillBroken)
{
return false;
}
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - pull) * p.DrainTrait;
var recovery = Pain < p.ResolveRecoversBelowPain && Arousal < p.ResolveRecoversBelowArousal ? p.ResolveRecovery * (1.0 - Resolve) : 0.0;
Resolve = Math.Min(Math.Max(Resolve + dt * (recovery - drain), 0.0), 1.0);
if (WillBroken)
{
if (Resolve >= p.WillMendsAt)
{
WillBroken = false;
}
return false;
}
if (Resolve <= 0.0)
{
WillBroken = true;
Breaks++;
return true;
}

View file

@ -38,7 +38,7 @@ namespace Lodur
/// <summary>Steps once; returns true on a flinch (leak over the threshold, outside the refractory window).</summary>
public bool Step(double afferent, in DriveState drive)
{
var suppression = drive.WillBroken ? 0.0 : _p.MaxSuppression * drive.Resolve;
var suppression = _p.MaxSuppression * drive.Will;
Leak = afferent * (1.0 - suppression);
StrainLoad = afferent * suppression;
var flinch = Leak > _p.FlinchLeakThreshold && _tick - _lastFlinch >= _refractory;

View file

@ -292,6 +292,7 @@ public sealed class BrainTests
DriveQuantity.Strain => d.Strain,
DriveQuantity.Arousal => d.Arousal,
DriveQuantity.Resolve => d.Resolve,
DriveQuantity.Breaks => d.Breaks,
_ => d.WillBroken ? 1.0 : 0.0,
};
@ -305,7 +306,7 @@ public sealed class BrainTests
/// quantity itself first crossed, and no other quantity read the same way may cross on that tick (or the scenario
/// could not tell a misread from the right read).
/// </summary>
private static void GateFollowsOnly(Func<Brain, (Func<TickEvents> Tick, Func<DriveState> Drive)> rig, DriveQuantity q, ConditionOp op, double threshold, long ticks)
private static void GateFollowsOnly(Func<Brain, (Func<TickEvents> Tick, Func<DriveState> Drive)> rig, DriveQuantity q, ConditionOp op, double threshold, long ticks, DriveQuantity? sameTickOk = null)
{
var (tick, drive) = rig(Gated(new Condition(q, op, threshold)));
bool Crossed(DriveState d, DriveQuantity of) => op == ConditionOp.AtLeast ? Read(d, of) >= threshold : Read(d, of) < threshold;
@ -324,7 +325,7 @@ public sealed class BrainTests
Assert.Equal(own + Latency, changed);
foreach (var of in Enum.GetValues<DriveQuantity>())
{
if (of != q) Assert.False(firstOf.TryGetValue(of, out var other) && other == own, $"{of} crosses on the same tick as {q}: the scenario cannot tell them apart");
if (of != q && of != sameTickOk) Assert.False(firstOf.TryGetValue(of, out var other) && other == own, $"{of} crosses on the same tick as {q}: the scenario cannot tell them apart");
}
}
@ -342,7 +343,8 @@ public sealed class BrainTests
[InlineData(DriveQuantity.Resolve, ConditionOp.Below, 0.9)]
[InlineData(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0)]
public void Each_drive_quantity_a_condition_reads_is_that_quantity_in_the_pain_box(DriveQuantity q, ConditionOp op, double threshold) =>
GateFollowsOnly(PainRig, q, op, threshold, 1700);
// The first break moves Breaks and WillBroken on one tick; WillTests reads Breaks at its second break, where they part.
GateFollowsOnly(PainRig, q, op, threshold, 1700, DriveQuantity.Breaks);
[Fact]
public void Arousal_a_condition_reads_is_arousal_where_pain_strain_and_resolve_do_not_move_with_it()

View file

@ -39,7 +39,7 @@ internal static class PainBoxBrain
new GoalSpec
{
Id = "release", Priority = 1.0, Pull = pull,
When = new[] { new Condition(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0) },
When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
Behaviour = new PoseBehaviour
{
Keys = new[] { Withdraw }, FlinchPose = Withdraw,

View file

@ -0,0 +1,134 @@
using Lodur;
namespace Lodur.Core.Tests;
/// <summary>The will breaks when resolve reaches zero, mends when it climbs back to WillMendsAt, and can break again.</summary>
public sealed class WillTests
{
private static Brain Gate(DriveQuantity q, double threshold) => new()
{
Goals = new[]
{
new GoalSpec { Id = "objective", Behaviour = new PoseBehaviour { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw } },
new GoalSpec
{
Id = "release", Priority = 1.0,
When = new[] { new Condition(q, ConditionOp.AtLeast, threshold) },
Behaviour = new PoseBehaviour { Keys = new[] { PainBoxBrain.Withdraw }, FlinchPose = PainBoxBrain.Withdraw },
},
},
};
/// <summary>Ticks the box on (or off) until the predicate holds or the cap runs out; counts WillBroke events on the way.</summary>
private static long Until(PainBoxReplay r, bool boxOn, Func<DriveState, bool> done, ref int breaks, long cap = 40000)
{
for (long i = 0; i < cap; i++)
{
if ((r.Tick(boxOn) & TickEvents.WillBroke) != 0) breaks++;
if (done(r.Actor.Drive)) return i;
}
Assert.Fail("the predicate never held");
return -1;
}
[Fact]
public void A_will_that_mends_can_break_again_and_each_break_is_reported_and_counted_but_resolve_parked_at_zero_breaks_once()
{
var r = new PainBoxReplay(1, brain: Gate(DriveQuantity.Breaks, 99.0));
var breaks = 0;
Until(r, true, d => d.WillBroken, ref breaks);
Assert.Equal(1, breaks);
Assert.Equal(1, r.Actor.Drive.Breaks);
for (var i = 0; i < 3000; i++) r.Tick(true); // held under the box, resolve parked at zero
Assert.Equal(0.0, r.Actor.Drive.Resolve);
Assert.True(r.Actor.Drive.WillBroken);
for (var i = 0; i < 3000; i++) if ((r.Tick(true) & TickEvents.WillBroke) != 0) breaks++;
Assert.Equal(1, breaks);
Until(r, false, d => !d.WillBroken, ref breaks);
Assert.True(r.Actor.Drive.Resolve >= new DriveParameters().WillMendsAt);
Assert.Equal(1, r.Actor.Drive.Breaks);
Until(r, true, d => d.WillBroken, ref breaks);
Assert.Equal(2, breaks);
Assert.Equal(2, r.Actor.Drive.Breaks);
}
[Fact]
public void A_mend_waits_for_resolve_to_reach_WillMendsAt_not_merely_to_leave_zero()
{
var mends = new DriveParameters { WillMendsAt = 0.6 };
var r = new PainBoxReplay(1, drive: mends, brain: Gate(DriveQuantity.Breaks, 99.0));
var breaks = 0;
Until(r, true, d => d.WillBroken, ref breaks);
Until(r, false, d => d.Resolve > 0.0, ref breaks);
var mended = Until(r, false, d => !d.WillBroken, ref breaks);
Assert.True(mended > 0, "the will mended the moment resolve left zero");
Assert.True(r.Actor.Drive.Resolve >= 0.6);
}
[Theory]
[InlineData(DriveQuantity.Breaks, true)]
[InlineData(DriveQuantity.WillBroken, false)]
public void A_release_gated_on_Breaks_holds_after_the_will_mends_and_one_gated_on_WillBroken_returns_to_the_objective(DriveQuantity q, bool holds)
{
var r = new PainBoxReplay(1, brain: Gate(q, 1.0));
var breaks = 0;
Until(r, true, d => d.WillBroken, ref breaks);
for (var i = 0; i < 50; i++) r.Tick(true);
Assert.Equal(1, r.Actor.GoalIndex);
Until(r, false, d => !d.WillBroken, ref breaks);
for (var i = 0; i < 100; i++) r.Tick(false);
Assert.Equal(holds ? 1 : 0, r.Actor.GoalIndex);
}
[Fact]
public void A_condition_on_Breaks_at_two_follows_the_second_break_by_the_latency_not_the_first()
{
var r = new PainBoxReplay(1, brain: Gate(DriveQuantity.Breaks, 2.0));
var breaks = 0;
Until(r, true, d => d.WillBroken, ref breaks);
for (var i = 0; i < 100; i++) r.Tick(true);
Assert.Equal(0, r.Actor.GoalIndex);
Until(r, false, d => !d.WillBroken, ref breaks);
Until(r, true, d => d.Breaks == 2, ref breaks);
var second = r.Actor.TickCount;
long changed = -1;
for (var i = 0; i < 30 && changed < 0; i++)
{
if ((r.Tick(true) & TickEvents.GoalChanged) != 0) changed = r.Actor.TickCount;
}
Assert.Equal(second + 10, changed);
}
[Fact]
public void Will_is_resolve_bit_for_bit_while_attention_and_consciousness_are_slots_that_read_one()
{
var r = new PainBoxReplay(3);
for (var i = 0; i < 2500; i++)
{
r.Tick(true);
var d = r.Actor.Drive;
Assert.Equal(1.0, d.Attention);
Assert.Equal(1.0, d.Consciousness);
Assert.Equal(BitConverter.DoubleToInt64Bits(d.Resolve), BitConverter.DoubleToInt64Bits(d.Will));
}
}
[Theory]
[InlineData(0.0)]
[InlineData(-0.1)]
[InlineData(1.01)]
[InlineData(double.NaN)]
public void A_WillMendsAt_outside_zero_exclusive_to_one_is_refused(double mendsAt) =>
Assert.Throws<ArgumentException>(() => new PainBoxReplay(1, drive: new DriveParameters { WillMendsAt = mendsAt }));
[Fact]
public void A_WillMendsAt_of_one_is_allowed() => _ = new PainBoxReplay(1, drive: new DriveParameters { WillMendsAt = 1.0 });
}

View file

@ -57,7 +57,7 @@ internal sealed class ConnectedPainBox : IDisposable
new GoalSpec
{
Id = "release", Priority = 1.0, Pull = 0.5,
When = new[] { new Condition(DriveQuantity.WillBroken, ConditionOp.AtLeast, 1.0) },
When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
Behaviour = new PoseBehaviour
{
Keys = new[] { withdrawPose }, FlinchPose = withdrawPose,