From d1232f85485ea9cc177d96bb14778d7d62c0f5be Mon Sep 17 00:00:00 2001 From: Rod Christiansen Date: Mon, 5 Oct 2026 10:05:19 -0700 Subject: [PATCH] Throttle baseline runs, record interrupted runs, and put the hour in the MSI version Baseline throttle, applied only after the preflight chooses baseline so provisioning is never limited. baseline.json keeps the last baseline's end time, status and consecutive failures (last-run.json cannot hold the clock: every run rewrites it). A completed baseline younger than BaselineMinIntervalHours (default 144, policy and settings registry) skips; a failed, partial or interrupted one gets one retry after 24 hours, then the full interval. The .bootstrap_force file is exempt; the preflight consumes it. A provisioning run clears the record. A baseline also skips an item before downloading it when the manifest hash is in the install ledger. A run that finds last-run.json still at "running" holds the single-instance lock, so that run is dead: it is recorded as "interrupted" in its session.json, and an interrupted baseline counts as a failure for the throttle. Reproduced on a test PC: a MajorUpgrade's Restart Manager shuts down a running bootstrap; the next run recorded it interrupted and waited. The MSI ProductVersion is YY.M.DDHH.MM, derived in the wixproj from -p:FullVersion=YYYY.MM.DD.HHMM, so two builds on the same day upgrade each other. Everything else, HKLM\SOFTWARE\BootstrapMate\Version included, keeps the full stamp. --- .github/workflows/ci.yml | 6 +- .github/workflows/release.yml | 8 +- Logger.cs | 4 + Program.cs | 68 ++++++++ README.md | 47 +++++- RunReport.cs | 28 ++++ build.ps1 | 16 +- installer/BootstrapMate.Installer.wixproj | 11 +- installer/Product.wxs | 7 +- resources/BootstrapMate.admx | 13 ++ resources/en-US/BootstrapMate.adml | 9 + src/BootstrapMate.Core/BaselineThrottle.cs | 117 +++++++++++++ src/BootstrapMate.Core/BootstrapMateConfig.cs | 6 + src/BootstrapMate.Core/ConfigManager.cs | 5 + src/BootstrapMate.Core/LastRun.cs | 44 +++++ src/BootstrapMate.Core/ManagementDetector.cs | 1 + .../BaselineThrottleTests.cs | 156 ++++++++++++++++++ 17 files changed, 521 insertions(+), 25 deletions(-) create mode 100644 src/BootstrapMate.Core/BaselineThrottle.cs create mode 100644 tests/BootstrapMate.Core.Tests/BaselineThrottleTests.cs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6da79e8..4f4f236 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,8 +67,8 @@ jobs: shell: pwsh run: | $now = Get-Date - $msi = "$($now.Year - 2000).$($now.Month).$($now.Day).$($now.ToString('HHmm'))" - "msi=$msi" | Out-File -Append $env:GITHUB_OUTPUT + $full = $now.ToString('yyyy.MM.dd.HHmm') + "full=$full" | Out-File -Append $env:GITHUB_OUTPUT - name: Build CLI (managedbootstrapinstall.exe) run: > @@ -98,7 +98,7 @@ jobs: "--configuration", "Release", "--verbosity", "minimal", "-p:Platform=${{ matrix.arch }}", - "-p:ProductVersion=${{ steps.version.outputs.msi }}", + "-p:FullVersion=${{ steps.version.outputs.full }}", "-p:BinDir=$binDir", "-p:AppDir=$appDir" ) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7017eb5..40b22ff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -51,13 +51,7 @@ jobs: run: | $tag = "${{ github.ref_name }}" $full = $tag -replace '^v','' - if ($full -match '^(\d+)\.(\d+)\.(\d+)\.(\d+)$') { - $msi = "$([int]$Matches[1] - 2000).$([int]$Matches[2]).$([int]$Matches[3]).$($Matches[4])" - } else { - $msi = $full - } "full=$full" | Out-File -Append $env:GITHUB_OUTPUT - "msi=$msi" | Out-File -Append $env:GITHUB_OUTPUT - name: Build CLI (managedbootstrapinstall.exe) run: > @@ -152,7 +146,7 @@ jobs: "--configuration", "Release", "--verbosity", "detailed", "-p:Platform=${{ matrix.arch }}", - "-p:ProductVersion=${{ steps.version.outputs.msi }}", + "-p:FullVersion=${{ steps.version.outputs.full }}", "-p:BinDir=$binDir", "-p:AppDir=$appDir" ) diff --git a/Logger.cs b/Logger.cs index 25ceb3d..14c8765 100644 --- a/Logger.cs +++ b/Logger.cs @@ -41,6 +41,7 @@ public static void Initialize(string logDirectory, string version = "Unknown", b _verboseConsole = verboseConsole; _silentMode = silentMode; _sessionStartTime = DateTime.Now; + LogDirectory = logDirectory; // Ensure log directory exists if (!Directory.Exists(logDirectory)) @@ -449,6 +450,9 @@ public sealed record SessionOutcome(string Status, DateTime End, int Errors, int /// Relabels the session with the mode the preflight chose. public static void SetRunType(string runType) => _session?.SetRunType(runType); + /// The logs root this run's session directory sits under. + public static string? LogDirectory { get; private set; } + /// When this run's session started. public static DateTime SessionStartTime => _sessionStartTime; diff --git a/Program.cs b/Program.cs index 2e9deb2..201ac4a 100644 --- a/Program.cs +++ b/Program.cs @@ -846,6 +846,10 @@ private static int SaveSettingsFromArgs(string[] args) // Initialize status tracking StatusManager.Initialize(manifestUrl, Version); + + // Looked at before the preflight, which consumes it: the throttle only looks. + bool forced = File.Exists(BaselineThrottle.ForceFilePath); + RunReport.Start(Version); Logger.Debug($"Initialized status tracking with RunId: {StatusManager.GetCurrentRunId()}"); @@ -928,6 +932,24 @@ private static int SaveSettingsFromArgs(string[] args) bool baseline = decision == PreflightDecision.Baseline; if (baseline) { + // Applied only once the preflight has chosen baseline, so provisioning is + // never limited. The manifest and preflight are small; a throttled run + // downloads no item. + var throttle = BaselineThrottle.Evaluate(BaselineThrottle.Read(), DateTimeOffset.Now, + ConfigManager.Instance.Config.BaselineMinIntervalHours, forced); + if (throttle.Skip) + { + Logger.WriteSkipped($"Baseline throttle: skipping this run: {throttle.Reason}"); + RunReport.SetRunType(RunTypes.Skip); + StatusManager.SetPhaseStatus(InstallationPhase.SetupAssistant, InstallationStage.Skipped); + StatusManager.SetPhaseStatus(InstallationPhase.Userland, InstallationStage.Skipped); + Logger.WriteCompletion("Nothing downloaded: baseline throttled"); + StatusManager.WriteSuccessfulCompletionRegistry(); + await ReportManager.SendRunSummaryAsync(true, runStartUtc, Version, manifestUrl); + RunReport.Finish(); + return ExitSuccess; + } + Logger.Info($"Baseline throttle: running ({throttle.Reason})"); Logger.Info("Baseline mode: refreshing setupassistant items without a dialog; userland is skipped."); } else @@ -1567,6 +1589,44 @@ static async Task DownloadFile(string displayName, string url, string localPath) Logger.WriteSubProgress("Downloaded", sizeText); } + /// + /// The same checks as , from manifest fields only: + /// the item's hash is in the install ledger, or an MSI item's + /// productCode/upgradeCode is installed at its version or newer. + /// Null when the manifest does not say enough, and the file is downloaded and + /// checked after. + /// + static string? BaselineSkipReasonBeforeDownload(JsonElement packageInfo, string type) + { + string? Field(string name) => + packageInfo.ValueKind == JsonValueKind.Object && + packageInfo.TryGetProperty(name, out var p) && p.ValueKind == JsonValueKind.String + ? p.GetString() : null; + + var hash = Field("hash"); + if (!string.IsNullOrWhiteSpace(hash)) + { + var bare = hash.Trim(); + if (bare.StartsWith("sha256:", StringComparison.OrdinalIgnoreCase)) bare = bare[7..]; + if (new InstallLedger().Contains(bare)) + return "this file was already installed by BootstrapMate"; + } + + var version = Field("version"); + var productCode = Field("productCode"); + var upgradeCode = Field("upgradeCode"); + if (type.Equals("msi", StringComparison.OrdinalIgnoreCase) && + !string.IsNullOrWhiteSpace(version) && + (!string.IsNullOrWhiteSpace(productCode) || !string.IsNullOrWhiteSpace(upgradeCode))) + { + var product = new MsiProduct.Info(productCode ?? "", version!, upgradeCode, PackageLabel(packageInfo)); + if (MsiProduct.IsInstalledAtOrAbove(product, out var installed)) + return $"version {installed} is installed (manifest version {version})"; + } + + return null; + } + /// /// Why a baseline run can leave this file alone, or null when it must install it: /// the same file is in the install ledger, or its MSI product is already @@ -1599,6 +1659,14 @@ static async Task DownloadAndInstallPackage(string displayName, string url try { + // Baseline: decide from the manifest alone when possible, so an item that is + // already in place costs no download. + if (baseline && BaselineSkipReasonBeforeDownload(packageInfo, type) is { } earlySkip) + { + Logger.WriteSkipped($"{displayName} - {earlySkip} (not downloaded)"); + return false; + } + DialogManager.Instance.NotifyDownloadStarted(displayName); await DownloadFile(displayName, url, localPath); diff --git a/README.md b/README.md index f71085e..7ec764b 100644 --- a/README.md +++ b/README.md @@ -173,8 +173,11 @@ the preflight has decided, it is rewritten with `skip`, `baseline` or run. It is written atomically when the run starts, with status `running`, and again when the session closes. At the start, `end_time` and `duration_seconds` are `null` and `items` is empty. Its status values match `session.json`: `running`, -`completed`, `partial_failure` (some items failed), and `failed` (the preflight -failed or the manifest would not load). The time in the `--last-run` line is UTC +`completed`, `partial_failure` (some items failed), `failed` (the preflight +failed or the manifest would not load), and `interrupted`: a run that died without +recording its end (a reboot, a killed process). The next run finds the record still +at `running`, relabels that run's `session.json`, and logs a warning before it +writes its own record. The time in the `--last-run` line is UTC to the minute. ```json @@ -214,6 +217,46 @@ install run started without elevation and without an interactive console (a scri a remote shell, redirected input) exits `3` instead of waiting at the elevation prompt. +## Baseline throttle + +Every baseline run downloads from the package host, so baselines are rate-limited. +The check runs only after the preflight has chosen baseline, so provisioning is never +limited. It reads `C:\ProgramData\ManagedBootstrap\baseline.json`, which holds the +last baseline's `end_time`, `status` and `consecutive_failures`. `last-run.json` +cannot hold the clock, because every run rewrites it. + +| Last baseline | Next baseline | +|---|---| +| Completed less than `BaselineMinIntervalHours` ago (default 144) | Skips | +| Failed, partially failed or interrupted for the first time | One retry after 24 hours, never sooner | +| Failed again after that retry | Waits the full interval | +| None: a provisioning run clears the record | Runs | +| Any, with `C:\ProgramData\ManagedBootstrap\.bootstrap_force` present | Runs; the preflight consumes the file | + +A throttled run fetches the manifest and runs the preflight, then downloads nothing. +It logs why, ends as `skip`, records SetupAssistant and Userland as `Skipped` in +`status.json`, and leaves `baseline.json` alone. `--force` does not bypass the +throttle. `BaselineMinIntervalHours` comes from policy or the settings registry +(0-8760; 0 disables the limit for completed runs). + +A baseline run also skips an item without downloading it when the manifest says +enough: its `hash` is already in the install ledger, or an `msi` item's `productCode` +or `upgradeCode` is installed at its `version` (the MSI ProductVersion) or newer. +Without those fields, the file is downloaded and checked afterwards. + +## Versions + +Everything carries the build stamp `YYYY.MM.DD.HHMM`: file and assembly versions, +`--version`, `HKLM\SOFTWARE\BootstrapMate\Version` (the Intune detection value), +logs, `last-run.json` and release tags. + +The MSI ProductVersion is the only exception. Windows Installer fields are numeric +(255.255.65535), and upgrades compare only the first three fields, so the MSI uses +`YY.M.DDHH.MM`: `2026.10.04.1951` is `26.10.419.51`. Upgrades compare to the hour. +Two builds made in the same hour compare equal and do not upgrade each other, so +publish at most one build per hour. Pass `-p:FullVersion=YYYY.MM.DD.HHMM` to the +installer project; it derives the ProductVersion. + ## Downloads Package downloads stream to disk with no overall HTTP timeout. An attempt fails when diff --git a/RunReport.cs b/RunReport.cs index 7ac2fbb..65b3fc5 100644 --- a/RunReport.cs +++ b/RunReport.cs @@ -17,6 +17,7 @@ public static void Start(string version) { lock (_lock) { + RecoverInterruptedRun(); _record = new LastRunRecord { SessionId = Logger.GetSessionId() ?? "", @@ -29,6 +30,26 @@ public static void Start(string version) } } + /// + /// A last-run.json still at "running" belongs to a run that died: this run holds the + /// single-instance lock, so none other is going. Its session is relabelled + /// "interrupted", and an interrupted baseline counts as a failed one, so the throttle + /// gives it one retry a day later instead of letting whatever killed it loop. + /// + private static void RecoverInterruptedRun() + { + var orphan = LastRunFile.MarkInterrupted(LastRunFile.Read(), Logger.LogDirectory); + if (orphan is null) return; + + Logger.Warning($"Previous run {orphan.SessionId} ({orphan.RunType}, started {orphan.StartTime}) " + + "never finished; recorded as interrupted"); + if (string.Equals(orphan.RunType, RunTypes.Baseline, StringComparison.OrdinalIgnoreCase)) + { + var when = DateTimeOffset.TryParse(orphan.StartTime, out var started) ? started : DateTimeOffset.Now; + BaselineThrottle.Write(BaselineThrottle.After(BaselineThrottle.Read(), RunStatuses.Interrupted, when)); + } + } + public static void SetRunType(string runType) { Logger.SetRunType(runType); @@ -72,6 +93,13 @@ public static void Finish(string? status = null) _record.Errors = outcome.Errors; _record.Warnings = outcome.Warnings; LastRunFile.Write(_record); + + // The baseline clock: a baseline sets it, a provisioning run clears it (a + // machine provisioned again starts over), a skip leaves it alone. + if (_record.RunType == RunTypes.Baseline) + BaselineThrottle.Write(BaselineThrottle.After(BaselineThrottle.Read(), outcome.Status, new DateTimeOffset(outcome.End))); + else if (_record.RunType == RunTypes.Provisioning && outcome.Status == RunStatuses.Completed) + BaselineThrottle.Clear(); } } diff --git a/build.ps1 b/build.ps1 index e2ff128..b2aeda1 100644 --- a/build.ps1 +++ b/build.ps1 @@ -1507,14 +1507,10 @@ function Update-Version { $newVersion = "$year.$month.$day.$revision" Write-Log "Updating version to: $newVersion (YYYY.MM.DD.HHMM format for Intune compatibility)" "INFO" - # Create MSI-compatible version (major.minor.build < 65536) - # Convert YYYY.MM.DD.HHMM to YY.MM.DD.HHMM for MSI ProductVersion - $now = Get-Date - $msiMajor = $now.Year - 2000 # e.g., 25 for 2025 - $msiMinor = [int]$now.ToString("MM") # e.g., 9 for September - $msiBuild = [int]$now.ToString("dd") # e.g., 2 for 2nd day - $msiRevision = [int]$now.ToString("HHmm") # e.g., 2141 for 21:41 - $msiVersion = "$msiMajor.$msiMinor.$msiBuild.$msiRevision" + # MSI ProductVersion is YY.M.DDHH.MM (Windows Installer compares only the first + # three fields, so the hour has to be in them). The wixproj derives the same value + # from FullVersion; this copy is only for the log. + $msiVersion = "{0}.{1}.{2}.{3}" -f ($now.Year - 2000), $now.Month, ($now.Day * 100 + $now.Hour), $now.Minute Write-Log "MSI ProductVersion: $msiVersion (MSI-compliant format)" "INFO" @@ -1536,7 +1532,7 @@ function Update-Version { # Return version information regardless of whether static version was found return @{ FullVersion = $newVersion # YYYY.MM.DD.HHMM for Intune detection - MsiVersion = $msiVersion # YY.MM.DD.HHMM for MSI ProductVersion + MsiVersion = $msiVersion # YY.M.DDHH.MM for MSI ProductVersion } } @@ -1571,7 +1567,7 @@ function Build-MSI { "--configuration", "Release", "--verbosity", "normal", "-p:Platform=$Arch", - "-p:ProductVersion=$($versionInfo.MsiVersion)", + "-p:FullVersion=$($versionInfo.FullVersion)", "-p:BinDir=$binDirAbsolute" ) if ($appDirAbsolute) { diff --git a/installer/BootstrapMate.Installer.wixproj b/installer/BootstrapMate.Installer.wixproj index 1e40ff0..fc11453 100644 --- a/installer/BootstrapMate.Installer.wixproj +++ b/installer/BootstrapMate.Installer.wixproj @@ -11,14 +11,23 @@ + + $([System.Text.RegularExpressions.Regex]::Replace('$(FullVersion)', '^20(\d\d)\.0?(\d{1,2})\.0?(\d{1,2})\.(\d\d)0?(\d{1,2})$', '$1.$2.$3$4.$5')) 0.0.0 + $(ProductVersion) $(MSBuildProjectDirectory)\..\publish\executables\$(Platform) $(MSBuildProjectDirectory)\..\publish\app\$(Platform) $(MSBuildProjectDirectory)\sbin-staging - BIN_DIR=$(BinDir);APP_DIR=$(AppDir);SBIN_DIR=$(SbinDir);ProductVersion=$(ProductVersion) + BIN_DIR=$(BinDir);APP_DIR=$(AppDir);SBIN_DIR=$(SbinDir);ProductVersion=$(ProductVersion);FullVersion=$(FullVersion) diff --git a/installer/Product.wxs b/installer/Product.wxs index 7cc6852..1be9831 100644 --- a/installer/Product.wxs +++ b/installer/Product.wxs @@ -7,6 +7,9 @@ + + + - + - + Default: C:\Program Files\BootstrapMate + Minimum Hours Between Baseline Runs + A baseline run that starts less than this many hours after the last completed baseline exits as a skip before downloading anything. A failed, partially failed or interrupted baseline gets one retry after 24 hours, never sooner; if that retry fails too, the full interval applies. Provisioning runs are not limited. While C:\ProgramData\ManagedBootstrap\.bootstrap_force is present the limit does not apply; the preflight consumes the file. + +Range: 0-8760 hours +Default: 144 hours (6 days) + Network Timeout (seconds) Timeout in seconds for the manifest request, and how long a package download may go without receiving data before the attempt fails and is retried. A large package keeps downloading for as long as data keeps arriving. @@ -165,6 +171,9 @@ Default: Disabled Timeout (seconds): + + Hours between baseline runs: + diff --git a/src/BootstrapMate.Core/BaselineThrottle.cs b/src/BootstrapMate.Core/BaselineThrottle.cs new file mode 100644 index 0000000..9afdc5b --- /dev/null +++ b/src/BootstrapMate.Core/BaselineThrottle.cs @@ -0,0 +1,117 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace BootstrapMate.Core; + +/// +/// The last baseline run's outcome, kept apart from last-run.json because every run +/// rewrites that file, a throttled one included, so it cannot hold the clock. +/// The schema matches the macOS build's baseline.json. +/// +public sealed class BaselineRecord +{ + [JsonPropertyName("end_time")] public string EndTime { get; set; } = ""; + [JsonPropertyName("status")] public string Status { get; set; } = ""; + [JsonPropertyName("consecutive_failures")] public int ConsecutiveFailures { get; set; } +} + +/// +/// Keeps baseline runs from repeating. Every run downloads from the package host, so a +/// machine that is already current must not baseline again on every trigger (the daily +/// Self-Heal task, an Intune reinstall or retry). The rule matches the macOS build. +/// +/// +/// Applied only after the preflight has chosen baseline, so provisioning is never limited: +/// +/// The .bootstrap_force file in ProgramData\ManagedBootstrap: run. The throttle +/// only looks; the preflight consumes it. +/// No record: run. A provisioning run clears the record. +/// Last baseline completed less than BaselineMinIntervalHours ago (default 144): skip. +/// Last baseline failed, partially failed or was interrupted: one retry after 24 hours; +/// if that retry fails too, the full interval applies again. +/// +/// A throttled run leaves the record alone, so the last real baseline stays the reference. +/// +public static class BaselineThrottle +{ + public const int DefaultMinIntervalHours = 144; + public const int FailedRetryHours = 24; + + public static readonly string ForceFilePath = @"C:\ProgramData\ManagedBootstrap\.bootstrap_force"; + public static readonly string DefaultRecordPath = @"C:\ProgramData\ManagedBootstrap\baseline.json"; + + public sealed record Decision(bool Skip, string Reason); + + public static Decision Evaluate(BaselineRecord? last, DateTimeOffset now, int minIntervalHours, bool forced) + { + if (forced) return new(false, $"{ForceFilePath} is present"); + if (last is null) return new(false, "no baseline recorded"); + if (!DateTimeOffset.TryParse(last.EndTime, out var when)) + return new(false, "last baseline has no readable time"); + + var age = now - when; + bool completed = string.Equals(last.Status, RunStatuses.Completed, StringComparison.OrdinalIgnoreCase); + var interval = TimeSpan.FromHours(Math.Max(0, minIntervalHours)); + // The first failure earns one retry a day later; once that retry has been used, + // a failing machine waits the full interval like a healthy one. + var wait = completed || last.ConsecutiveFailures > 1 + ? interval + : TimeSpan.FromHours(FailedRetryHours); + + if (age < wait) + { + return new(true, + $"last baseline {last.Status} {age.TotalHours:F1}h ago, under the {wait.TotalHours:F0}h minimum; " + + $"next baseline allowed after {when + wait:yyyy-MM-dd HH:mm zzz}"); + } + + return new(false, $"last baseline {last.Status} {age.TotalHours:F1}h ago (limit {wait.TotalHours:F0}h)"); + } + + /// The record a finished baseline leaves, counting failures in a row. + public static BaselineRecord After(BaselineRecord? previous, string status, DateTimeOffset end) => new() + { + EndTime = end.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"), + Status = status, + ConsecutiveFailures = string.Equals(status, RunStatuses.Completed, StringComparison.OrdinalIgnoreCase) + ? 0 + : (previous?.ConsecutiveFailures ?? 0) + 1 + }; + + private static readonly JsonSerializerOptions WriteOptions = new() { WriteIndented = true }; + + /// The record at , or null when it is absent or unreadable. + public static BaselineRecord? Read(string? path = null) + { + try + { + var target = path ?? DefaultRecordPath; + return File.Exists(target) ? JsonSerializer.Deserialize(File.ReadAllText(target)) : null; + } + catch + { + return null; + } + } + + /// Writes through a temporary file and a rename. Failure is swallowed. + public static void Write(BaselineRecord record, string? path = null) + { + var target = path ?? DefaultRecordPath; + try + { + var directory = Path.GetDirectoryName(target); + if (!string.IsNullOrEmpty(directory)) Directory.CreateDirectory(directory); + File.WriteAllText(target + ".tmp", JsonSerializer.Serialize(record, WriteOptions)); + File.Move(target + ".tmp", target, overwrite: true); + } + catch + { + } + } + + public static void Clear(string? path = null) + { + try { File.Delete(path ?? DefaultRecordPath); } catch { } + } +} diff --git a/src/BootstrapMate.Core/BootstrapMateConfig.cs b/src/BootstrapMate.Core/BootstrapMateConfig.cs index 92a01db..5091bc2 100644 --- a/src/BootstrapMate.Core/BootstrapMateConfig.cs +++ b/src/BootstrapMate.Core/BootstrapMateConfig.cs @@ -39,6 +39,12 @@ public sealed class BootstrapMateConfig /// public int NetworkTimeout { get; set; } = BootstrapMateConstants.DefaultNetworkTimeout; + /// + /// Minimum hours between completed baseline runs; a failed baseline retries after 24. + /// See . + /// + public int BaselineMinIntervalHours { get; set; } = BaselineThrottle.DefaultMinIntervalHours; + /// Creates a deep copy of this configuration. public BootstrapMateConfig Clone() => (BootstrapMateConfig)MemberwiseClone(); } diff --git a/src/BootstrapMate.Core/ConfigManager.cs b/src/BootstrapMate.Core/ConfigManager.cs index 054d544..2d8daaa 100644 --- a/src/BootstrapMate.Core/ConfigManager.cs +++ b/src/BootstrapMate.Core/ConfigManager.cs @@ -163,6 +163,7 @@ void WriteInt(string key, int value) WriteBool("BlurScreen", settings.BlurScreen); WriteString("CustomInstallPath", settings.CustomInstallPath); WriteInt("NetworkTimeout", settings.NetworkTimeout); + WriteInt("BaselineMinIntervalHours", settings.BaselineMinIntervalHours); WriteString("ReportingUrl", settings.ReportingUrl); WriteString("ReportingHeader", settings.ReportingHeader); WriteBool("VerifyPackageSignatures", settings.VerifyPackageSignatures); @@ -241,6 +242,9 @@ private void LoadFromManagement(ManagementDetector management) if (management.GetManagedInt("NetworkTimeout") is { } timeout) Config.NetworkTimeout = timeout; + if (management.GetManagedInt("BaselineMinIntervalHours") is { } baselineHours) + Config.BaselineMinIntervalHours = baselineHours; + if (management.GetManagedString("ReportingUrl") is { Length: > 0 } reportingUrl) Config.ReportingUrl = reportingUrl; @@ -302,6 +306,7 @@ private void LoadFromHive(RegistryHive hive, ConfigSource source, RegistryView v Config.BlurScreen = ReadBool(settingsKey, "BlurScreen") ?? Config.BlurScreen; Config.CustomInstallPath = ReadString(settingsKey, "CustomInstallPath") ?? Config.CustomInstallPath; Config.NetworkTimeout = ReadInt(settingsKey, "NetworkTimeout") ?? Config.NetworkTimeout; + Config.BaselineMinIntervalHours = ReadInt(settingsKey, "BaselineMinIntervalHours") ?? Config.BaselineMinIntervalHours; Config.ReportingUrl = ReadString(settingsKey, "ReportingUrl") ?? Config.ReportingUrl; Config.ReportingHeader = ReadString(settingsKey, "ReportingHeader") ?? Config.ReportingHeader; Config.VerifyPackageSignatures = ReadBool(settingsKey, "VerifyPackageSignatures") ?? Config.VerifyPackageSignatures; diff --git a/src/BootstrapMate.Core/LastRun.cs b/src/BootstrapMate.Core/LastRun.cs index f4ad724..1078319 100644 --- a/src/BootstrapMate.Core/LastRun.cs +++ b/src/BootstrapMate.Core/LastRun.cs @@ -57,6 +57,11 @@ public static class RunStatuses public const string PartialFailure = "partial_failure"; /// The run could not do its work: a failed preflight, or a manifest that would not load. public const string Failed = "failed"; + /// + /// The run died without recording its end (a reboot, a killed process, an MSI upgrade). + /// Set by the next run, which finds the record still at . + /// + public const string Interrupted = "interrupted"; } public static class ItemResults @@ -117,6 +122,45 @@ public static void Write(LastRunRecord record, string? path = null) } } + /// + /// A record left at by a run that is no longer going, + /// relabelled ; null when the record finished. + /// Only call this while holding the single-instance lock, so no live run is mistaken + /// for a dead one. Its session.json under is relabelled too. + /// + public static LastRunRecord? MarkInterrupted(LastRunRecord? record, string? logsDirectory = null) + { + if (record is null || !string.Equals(record.Status, RunStatuses.Running, StringComparison.OrdinalIgnoreCase)) + return null; + + record.Status = RunStatuses.Interrupted; + if (logsDirectory is not null && SessionDirectory(logsDirectory, record.SessionId) is { } dir) + { + try + { + var sessionFile = Path.Combine(dir, "session.json"); + if (File.Exists(sessionFile) && + System.Text.Json.Nodes.JsonNode.Parse(File.ReadAllText(sessionFile)) is System.Text.Json.Nodes.JsonObject session && + (string?)session["status"] == RunStatuses.Running) + { + session["status"] = RunStatuses.Interrupted; + File.WriteAllText(sessionFile, session.ToJsonString(WriteOptions)); + } + } + catch + { + } + } + return record; + } + + /// Session 2026-10-05-030002 lives in logs/2026-10-05/030002. + public static string? SessionDirectory(string logsDirectory, string sessionId) + { + if (sessionId.Length < 12 || sessionId[10] != '-') return null; + return Path.Combine(logsDirectory, sessionId[..10], sessionId[11..]); + } + /// First line of an error, trimmed to . public static string ShortError(string? error) { diff --git a/src/BootstrapMate.Core/ManagementDetector.cs b/src/BootstrapMate.Core/ManagementDetector.cs index 7b8b60e..5fbade7 100644 --- a/src/BootstrapMate.Core/ManagementDetector.cs +++ b/src/BootstrapMate.Core/ManagementDetector.cs @@ -35,6 +35,7 @@ public sealed class ManagementDetector ["BlurScreen"] = ["BlurScreen"], ["CustomInstallPath"] = ["CustomInstallPath", "InstallPath", "iapath"], ["NetworkTimeout"] = ["NetworkTimeout"], + ["BaselineMinIntervalHours"] = ["BaselineMinIntervalHours"], ["ReportingUrl"] = ["ReportingUrl", "ReportURL", "reportingUrl"], ["ReportingHeader"] = ["ReportingHeader", "ReportingAuthorizationHeader"], ["VerifyPackageSignatures"] = ["VerifyPackageSignatures", "VerifySignatures"], diff --git a/tests/BootstrapMate.Core.Tests/BaselineThrottleTests.cs b/tests/BootstrapMate.Core.Tests/BaselineThrottleTests.cs new file mode 100644 index 0000000..4bfbbda --- /dev/null +++ b/tests/BootstrapMate.Core.Tests/BaselineThrottleTests.cs @@ -0,0 +1,156 @@ +using BootstrapMate.Core; +using Xunit; + +namespace BootstrapMate.Core.Tests; + +public class BaselineThrottleTests +{ + private static readonly DateTimeOffset Now = new(2026, 10, 5, 3, 0, 0, TimeSpan.FromHours(-7)); + + private static BaselineRecord Record(string status, double hoursAgo, int failures = 0) => new() + { + Status = status, + EndTime = Now.AddHours(-hoursAgo).ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"), + ConsecutiveFailures = failures + }; + + private static BaselineThrottle.Decision Eval(BaselineRecord? last, bool forced = false, int interval = 144) => + BaselineThrottle.Evaluate(last, Now, interval, forced); + + [Fact] + public void AYoungCompletedBaselineSkips() + { + var d = Eval(Record(RunStatuses.Completed, 24)); + Assert.True(d.Skip); + Assert.Contains("next baseline allowed after", d.Reason); + } + + [Fact] + public void AnOldCompletedBaselineRuns() => + Assert.False(Eval(Record(RunStatuses.Completed, 145)).Skip); + + [Theory] + [InlineData(RunStatuses.Failed)] + [InlineData(RunStatuses.PartialFailure)] + [InlineData(RunStatuses.Interrupted)] + public void AFirstFailureRetriesOnceAfter24Hours(string status) + { + Assert.True(Eval(Record(status, 23, failures: 1)).Skip); + Assert.False(Eval(Record(status, 25, failures: 1)).Skip); + } + + [Fact] + public void OnceTheRetryIsUsedTheFullIntervalApplies() + { + Assert.True(Eval(Record(RunStatuses.Failed, 25, failures: 2)).Skip); + Assert.True(Eval(Record(RunStatuses.Interrupted, 143, failures: 5)).Skip); + Assert.False(Eval(Record(RunStatuses.Failed, 145, failures: 2)).Skip); + } + + [Fact] + public void AFailedBaselineNeverRetriesSoonerThan24HoursEvenWithAShortInterval() => + Assert.True(Eval(Record(RunStatuses.Failed, 2, failures: 1), interval: 1).Skip); + + [Fact] + public void TheForceFileAlwaysRuns() + { + var d = Eval(Record(RunStatuses.Completed, 1), forced: true); + Assert.False(d.Skip); + Assert.Contains(".bootstrap_force", d.Reason); + } + + [Fact] + public void NoRecordRuns() => Assert.False(Eval(null).Skip); + + [Fact] + public void AnUnreadableTimeDoesNotBlockTheRun() + { + var last = Record(RunStatuses.Completed, 1); + last.EndTime = "garbage"; + Assert.False(Eval(last).Skip); + } + + [Fact] + public void ZeroIntervalTurnsTheThrottleOffForCompletedRuns() => + Assert.False(Eval(Record(RunStatuses.Completed, 0.1), interval: 0).Skip); + + [Fact] + public void TheDefaultIntervalIsSixDays() => + Assert.Equal(144, new BootstrapMateConfig().BaselineMinIntervalHours); + + [Fact] + public void FailuresCountUntilABaselineCompletes() + { + var first = BaselineThrottle.After(null, RunStatuses.Failed, Now); + Assert.Equal(1, first.ConsecutiveFailures); + var second = BaselineThrottle.After(first, RunStatuses.Interrupted, Now); + Assert.Equal(2, second.ConsecutiveFailures); + Assert.Equal(0, BaselineThrottle.After(second, RunStatuses.Completed, Now).ConsecutiveFailures); + } + + [Fact] + public void TheRecordSurvivesAWriteAndRead() + { + var path = Path.Combine(Path.GetTempPath(), $"baseline-{Guid.NewGuid():N}.json"); + try + { + BaselineThrottle.Write(Record(RunStatuses.PartialFailure, 3, failures: 1), path); + var read = BaselineThrottle.Read(path); + Assert.NotNull(read); + Assert.Equal(RunStatuses.PartialFailure, read!.Status); + Assert.Equal(1, read.ConsecutiveFailures); + Assert.Contains("\"consecutive_failures\"", File.ReadAllText(path)); + BaselineThrottle.Clear(path); + Assert.Null(BaselineThrottle.Read(path)); + } + finally + { + File.Delete(path); + } + } +} + +public class InterruptedRunTests +{ + [Fact] + public void ARunStillMarkedRunningIsRelabelledInterrupted() + { + var root = Path.Combine(Path.GetTempPath(), $"logs-{Guid.NewGuid():N}"); + var sessionDir = Path.Combine(root, "2026-10-05", "030002"); + Directory.CreateDirectory(sessionDir); + var sessionFile = Path.Combine(sessionDir, "session.json"); + File.WriteAllText(sessionFile, "{\"session_id\":\"2026-10-05-030002\",\"status\":\"running\"}"); + try + { + var record = new LastRunRecord { SessionId = "2026-10-05-030002", RunType = RunTypes.Baseline, Status = RunStatuses.Running }; + var marked = LastRunFile.MarkInterrupted(record, root); + Assert.NotNull(marked); + Assert.Equal(RunStatuses.Interrupted, marked!.Status); + Assert.Contains("\"interrupted\"", File.ReadAllText(sessionFile)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + + [Theory] + [InlineData(RunStatuses.Completed)] + [InlineData(RunStatuses.Failed)] + [InlineData(RunStatuses.PartialFailure)] + public void AFinishedRunIsLeftAlone(string status) => + Assert.Null(LastRunFile.MarkInterrupted(new LastRunRecord { Status = status })); + + [Fact] + public void NoRecordIsNotAnInterruption() => Assert.Null(LastRunFile.MarkInterrupted(null)); + + [Theory] + [InlineData("2026-10-05-030002", "2026-10-05", "030002")] + [InlineData("2026-10-05-030002_2", "2026-10-05", "030002_2")] + public void ASessionIdNamesItsDirectory(string id, string day, string time) => + Assert.Equal(Path.Combine("logs", day, time), LastRunFile.SessionDirectory("logs", id)); + + [Fact] + public void AnUnrecognisedSessionIdHasNoDirectory() => + Assert.Null(LastRunFile.SessionDirectory("logs", "")); +}