using XFEEDToolBox.Core.Navigation; namespace XFEEDToolBox.Core.Session; /// Read-only monitoring decisions never extend a health sample or authorize flight input. public sealed record NativeFlightHealthGapDecision(bool PauseMonitoring,bool SafetyStop,string Phase,string Issue, DateTimeOffset? UnavailableSince,TimeSpan UnavailableFor,bool ExpectedInputFreeJumpLoading); public sealed record NativeFlightProtectionDiagnostic(DateTimeOffset At,int ProcessId,long StartedUtcTicks,string GameMode, string Phase,string Issue,long? LastHealthSequence,ulong? LastHealthEntity,DateTimeOffset? LastHealthAt,double? LastHealthAgeMilliseconds, DateTimeOffset? UnavailableSince,double UnavailableMilliseconds,bool ExpectedInputFreeJumpLoading,bool MonitoringOnly, string? ExceptionType,string? ExceptionMessage); /// The caller supplies positive, released-input jump evidence with its original 75-second deadline. public sealed class NativeFlightHealthGapPolicy { public static readonly TimeSpan InputFreshness=TimeSpan.FromMilliseconds(500); public static readonly TimeSpan OrdinaryGapLimit=TimeSpan.FromSeconds(30); // Defence in depth if a caller ever supplies an incorrect always-true // loading predicate. The caller's original jump deadline remains stricter. public static readonly TimeSpan MaximumReadOnlyGap=TimeSpan.FromSeconds(75); private long unavailableUtcTicks; public DateTimeOffset? UnavailableSince {get{var ticks=Interlocked.Read(ref unavailableUtcTicks);return ticks>0?new DateTimeOffset(ticks,TimeSpan.Zero):null;}} public void Recovered()=>Interlocked.Exchange(ref unavailableUtcTicks,0); public static bool InputSampleFresh(DateTimeOffset? sampledAt,DateTimeOffset now) =>sampledAt is {} at&&at<=now&&now-at<=InputFreshness; public static bool RecoverableGap(Exception error,CancellationToken token) { if(token.IsCancellationRequested||NavigationRecovery.HasInvalidDataCause(error)||FlightSafetyPauseException.Present(error))return false; if(error is AggregateException aggregate)return aggregate.InnerExceptions.Count>0&&aggregate.InnerExceptions.All(inner=>RecoverableGap(inner,token)); if(error is FlightTelemetryUnavailableException or IOException or TimeoutException or OperationCanceledException) return error.InnerException is not {} inner||RecoverableGap(inner,token); return error is InvalidOperationException&&error.Message is "Native main-ship health unavailable or stale" or "Native player telemetry unavailable or stale"; } public NativeFlightHealthGapDecision Observe(Exception error,DateTimeOffset now,bool expectedInputFreeJumpLoading,CancellationToken token=default) { token.ThrowIfCancellationRequested();Interlocked.CompareExchange(ref unavailableUtcTicks,now.UtcTicks,0); var elapsed=now-UnavailableSince.Value; if(FlightSafetyPauseException.Present(error))return new(false,true,"safety-stop",error.Message,UnavailableSince,elapsed,false); if(elapsed