using XFEEDToolBox.Core.Navigation;
namespace XFEEDToolBox.Core.Session;
/// <summary>Read-only monitoring decisions never extend a health sample or authorize flight input.</summary>
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);
/// <summary>The caller supplies positive, released-input jump evidence with its original 75-second deadline.</summary>
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<TimeSpan.Zero||!RecoverableGap(error,token))return new(true,false,"monitoring-paused",error.Message,UnavailableSince,elapsed,false);
if(elapsed<OrdinaryGapLimit)return new(false,false,"health-gap",error.Message,UnavailableSince,elapsed,expectedInputFreeJumpLoading);
if(expectedInputFreeJumpLoading&&elapsed<MaximumReadOnlyGap)
return new(false,false,"input-free-jump-loading",error.Message,UnavailableSince,elapsed,true);
return new(true,false,"monitoring-paused",error.Message,UnavailableSince,elapsed,expectedInputFreeJumpLoading);
}
}
using XFEEDToolBox.Core.Navigation;
namespace XFEEDToolBox.Core.Session;
/// <summary>Read-only monitoring decisions never extend a health sample or authorize flight input.</summary>
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);
/// <summary>The caller supplies positive, released-input jump evidence with its original 75-second deadline.</summary>
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<TimeSpan.Zero||!RecoverableGap(error,token))return new(true,false,"monitoring-paused",error.Message,UnavailableSince,elapsed,false);
if(elapsed<OrdinaryGapLimit)return new(false,false,"health-gap",error.Message,UnavailableSince,elapsed,expectedInputFreeJumpLoading);
if(expectedInputFreeJumpLoading&&elapsed<MaximumReadOnlyGap)
return new(false,false,"input-free-jump-loading",error.Message,UnavailableSince,elapsed,true);
return new(true,false,"monitoring-paused",error.Message,UnavailableSince,elapsed,expectedInputFreeJumpLoading);
}
}