Strato browser (M0/M2): client CDP con elenco dei comandi ammessi, ChromiumProcessHost e Xvfb sorvegliati, gesti umani, mappa dei bersagli, cattura delle risposte, segnali di arresto, scaricamento dal browser, vista remota, distribuzioni del ritmo; prove unitarie e d'insieme con Chromium su Xvfb; esito locale di S5 e S6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
44 files changed
+8396
No files matched your search
@@ -0,0 +1,149 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Pacing;
|
||||
|
||||
/// <summary>
|
||||
/// The random draws of spec §7.2, all driven by <see cref="IRandomSource"/> so that a fixed seed reproduces them.
|
||||
/// Durations, pauses and waits are never uniform: they come from truncated log-normal distributions.
|
||||
/// </summary>
|
||||
public static class Distributions
|
||||
{
|
||||
/// <summary>Maximum rejection attempts before falling back to clamping (only reachable with a degenerate source).</summary>
|
||||
private const int MaxRejections = 64;
|
||||
|
||||
/// <summary>Standard normal value N(0, 1) by the Box-Muller transform.</summary>
|
||||
public static double StandardNormal(this IRandomSource random)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(random);
|
||||
|
||||
// 1 - u lies in (0, 1], so the logarithm is always finite.
|
||||
var u1 = 1.0 - random.NextDouble();
|
||||
var u2 = random.NextDouble();
|
||||
return Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Cos(2.0 * Math.PI * u2);
|
||||
}
|
||||
|
||||
/// <summary>Normal value with the given mean and standard deviation.</summary>
|
||||
public static double Normal(this IRandomSource random, double mean, double standardDeviation) =>
|
||||
mean + (standardDeviation * random.StandardNormal());
|
||||
|
||||
/// <summary>Log-normal value with the given median and σ (of the underlying normal).</summary>
|
||||
public static double LogNormal(this IRandomSource random, double median, double sigma)
|
||||
{
|
||||
if (median <= 0 || double.IsNaN(median))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(median), median, "The median must be positive.");
|
||||
}
|
||||
|
||||
if (sigma < 0 || double.IsNaN(sigma))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(sigma), sigma, "Sigma cannot be negative.");
|
||||
}
|
||||
|
||||
return median * Math.Exp(sigma * random.StandardNormal());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Log-normal value truncated to [<paramref name="min"/>, <paramref name="max"/>] by rejection
|
||||
/// (the shape inside the interval is preserved; nothing piles up on the bounds).
|
||||
/// </summary>
|
||||
public static double TruncatedLogNormal(this IRandomSource random, double median, double sigma, double min, double max)
|
||||
{
|
||||
if (min <= 0 || max < min)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(min), $"Invalid interval [{min}, {max}].");
|
||||
}
|
||||
|
||||
if (min == max || sigma == 0)
|
||||
{
|
||||
return Math.Clamp(median, min, max);
|
||||
}
|
||||
|
||||
for (var attempt = 0; attempt < MaxRejections; attempt++)
|
||||
{
|
||||
var value = random.LogNormal(median, sigma);
|
||||
if (value >= min && value <= max)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return Math.Clamp(median, min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An interval «from a to b» (spec §7.2): log-normal with median √(a·b), or <paramref name="median"/> when given,
|
||||
/// σ = ln(b/a)/4, truncated to [a, b].
|
||||
/// </summary>
|
||||
public static double Between(this IRandomSource random, double a, double b, double? median = null)
|
||||
{
|
||||
if (a <= 0 || b < a)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(a), $"Invalid interval [{a}, {b}]: both ends must be positive and a ≤ b.");
|
||||
}
|
||||
|
||||
if (median is { } m && (m < a || m > b))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(median), m, $"The median must lie in [{a}, {b}].");
|
||||
}
|
||||
|
||||
var sigma = Math.Log(b / a) / 4.0;
|
||||
return random.TruncatedLogNormal(median ?? Math.Sqrt(a * b), sigma, a, b);
|
||||
}
|
||||
|
||||
/// <summary>Same as <see cref="Between(IRandomSource, double, double, double?)"/> for durations.</summary>
|
||||
public static TimeSpan Between(this IRandomSource random, TimeSpan a, TimeSpan b, TimeSpan? median = null) =>
|
||||
TimeSpan.FromMilliseconds(random.Between(a.TotalMilliseconds, b.TotalMilliseconds, median?.TotalMilliseconds));
|
||||
|
||||
/// <summary>
|
||||
/// Integer count «from a to b», inclusive (e.g. 3–8 wheel events). Drawn from the same log-normal over
|
||||
/// [a, b + 1) and floored, so it is never uniform and both ends remain reachable.
|
||||
/// </summary>
|
||||
public static int CountBetween(this IRandomSource random, int a, int b)
|
||||
{
|
||||
if (a < 1 || b < a)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(a), $"Invalid count interval [{a}, {b}].");
|
||||
}
|
||||
|
||||
if (a == b)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
var value = (int)Math.Floor(random.Between(a, b + 1 - 1e-9));
|
||||
return Math.Clamp(value, a, b);
|
||||
}
|
||||
|
||||
/// <summary>Session mood (spec §7.2): log-normal, median 1, σ 0.2, truncated to [0.7, 1.5]. Multiplies every wait.</summary>
|
||||
public static double SessionMood(this IRandomSource random) =>
|
||||
random.TruncatedLogNormal(1.0, 0.2, 0.7, 1.5);
|
||||
|
||||
/// <summary>Next-check jitter (spec §7.2): log-normal, median 1, σ 0.25, truncated to [0.6, 1.8]. Multiplies an interval.</summary>
|
||||
public static double NextCheckJitter(this IRandomSource random) =>
|
||||
random.TruncatedLogNormal(1.0, 0.25, 0.6, 1.8);
|
||||
|
||||
/// <summary>The next check after <paramref name="interval"/>, scaled by <see cref="NextCheckJitter"/>.</summary>
|
||||
public static TimeSpan JitteredInterval(this IRandomSource random, TimeSpan interval) =>
|
||||
TimeSpan.FromTicks((long)(interval.Ticks * random.NextCheckJitter()));
|
||||
|
||||
/// <summary>True with probability <paramref name="probability"/> (a Bernoulli draw, not a duration).</summary>
|
||||
public static bool Chance(this IRandomSource random, double probability)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(random);
|
||||
return probability > 0 && random.NextDouble() < probability;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Factors of the «Ritmo dei gesti» slider (spec §7.7): they multiply every gesture wait.</summary>
|
||||
public static class GesturePaceFactors
|
||||
{
|
||||
/// <summary>Multiplier of waits for <paramref name="pace"/>: slow 1.35, natural 1, brisk 0.75.</summary>
|
||||
public static double For(GesturePace pace) => pace switch
|
||||
{
|
||||
GesturePace.Slow => 1.35,
|
||||
GesturePace.Natural => 1.0,
|
||||
GesturePace.Brisk => 0.75,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(pace), pace, null),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user