Private/Write-KadenSharedState.ps1

function Write-KadenSharedState {
    <#
    .SYNOPSIS
        Record applied version state without dropping unknown shared-manifest keys.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$ChildRoot,
        [Parameter(Mandatory = $true)]
        [string]$ManagedContentVersion,
        [string]$ProjectId,
        [Parameter(Mandatory = $true)]
        [string]$InstallationStatus
    )

    $sharedPath = Join-Path $ChildRoot '.kaden.json'
    $shared = [ordered]@{
        schemaVersion         = 1
        projectId             = $ProjectId
        managedContentVersion = $ManagedContentVersion
    }
    if (Test-Path -LiteralPath $sharedPath) {
        $existing = Get-Content -LiteralPath $sharedPath -Raw | ConvertFrom-Json
        foreach ($prop in @($existing.PSObject.Properties)) {
            $name = [string]$prop.Name
            if ($shared.Contains($name)) { continue }
            $shared[$name] = $prop.Value
        }
        if ([string]::IsNullOrWhiteSpace($ProjectId) -and $existing.projectId) {
            $shared.projectId = [string]$existing.projectId
        }
    }
    $shared.managedContentVersion = $ManagedContentVersion
    Write-KadenJsonAtomic -LiteralPath $sharedPath -Object $shared

    $local = [ordered]@{
        schemaVersion      = 1
        lastAppliedVersion = $ManagedContentVersion
        lastAppliedAtUtc   = [datetime]::UtcNow.ToString('o')
        installationStatus = $InstallationStatus
    }
    Write-KadenJsonAtomic -LiteralPath (Join-Path $ChildRoot '.kaden.local.json') -Object $local
}