categories/Network.ps1

# Network category: Nagle's Algorithm (per active TCP/IP interface) and Energy Efficient
# Ethernet / Green Ethernet (per physical adapter, discovered via Get-NetAdapterAdvancedProperty
# rather than a hardcoded per-vendor registry value name). research.md -> network-latency
# category table.
#
# ponytail: "allow the computer to turn off this device to save power" (PnPCapabilities) was
# scoped in research.md but dropped before writing any code - the only real NIC available to
# verify against (Intel I225-V) has no PnPCapabilities value set at all, so the documented
# "24 = disable power-off checkbox" flag could be neither confirmed nor denied, same verify-or-
# drop call already made for Modern Standby networking in 008-real-performance-parity. Upgrade
# path: add it back if/when it can be confirmed against real hardware (toggle the Device Manager
# checkbox, read the resulting registry value, compare).

function Get-OctaNetworkCategory {
    [CmdletBinding()]
    param()
    return [pscustomobject]@{
        Id                          = 'network-latency'
        DisplayName                 = 'Network Latency'
        Description                 = "Nagle's Algorithm (per interface), Energy Efficient Ethernet (per adapter)"
        RequiresElevation           = $true
        ContainsIrreversibleActions = $false
        GetActionsFunction          = 'Get-OctaNetworkActions'
        ApplyActionFunction         = 'Set-OctaNetworkAction'
    }
}

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

    # Nagle's Algorithm - one TcpAckFrequency/TCPNoDelay pair per interface that's actually
    # configured (has a real IP), not every historical interface GUID Windows never cleans up.
    $interfacesKey = 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces'
    $interfaces = @(Get-ChildItem -Path $interfacesKey -ErrorAction SilentlyContinue)
    foreach ($iface in $interfaces) {
        $ifaceKeyPath = "$interfacesKey\$($iface.PSChildName)"
        $props = Get-ItemProperty -Path $ifaceKeyPath -ErrorAction SilentlyContinue
        $hasAddress = ($props.IPAddress -and $props.IPAddress -ne '0.0.0.0') -or
        ($props.DhcpIPAddress -and $props.DhcpIPAddress -ne '0.0.0.0')
        if (-not $hasAddress) { continue }

        foreach ($valueName in @('TcpAckFrequency', 'TCPNoDelay')) {
            $current = $props.$valueName
            if ($null -eq $current -or $current -ne 1) {
                $displayCurrent = if ($null -eq $current) { '(not set)' } else { $current }
                $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$ifaceKeyPath|$valueName" `
                    -CurrentValue $displayCurrent -PlannedValue 1 -Reversible $true -RiskLevel Safe
            }
        }
    }

    # Energy Efficient Ethernet / Green Ethernet - vendor-specific RegistryKeyword, discovered
    # generically per physical adapter instead of hardcoded (research.md). Adapters whose driver
    # doesn't expose a matching advanced property simply contribute no action - honest
    # "nothing to do" per SC-003, same pattern gpu.ps1 uses for non-NVIDIA adapters.
    $adapters = @(Get-NetAdapter -Physical -ErrorAction SilentlyContinue)
    foreach ($adapter in $adapters) {
        $eeeProps = @(Get-NetAdapterAdvancedProperty -Name $adapter.Name -AllProperties -ErrorAction SilentlyContinue |
            Where-Object { $_.DisplayName -match 'Energy.*Efficient|Green Ethernet' -or $_.RegistryKeyword -match 'EEE' })
        foreach ($prop in $eeeProps) {
            $current = $prop.RegistryValue[0]
            if ($current -ne '0') {
                # Get-NetAdapterAdvancedProperty doesn't expose the adapter's own registry key
                # path directly - resolve it the same way gpu.ps1 does, by matching
                # NetCfgInstanceId under the Net class GUID.
                $netClassKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}'
                $adapterSubkey = Get-ChildItem -Path $netClassKey -ErrorAction SilentlyContinue |
                Where-Object { $_.PSChildName -match '^\d{4}$' } |
                Where-Object { (Get-ItemProperty -Path $_.PSPath -Name 'NetCfgInstanceId' -ErrorAction SilentlyContinue).NetCfgInstanceId -eq $adapter.InterfaceGuid }
                if (-not $adapterSubkey) { continue }
                $adapterKeyPath = "$netClassKey\$($adapterSubkey.PSChildName)"

                $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$adapterKeyPath|$($prop.RegistryKeyword)" `
                    -CurrentValue $current -PlannedValue '0' -Reversible $true -RiskLevel Safe
            }
        }
    }

    return $actions
}

function Set-OctaNetworkAction {
    [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 @('TcpAckFrequency', 'TCPNoDelay')) { 'DWord' } else { 'String' }
    Set-ItemProperty -Path $keyPath -Name $valueName -Value $Action.PlannedValue -Type $type
}