From 830db6f5506dbfb94105b2e65dd209ea2fe9a618 Mon Sep 17 00:00:00 2001 From: nick Date: Fri, 6 Nov 2026 20:02:39 +0000 Subject: [PATCH] Promote advertised Companion feature explicitly after MSI recaching --- .../WindowsValidation/Setup/Setup.cs | 7 +++++++ .../Setup/Test-UpgradeRegression.ps1 | 20 +++++++++++++++++++ .../WindowsValidation/Setup/UpgradePolicy.cs | 12 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/PlotDirector.NativeCompanion/WindowsValidation/Setup/Setup.cs b/PlotDirector.NativeCompanion/WindowsValidation/Setup/Setup.cs index 81b864ce..473cb63e 100644 --- a/PlotDirector.NativeCompanion/WindowsValidation/Setup/Setup.cs +++ b/PlotDirector.NativeCompanion/WindowsValidation/Setup/Setup.cs @@ -22,6 +22,7 @@ internal static class Setup [DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiInstallProduct(string package, string properties); [DllImport("msi.dll")] private static extern uint MsiSetInternalUI(uint level, IntPtr parent); [DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiEnableLog(uint mode, string path, uint attributes); + [DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiConfigureFeature(string product, string feature, int state); [STAThread] private static int Main(string[] args) { Application.EnableVisualStyles(); @@ -73,6 +74,12 @@ internal static class Setup CompanionInstallerInventory.RequireCurrentUser(installed, SetupRelease.MsiVersion); if (installed.Length != 1 || installed[0].Version != SetupRelease.MsiVersion) throw new InvalidOperationException("Installed MSI identity/version does not match this user's package. Stop and retain the setup log."); Write("Verified MSI context=2 and current-user SID; product=" + SetupRelease.ProductCode); + CompanionUpgradePolicy.EnsureLocalFeature( + () => CompanionInstallerInventory.FeatureState(installed[0], "Complete"), + () => { + MsiEnableLog(0x1fff, Path.Combine(state, "windows-installer-local-feature.log"), 0); + return MsiConfigureFeature(SetupRelease.ProductCode, "Complete", 3); + }, Write); var home = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "PlotDirector Companion"); var manifest = Path.Combine(home, "PlotDirector.WordAddIn.vsto"); var agent = Path.Combine(home, "Agent", "PlotDirector.Companion.Agent.exe"); diff --git a/PlotDirector.NativeCompanion/WindowsValidation/Setup/Test-UpgradeRegression.ps1 b/PlotDirector.NativeCompanion/WindowsValidation/Setup/Test-UpgradeRegression.ps1 index 8d7bde3f..4e31d3b9 100644 --- a/PlotDirector.NativeCompanion/WindowsValidation/Setup/Test-UpgradeRegression.ps1 +++ b/PlotDirector.NativeCompanion/WindowsValidation/Setup/Test-UpgradeRegression.ps1 @@ -48,6 +48,26 @@ foreach($versions in @(@{word='1.0.0.10';agent='1.0.0.10'},@{word='1.0.0.12';age Assert-Upgrade ($message -like '*Expected 1.0.0.12*' -and $message.Contains('Word add-in detected '+$versions.word) -and $message.Contains('tray executable detected '+$versions.agent)) "Strict check rejects and diagnoses old/mixed files: $($versions.word) / $($versions.agent)" } $null=$policy.GetMethod('VerifyVersions').Invoke($null,@('1.0.0.12','1.0.0.12','1.0.0.12'));$checks.Add('Strict check accepts matching version12 files') +$transition=$policy.GetMethod('EnsureLocalFeature') +$query=[Func[int]]{$script:stateValue};$logger=[Action[string]]{param($message)} +foreach($initial in @(1,2,4)){ + $script:stateValue=$initial;$script:configureCalls=0 + $configure=[Func[uint32]]{$script:configureCalls++;$script:stateValue=3;return [uint32]0} + $null=$transition.Invoke($null,@($query,$configure,$logger)) + Assert-Upgrade ($script:configureCalls -eq 1 -and $script:stateValue -eq 3) "Compiled control flow explicitly promotes feature state $initial to Local before accepting versions" +} +$script:stateValue=3;$script:configureCalls=0 +$null=$transition.Invoke($null,@($query,$configure,$logger)) +Assert-Upgrade ($script:configureCalls -eq 0) 'Already Local feature is not needlessly configured again' +foreach($failure in @('native-error','still-advertised','unknown-state')){ + $script:stateValue=if($failure -eq 'unknown-state'){-1}else{1};$script:configureCalls=0 + $configure=[Func[uint32]]{$script:configureCalls++;if($failure -eq 'native-error'){return [uint32]1603};return [uint32]0} + $refused=$false;try{$null=$transition.Invoke($null,@($query,$configure,$logger))}catch{$refused=$true} + Assert-Upgrade $refused "Compiled feature transition refuses $failure instead of trusting result0 or old files" + if($failure -eq 'unknown-state'){Assert-Upgrade ($script:configureCalls -eq 0) 'Unknown native state is rejected before any configuration request'} +} +$bootstrap=Get-Content -Raw (Join-Path $PSScriptRoot 'Setup.cs') +Assert-Upgrade ($bootstrap.IndexOf('CompanionUpgradePolicy.EnsureLocalFeature(') -lt $bootstrap.IndexOf('CompanionUpgradePolicy.VerifyVersions(') -and $bootstrap.Contains('MsiConfigureFeature(SetupRelease.ProductCode, "Complete", 3)')) 'Bootstrapper wires explicit native Local configuration before strict version validation' $required=@('PlotDirector.WordAddIn.dll','PlotDirector.Companion.Agent.exe') foreach($name in $required){$file=@($corrected.files | Where-Object {($_.FileName -split '\|')[-1] -eq $name});Assert-Upgrade ($file.Count -eq 1 -and @($corrected.components | Where-Object {$_.Component_ -eq $file[0].Component_ -and $_.Feature_ -eq 'Complete'}).Count -eq 1) "Required file belongs to the locally selected Complete feature: $name"} @{passed=$checks;rejectedOriginalAuthoring=$failed.problems;correctedSetupSha256=(Get-FileHash $exe).Hash;scope='Read-only MSI tables and compiled pure policy only. No installed upgrade was executed.'} | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath $ReportPath -Encoding UTF8 diff --git a/PlotDirector.NativeCompanion/WindowsValidation/Setup/UpgradePolicy.cs b/PlotDirector.NativeCompanion/WindowsValidation/Setup/UpgradePolicy.cs index 8b06600f..4197504c 100644 --- a/PlotDirector.NativeCompanion/WindowsValidation/Setup/UpgradePolicy.cs +++ b/PlotDirector.NativeCompanion/WindowsValidation/Setup/UpgradePolicy.cs @@ -5,6 +5,18 @@ using System.IO; // desktop application must be installed locally, never advertised on demand. public static class CompanionUpgradePolicy { + public static void EnsureLocalFeature(Func query, Func installLocal, Action log) + { + int state = query(); log("Complete feature before local configuration=" + state); + if (state == 3) return; + if (state != 1 && state != 2 && state != 4) throw new InvalidDataException("Unexpected Complete feature state " + state + ". No local configuration was attempted."); + // REINSTALL repairs the existing state, including Advertised. It does not + // promote that state to Local, even with ADDLOCAL in the recache request. + uint result = installLocal(); log("Explicit ConfigureFeature(Local) result=" + result); + if (result != 0 && result != 3010) throw new InvalidOperationException("Installing Complete locally failed with Windows Installer code " + result + ". Retain the local-feature log."); + state = query(); log("Complete feature after local configuration=" + state); + if (state != 3) throw new InvalidDataException("Complete feature remained " + state + " after local configuration; required Local=3. Installation is not accepted."); + } public static string InstallProperties(bool alreadyRegistered) { string properties = "ALLUSERS=\"\" MSIINSTALLPERUSER=1 REBOOT=ReallySuppress ADDLOCAL=Complete REINSTALLMODE=vamus";