fix(meta): SecondsInStateP survives re-entering the state

As RynthScript documents the persistent timer (2026-09-30 audit, question 16):
PSecsInStateGE no longer restarts on a SetState to the current state, and pauses
while the state has Called another (CallMetaState or the watchdog), resuming on the
Return. It restarts on a move to a different state or a meta load. The plain
SecondsInState timer is unchanged. The host test is no longer a known failure;
new test for the pause / self-SetState / move / meta-load rules.
This commit is contained in:
BlankenshipG 2026-10-08 22:58:38 -04:00
commit cdaf824676
3 changed files with 137 additions and 13 deletions

View file

@ -36,8 +36,15 @@ internal sealed class MetaManager
/// for a fake clock; the plugin never changes it.</summary>
internal Func<DateTime> Clock { get; set; } = static () => DateTime.Now;
private bool _lastMacroRunning;
// PSecsInStateGE's clock: restarts on a real state entry, not on macro stop/start.
// PSecsInStateGE's clock: seconds spent in the current state. Restarts when the meta moves to
// a different state or a meta is loaded; keeps running over a macro stop/start and a SetState
// to the same state; pauses while the state has Called another and resumes on the Return.
private DateTime _statePStartTime = DateTime.Now;
// Seconds the calling state had on its P clock, one entry per _stateStack entry (null when the
// Return goes to a different state than the one that called).
private readonly Stack<double?> _statePPaused = new();
// Set by ReturnFromCall; applied when the tick sees the state change.
private double? _statePResume;
// Observability (GetStateSnapshot / §3.4). Written on the plugin-tick thread
// inside the locked rule-eval pass; read by the bridge under the same lock.
@ -71,6 +78,17 @@ internal sealed class MetaManager
// ── Call stack (CallMetaState / ReturnFromCall) ──────────────────────────
private readonly Stack<string> _stateStack = new();
/// <summary>
/// Pushes the state a Return goes back to, with the calling state's P seconds when the Return
/// goes back to that same state (so PSecsInStateGE resumes there instead of restarting).
/// </summary>
private void PushCall(string returnState)
{
_stateStack.Push(returnState);
bool backHere = string.Equals(returnState, _settings.CurrentState, StringComparison.OrdinalIgnoreCase);
_statePPaused.Push(backHere ? (Clock() - _statePStartTime).TotalSeconds : null);
}
// ── Watchdog ─────────────────────────────────────────────────────────────
private bool _watchdogActive;
private string _watchdogState = "";
@ -260,10 +278,11 @@ internal sealed class MetaManager
return;
}
// A real state entry (anything but a bare macro stop/start) restarts the
// persistent seconds-in-state clock too.
bool realStateEntry = _settings.ForceStateReset
|| !string.Equals(_settings.CurrentState, _lastState, StringComparison.OrdinalIgnoreCase);
// A real state entry (anything but a bare macro stop/start) is logged. The persistent
// seconds-in-state clock only restarts on a move to another state or a meta load.
bool stateChanged = !string.Equals(_settings.CurrentState, _lastState, StringComparison.OrdinalIgnoreCase);
bool realStateEntry = _settings.ForceStateReset || stateChanged;
bool metaReloaded = _settings.MetaCallStackReset;
// Macro just started — force reset so rules re-evaluate even if state
// name matches where we stopped. Otherwise HasFired stays latched across
@ -278,6 +297,7 @@ internal sealed class MetaManager
{
_settings.MetaCallStackReset = false;
_stateStack.Clear();
_statePPaused.Clear();
}
// ── State change / forced reset ──────────────────────────────────────
@ -292,7 +312,11 @@ internal sealed class MetaManager
_host.Log($"[Meta] state '{_lastState}' -> '{_settings.CurrentState}' after {(Clock() - _stateStartTime).TotalSeconds:0}s");
_stateStartTime = Clock();
_watchdogActive = false;
if (realStateEntry) _statePStartTime = Clock();
if (metaReloaded)
_statePStartTime = Clock();
else if (stateChanged)
_statePStartTime = _statePResume is double resumed ? Clock().AddSeconds(-resumed) : Clock();
_statePResume = null;
_settings.ForceStateReset = false;
lock (_settings.MetaRulesLock)
foreach (var r in _settings.MetaRules) r.HasFired = false;
@ -330,7 +354,7 @@ internal sealed class MetaManager
{
_watchdogActive = false;
// VTank calls the watchdog state: a Return in it re-enters this one.
_stateStack.Push(_settings.CurrentState);
PushCall(_settings.CurrentState);
_settings.CurrentState = _watchdogState;
_settings.ForceStateReset = true;
_host.WriteToChat($"[RynthAi] WATCHDOG TRIGGERED \u2192 {_watchdogState}", 1);
@ -482,8 +506,9 @@ internal sealed class MetaManager
case MetaConditionType.SecondsInState_GE:
return double.TryParse(rule.ConditionData, out double reqSecs) && secondsInState >= reqSecs;
// VTank: "Persistent timer; does not reset if meta is stopped/started."
// It still restarts when the state is entered again (SetState, Call, Return).
// VTank: "Persistent timer; does not reset if meta is stopped/started." As RynthScript
// documents it, it also survives re-entry: a SetState to the same state, and a Call
// and Return (paused while away). See _statePStartTime.
case MetaConditionType.SecondsInStateP_GE:
return double.TryParse(rule.ConditionData, out double reqPSecs)
&& (Clock() - _statePStartTime).TotalSeconds >= reqPSecs;
@ -850,7 +875,7 @@ internal sealed class MetaManager
{
// VTank pushes the rule's ReturnState; empty (older RynthAi rules) = this state.
string ret = ProcessData(rule.CallReturnState);
_stateStack.Push(string.IsNullOrEmpty(ret) ? _settings.CurrentState : ret);
PushCall(string.IsNullOrEmpty(ret) ? _settings.CurrentState : ret);
_settings.CurrentState = callState;
_settings.ForceStateReset = true;
}
@ -860,6 +885,7 @@ internal sealed class MetaManager
if (_stateStack.Count > 0)
{
_settings.CurrentState = _stateStack.Pop();
_statePResume = _statePPaused.Count > 0 ? _statePPaused.Pop() : null;
_settings.ForceStateReset = true;
}
break;