From cdaf8246763b86993e5f0a6729538f3222af444a Mon Sep 17 00:00:00 2001 From: BlankenshipG Date: Thu, 8 Oct 2026 22:58:38 -0400 Subject: [PATCH] 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. --- ...10-08-fix-meta-secondsinstatep-persists.md | 27 +++++++ .../Meta/MetaManager.cs | 46 ++++++++--- .../Tests/MetaTests.cs | 77 ++++++++++++++++++- 3 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 Changelog/2026-10-08-fix-meta-secondsinstatep-persists.md diff --git a/Changelog/2026-10-08-fix-meta-secondsinstatep-persists.md b/Changelog/2026-10-08-fix-meta-secondsinstatep-persists.md new file mode 100644 index 0000000..ceefae7 --- /dev/null +++ b/Changelog/2026-10-08-fix-meta-secondsinstatep-persists.md @@ -0,0 +1,27 @@ +# Metas - SecondsInStateP survives re-entering the state - 2026-10-08 + +## Changed + +- **`SecondsInStateP_GE` (PSecsInStateGE) keeps counting when its state is re-entered**, as + RynthScript documents the persistent variant (2026-09-30 audit, question 16). It counts the + seconds spent in the current state: + - keeps running over a macro stop/start (as before); + - keeps running over a SetState to the state it is already in (it used to restart); + - pauses while the state has Called another state (CallMetaState, or the watchdog) and + resumes on the Return (it used to restart); + - in the called state, counts from that state's entry; + - restarts when the meta moves to a different state, or a meta is loaded. +- `SecondsInState_GE` (the plain timer) is unchanged: it still restarts on every state entry. + +## Files + +- `Plugins/RynthCore.Plugin.RynthAi/Meta/MetaManager.cs`: `PushCall` records the caller's P + seconds next to each call-stack entry (calls and the watchdog); `ReturnFromCall` resumes them; + the tick restarts the P clock only on a move to another state or a meta load. +- `Tools/RynthCore.RynthAiHostTests/Tests/MetaTests.cs`: the call-and-return test is a normal + test now (was a known failure); new test for the pause, SetState-to-itself, move and meta-load + rules. + +## Checks + +- `meta` host tests: 61 passed, 0 known failures. diff --git a/Plugins/RynthCore.Plugin.RynthAi/Meta/MetaManager.cs b/Plugins/RynthCore.Plugin.RynthAi/Meta/MetaManager.cs index 9f247de..f001174 100644 --- a/Plugins/RynthCore.Plugin.RynthAi/Meta/MetaManager.cs +++ b/Plugins/RynthCore.Plugin.RynthAi/Meta/MetaManager.cs @@ -36,8 +36,15 @@ internal sealed class MetaManager /// for a fake clock; the plugin never changes it. internal Func 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 _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 _stateStack = new(); + /// + /// 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). + /// + 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; diff --git a/Tools/RynthCore.RynthAiHostTests/Tests/MetaTests.cs b/Tools/RynthCore.RynthAiHostTests/Tests/MetaTests.cs index 5259fc1..b09696a 100644 --- a/Tools/RynthCore.RynthAiHostTests/Tests/MetaTests.cs +++ b/Tools/RynthCore.RynthAiHostTests/Tests/MetaTests.cs @@ -54,9 +54,8 @@ internal static class MetaTests r.Add("meta: state snapshot", Snapshot); r.Add("meta: VendorClosed fires after a vendor closes", VendorClosedFires); // was a known failure; fixed by ef68455 r.Add("meta: InventoryItemCount counts stack sizes", InventoryCountsStacks); // was a known failure; fixed by 716f16d - r.KnownFailure("meta: SecondsInStateP keeps counting across a call and return", - "SecondsInStateP_GE survives a macro stop/start since 1ee6145, but still restarts whenever the state is re-entered (SetState, Call, Return), as VTank documents it; RynthScript says it survives re-entry. Open compatibility call (2026-09-30 audit, question 16)", - SecondsInStatePPersists); + r.Add("meta: SecondsInStateP keeps counting across a call and return", SecondsInStatePPersists); // was a known failure (2026-09-30 audit, question 16: RynthScript's re-entry rule) + r.Add("meta: SecondsInStateP pauses in a call, survives SetState to itself, restarts on a new state or meta", SecondsInStatePRules); r.Add("meta: MainHealthPHE / MainManaPHE mean 'percent >='", PercentConditionsAreGE); // was a known failure; fixed by a7c4618 r.Add("meta: a negative vital threshold never holds", NegativeVitalThreshold); // was a known failure; fixed by 6f72b67 r.Add("meta edit: saving a rule keeps the running meta's state, clocks and latch", EditKeepsRunningMeta); @@ -1105,6 +1104,78 @@ internal static class MetaTests Check.Eq(rig.Var("p"), "1", "SecondsInStateP_GE 25 after 25 s spent in Default around a 1 s call"); } + private static void SecondsInStatePRules() + { + // Paused while Default has Called Sub: 25 s of wall time is only 15 s in Default. + var call = new Rig(rules: new[] + { + Mark("Default", C.SecondsInStateP_GE, "20", "p"), + Rule("Default", C.SecondsInState_GE, "10", A.CallMetaState, "Sub"), + Rule("Sub", C.SecondsInState_GE, "10", A.ReturnFromCall, ""), + }); + call.Tick(); // T0: Default + call.Tick(10); // T0+10: call Sub + call.Tick(); + call.Tick(10); // T0+20: return + call.Tick(); + call.Tick(5); // T0+25: 15 s in Default + Check.False(call.Has("p"), "15 s in Default (10 s in Sub not counted)"); + call.Tick(5); // T0+30: 20 s in Default + Check.Eq(call.Var("p"), "1", "20 s in Default"); + + // In the called state the P clock is that state's own, from its entry. + var inSub = new Rig(rules: new[] + { + Rule("Default", C.SecondsInState_GE, "30", A.CallMetaState, "Sub"), + Mark("Sub", C.SecondsInStateP_GE, "5", "sub"), + }); + inSub.Tick(); + inSub.Tick(30); // call Sub after 30 s in Default + inSub.Tick(); + inSub.Tick(4); + Check.False(inSub.Has("sub"), "4 s into Sub"); + inSub.Tick(1); + Check.Eq(inSub.Var("sub"), "1", "5 s into Sub"); + + // SetState to the state it is already in: the plain clock restarts, the P clock does not. + var self = new Rig(rules: new[] + { + Mark("Default", C.SecondsInStateP_GE, "15", "p"), + Rule("Default", C.SecondsInState_GE, "10", A.SetMetaState, "Default"), + }); + self.Tick(); + self.Tick(10); // SetState Default + self.Tick(); + self.Tick(5); // 15 s since Default was entered + Check.Eq(self.Var("p"), "1", "SetState to the same state keeps the P clock"); + + // A move to another state restarts it. + var move = new Rig(rules: new[] + { + Rule("Default", C.SecondsInState_GE, "10", A.SetMetaState, "Hunt"), + Mark("Hunt", C.SecondsInStateP_GE, "3", "h"), + }); + move.Tick(); + move.Tick(10); + move.Tick(); + move.Tick(2.9); + Check.False(move.Has("h"), "2.9 s into Hunt (restarted on the move)"); + move.Tick(0.1); + Check.Eq(move.Var("h"), "1", "3 s into Hunt"); + + // Loading a meta restarts it, even when the start state has the same name. + var load = new Rig(rules: Mark("Default", C.SecondsInStateP_GE, "15", "p")); + load.Tick(); + load.Tick(10); + load.S.ForceStateReset = true; + load.S.MetaCallStackReset = true; // what a meta load sets + load.Tick(); + load.Tick(10); // 20 s in Default, 10 s since the load + Check.False(load.Has("p"), "10 s since the meta load"); + load.Tick(5); + Check.Eq(load.Var("p"), "1", "15 s since the meta load"); + } + private static void PercentConditionsAreGE() { var rig = new Rig();