categories/Performance.ps1

# Performance category: SvcHostSplitThresholdInKB, Win32PrioritySeparation, keyboard
# delay/speed, MMCSS SystemResponsiveness/NetworkThrottlingIndex, MMCSS "Games" task profile.
# research.md -> performance category table.
#
# ponytail: the "Games" task profile values below (GPU Priority=8, Priority=6, Scheduling
# Category=High) already ship as Windows 11's own defaults on most machines - this action is
# honest drift-detection/repair (some OEM "gaming boost" utilities or third-party tuning tools
# have been observed to lower them), not a from-scratch improvement. The existing "only emit an
# action when current differs from target" pattern this file already uses makes that honest for
# free: a stock machine's dry-run correctly reports nothing to do here. See
# specs/011-competitive-latency-tuning/research.md before treating an empty result as a bug.

function Get-OctaPerformanceCategory {
    [CmdletBinding()]
    param()
    return [pscustomobject]@{
        Id                          = 'performance'
        DisplayName                 = 'Performance'
        Description                 = 'svchost/priority/keyboard/MMCSS tuning'
        RequiresElevation           = $true
        ContainsIrreversibleActions = $false
        GetActionsFunction          = 'Get-OctaPerformanceActions'
        ApplyActionFunction         = 'Set-OctaPerformanceAction'
    }
}

function Get-OctaPerformanceActions {
    [CmdletBinding()]
    param()
    $actions = @()

    # SvcHostSplitThresholdInKB - Safe (Windows already auto-applies this above ~3.5GB RAM;
    # this only does anything on older/constrained configurations).
    $svcHostKey = 'HKLM:\SYSTEM\CurrentControlSet\Control'
    $ramKb = [int64]((Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory / 1024)
    $currentThreshold = (Get-ItemProperty -Path $svcHostKey -Name 'SvcHostSplitThresholdInKB' -ErrorAction SilentlyContinue).SvcHostSplitThresholdInKB
    if ($null -eq $currentThreshold -or $currentThreshold -ne $ramKb) {
        $displayCurrent = if ($null -eq $currentThreshold) { '(not set)' } else { $currentThreshold }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$svcHostKey|SvcHostSplitThresholdInKB" `
            -CurrentValue $displayCurrent -PlannedValue $ramKb -Reversible $true -RiskLevel Safe
    }

    # Win32PrioritySeparation - Moderate (system-wide scheduling behavior, not cosmetic).
    $priorityKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\PriorityControl'
    $currentPriority = (Get-ItemProperty -Path $priorityKey -Name 'Win32PrioritySeparation' -ErrorAction SilentlyContinue).Win32PrioritySeparation
    if ($null -eq $currentPriority -or $currentPriority -ne 38) {
        $displayCurrent = if ($null -eq $currentPriority) { '(not set)' } else { $currentPriority }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$priorityKey|Win32PrioritySeparation" `
            -CurrentValue $displayCurrent -PlannedValue 38 -Reversible $true -RiskLevel Moderate
    }

    # Keyboard delay/speed - Safe (per-user preference, HKCU).
    $kbKey = 'HKCU:\Control Panel\Keyboard'
    $currentDelay = (Get-ItemProperty -Path $kbKey -Name 'KeyboardDelay' -ErrorAction SilentlyContinue).KeyboardDelay
    if ($null -eq $currentDelay -or $currentDelay -ne '0') {
        $displayCurrent = if ($null -eq $currentDelay) { '(not set)' } else { $currentDelay }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$kbKey|KeyboardDelay" `
            -CurrentValue $displayCurrent -PlannedValue '0' -Reversible $true -RiskLevel Safe
    }
    $currentSpeed = (Get-ItemProperty -Path $kbKey -Name 'KeyboardSpeed' -ErrorAction SilentlyContinue).KeyboardSpeed
    if ($null -eq $currentSpeed -or $currentSpeed -ne '31') {
        $displayCurrent = if ($null -eq $currentSpeed) { '(not set)' } else { $currentSpeed }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$kbKey|KeyboardSpeed" `
            -CurrentValue $displayCurrent -PlannedValue '31' -Reversible $true -RiskLevel Safe
    }

    # MMCSS - Moderate (removes/lowers a background-task scheduling reservation).
    $mmcssKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile'
    $currentResponsiveness = (Get-ItemProperty -Path $mmcssKey -Name 'SystemResponsiveness' -ErrorAction SilentlyContinue).SystemResponsiveness
    if ($null -eq $currentResponsiveness -or $currentResponsiveness -ne 0) {
        $displayCurrent = if ($null -eq $currentResponsiveness) { '(not set)' } else { $currentResponsiveness }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$mmcssKey|SystemResponsiveness" `
            -CurrentValue $displayCurrent -PlannedValue 0 -Reversible $true -RiskLevel Moderate
    }
    $currentThrottling = (Get-ItemProperty -Path $mmcssKey -Name 'NetworkThrottlingIndex' -ErrorAction SilentlyContinue).NetworkThrottlingIndex
    if ($null -eq $currentThrottling -or $currentThrottling -ne 0xffffffff) {
        $displayCurrent = if ($null -eq $currentThrottling) { '(not set)' } else { $currentThrottling }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$mmcssKey|NetworkThrottlingIndex" `
            -CurrentValue $displayCurrent -PlannedValue 0xffffffff -Reversible $true -RiskLevel Moderate
    }

    # MMCSS "Games" task profile - Safe (011-competitive-latency-tuning). Drift-detection/repair,
    # not an improvement over Windows' own shipped defaults - see file header comment.
    $gamesTaskKey = "$mmcssKey\Tasks\Games"
    $gamesTargets = @(
        @{ Name = 'GPU Priority'; Target = 8; Type = 'DWord' }
        @{ Name = 'Priority'; Target = 6; Type = 'DWord' }
        @{ Name = 'Scheduling Category'; Target = 'High'; Type = 'String' }
    )
    foreach ($t in $gamesTargets) {
        $current = (Get-ItemProperty -Path $gamesTaskKey -Name $t.Name -ErrorAction SilentlyContinue).($t.Name)
        if ($null -eq $current -or $current -ne $t.Target) {
            $displayCurrent = if ($null -eq $current) { '(not set)' } else { $current }
            $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$gamesTaskKey|$($t.Name)" `
                -CurrentValue $displayCurrent -PlannedValue $t.Target -Reversible $true -RiskLevel Safe
        }
    }

    return $actions
}

function Set-OctaPerformanceAction {
    [CmdletBinding()]
    param([Parameter(Mandatory)]$Action)
    $keyPath, $valueName = $Action.TargetIdentifier -split '\|', 2
    if (-not (Test-Path $keyPath)) {
        New-Item -Path $keyPath -Force | Out-Null
    }
    $type = if ($valueName -in @('KeyboardDelay', 'KeyboardSpeed', 'Scheduling Category')) { 'String' } else { 'DWord' }
    Set-ItemProperty -Path $keyPath -Name $valueName -Value $Action.PlannedValue -Type $type
}