Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

70.9 KB
Raw Blame
import { readMemoryObservation } from '@/utils/memoryObservation';
import type { MemoryObservation } from '@/types/api';
import { batch, createEffect, createSignal, onCleanup, untrack, type Accessor } from 'solid-js';
import { createStore, reconcile, unwrap } from 'solid-js/store';
import { canonicalizeMetricsHistoryTargetType } from '@/api/charts';
import { readAPIErrorMessage } from '@/api/responseUtils';
import { apiFetch, getOrgID } from '@/utils/apiClient';
import { normalizeOrgScope } from '@/utils/orgScope';
import { asTrimmedString } from '@/utils/stringUtils';
import { getGlobalWebSocketStore } from '@/stores/websocket-global';
import type {
  Resource,
  ResourceCephHealthCheckMeta,
  ResourceAgentUnraidMeta,
  ResourceCephMeta,
  ResourceChange,
  ResourceFacetCounts,
  ResourceDiscoveryTarget,
  ResourceDockerMeta,
  ResourceHealth,
  ResourceMetricsTarget,
  ResourceActionReadiness,
  ResourceAvailabilityMeta,
  ResourcePBSMeta,
  ResourcePMGMeta,
  ResourcePolicyPostureSummary,
  ResourcePhysicalDiskMeta,
  ResourcePoolHealth,
  ResourceStatus,
  ResourceStorageMeta,
  ResourceStorageRisk,
  ResourceTrueNASMeta,
  ResourceType,
  ResourceVirtualMachineMeta,
  ResourceVMwareMeta,
} from '@/types/resource';
import { normalizeDiskArray } from '@/utils/format';
import { logger } from '@/utils/logger';
import { eventBus } from '@/stores/events';
import { canonicalDiscoveryResourceType } from '@/utils/discoveryTarget';
import { canonicalizeFrontendResourceType } from '@/utils/resourceTypeCompat';
import { getPreferredNormalizedPlatformId } from '@/utils/resourceIdentity';
import { getExplicitResourceClusterName } from '@/utils/agentResources';
import type { ResourceListFacetsPayload } from '@/types/api';
import {
  buildFastResourceStorePatchOps,
  getFastResourceMergePatchKeys,
  mergeCanonicalResource,
  mergeCanonicalResourceDeltaSnapshot,
  mergeCanonicalResourceSnapshot,
  type ResourceChangedKeys,
} from '@/utils/resourceStateAdapters';
import { RESOURCE_METADATA_CHANGED_EVENT } from '@/utils/resourceMetadataEvents';
import {
  normalizeSourcePlatformScopes,
  resolvePlatformTypeFromSources,
  resolveSourceTypeFromSources,
} from '@/utils/sourcePlatforms';

const UNIFIED_RESOURCES_BASE_URL = '/api/resources';
const DEFAULT_UNIFIED_RESOURCES_QUERY =
  'type=agent,docker-host,pbs,pmg,k8s-cluster,k8s-node,network-endpoint';
const STORAGE_RECOVERY_UNIFIED_RESOURCES_QUERY =
  'type=storage,pbs,pmg,vm,system-container,pod,agent,k8s-cluster,k8s-node,physical_disk,ceph';
const UNIFIED_RESOURCES_PAGE_LIMIT = 100;
const UNIFIED_RESOURCES_CACHE_MAX_AGE_MS = 15_000;
const UNIFIED_RESOURCES_WS_DEBOUNCE_MS = 800;
const UNIFIED_RESOURCES_WS_MIN_REFETCH_INTERVAL_MS = 2_500;
const UNIFIED_RESOURCES_WS_INITIAL_HYDRATION_WAIT_MS = 1_200;
const UNIFIED_RESOURCES_WS_CANONICAL_REVALIDATE_DELAY_MS = UNIFIED_RESOURCES_CACHE_MAX_AGE_MS + 250;

type APIMetricValue = {
  observation?: MemoryObservation;
  value?: number;
  used?: number;
  total?: number;
  percent?: number;
  unit?: string;
};

type APIAgentDiskInfo = {
  device?: string;
  mountpoint?: string;
  filesystem?: string;
  type?: string;
  total?: number;
  used?: number;
  free?: number;
};

type APIAgentNetworkInterface = {
  name: string;
  mac?: string;
  addresses?: string[];
  rxBytes?: number;
  txBytes?: number;
  speedMbps?: number;
};

type APIAgentDiskIO = {
  device: string;
  readBytes?: number;
  writeBytes?: number;
  readOps?: number;
  writeOps?: number;
  readTimeMs?: number;
  writeTimeMs?: number;
  ioTimeMs?: number;
};

type APIAgentSensorSummary = {
  temperatureCelsius?: Record<string, number>;
  fanRpm?: Record<string, number>;
  additional?: Record<string, number>;
  custom?: Array<{
    id: string;
    name: string;
    unit?: string;
    value?: number;
    status: string;
    observedAt: string;
    error?: string;
    alertOnError?: boolean;
    stale?: boolean;
  }>;
  thermalState?: {
    source?: string;
    pressure?: string;
    thermalWarningLevel?: number;
    performanceWarningLevel?: number;
    cpuPowerStatus?: number;
    limitsPercent?: Record<string, number>;
  };
  smart?: Array<{
    device: string;
    model?: string;
    serial?: string;
    wwn?: string;
    type?: string;
    temperature: number;
    health?: string;
    standby?: boolean;
  }>;
};

type APIHostRAIDDevice = {
  device: string;
  state: string;
  slot: number;
};

type APIHostRAIDArray = {
  device: string;
  name?: string;
  level: string;
  state: string;
  totalDevices: number;
  activeDevices: number;
  workingDevices: number;
  failedDevices: number;
  spareDevices: number;
  uuid?: string;
  devices: APIHostRAIDDevice[];
  rebuildPercent: number;
  rebuildSpeed?: string;
};

type APIKubernetesData = {
  clusterId?: string;
  clusterName?: string;
  context?: string;
  nodeName?: string;
  namespace?: string;
  podName?: string;
  podUid?: string;
  podPhase?: string;
  podReason?: string;
  podMessage?: string;
  podContainers?: Array<{
    name?: string;
    image?: string;
    ready?: boolean;
    restartCount?: number;
    state?: string;
    reason?: string;
    message?: string;
  }>;
  restarts?: number;
  ownerKind?: string;
  ownerName?: string;
  image?: string;
  labels?: Record<string, string>;
  uptimeSeconds?: number;
  temperature?: number;
  metricCapabilities?: {
    nodeCpuMemory?: boolean;
    nodeTelemetry?: boolean;
    podCpuMemory?: boolean;
    podNetwork?: boolean;
    podEphemeralDisk?: boolean;
    podDiskIo?: boolean;
  };
};

type APIResource = {
  id: string;
  type?: string;
  name?: string;
  status?: string;
  customUrl?: string;
  uptime?: number;
  lastSeen?: string;
  parentName?: string;
  sources?: string[];
  platformScopes?: string[];
  sourceStatus?: Record<string, { status: string; lastSeen: string; error?: string }>;
  actionReadiness?: ResourceActionReadiness[];
  technology?: string;
  health?: ResourceHealth;
  identity?: {
    machineId?: string;
    hostnames?: string[];
    ipAddresses?: string[];
    clusterName?: string;
  };
  metrics?: {
    cpu?: APIMetricValue;
    memory?: APIMetricValue;
    disk?: APIMetricValue;
    netIn?: APIMetricValue;
    netOut?: APIMetricValue;
    diskRead?: APIMetricValue;
    diskWrite?: APIMetricValue;
  };
  parentId?: string;
  tags?: string[];
  proxmox?: {
    sourceId?: string;
    runtimeStatus?: string;
    nodeIdentity?: string;
    nodeName?: string;
    nodeAliases?: string[];
    nodeDisplayName?: string;
    pool?: string;
    clusterName?: string;
    isClusterMember?: boolean;
    instance?: string;
    host?: string;
    guestUrl?: string;
    connectionHealth?: string;
    pveVersion?: string;
    kernelVersion?: string;
    vmid?: number;
    cpus?: number;
    uptime?: number;
    lastBackup?: string;
    backupInProgress?: boolean;
    temperature?: number;
    temperatureDetails?: {
      available?: boolean;
      legacySensorsFormat?: boolean;
    };
    sensorSetupOutdated?: boolean;
    template?: boolean;
    containerType?: string;
    isOci?: boolean;
    disks?: APIAgentDiskInfo[];
    diskStatusReason?: string;
    guestAgentStatus?: string;
    guestAgentExpected?: boolean;
    networkInterfaces?: APIAgentNetworkInterface[];
    osName?: string;
    osVersion?: string;
    agentVersion?: string;
    osTemplate?: string;
    hasDocker?: boolean;
    dockerCheckedAt?: string;
    loadAverage?: number[];
    pendingUpdates?: number;
    temperatureMonitoringEnabled?: boolean;
    pendingUpdatesCheckedAt?: string;
    pendingUpdatesStatus?: 'checked' | 'stale' | 'unavailable' | 'not_checked';
    pendingUpdatesReason?:
      'permission_denied' | 'source_unavailable' | 'check_failed' | 'node_offline';
    swapUsed?: number;
    swapTotal?: number;
    balloon?: number;
    memoryCache?: number;
    lock?: string;
  };
  virtualMachine?: ResourceVirtualMachineMeta;
  agent?: {
    agentId?: string;
    agentVersion?: string;
    hostname?: string;
    platform?: string;
    osName?: string;
    osVersion?: string;
    kernelVersion?: string;
    architecture?: string;
    uptimeSeconds?: number;
    temperature?: number;
    cpuCount?: number;
    memory?: {
      total?: number;
      used?: number;
      free?: number;
      usage?: number;
      swapUsed?: number;
      swapTotal?: number;
    };
    networkInterfaces?: APIAgentNetworkInterface[];
    diskIO?: APIAgentDiskIO[];
    diskIo?: APIAgentDiskIO[];
    disks?: APIAgentDiskInfo[];
    sensors?: APIAgentSensorSummary;
    raid?: APIHostRAIDArray[];
    unraid?: ResourceAgentUnraidMeta;
    storageRisk?: ResourceStorageRisk;
    storageRiskSummary?: string;
    storagePostureSummary?: string;
    protectionReduced?: boolean;
    protectionSummary?: string;
    rebuildInProgress?: boolean;
    rebuildSummary?: string;
    poolHealth?: ResourcePoolHealth;
    commandsEnabled?: boolean;
    tokenId?: string;
    tokenName?: string;
    tokenHint?: string;
    tokenLastUsedAt?: number;
  };
  docker?: ResourceDockerMeta;
  truenas?: ResourceTrueNASMeta;
  pbs?: {
    instanceId?: string;
    hostname?: string;
    version?: string;
    uptimeSeconds?: number;
    datastoreCount?: number;
    backupJobCount?: number;
    syncJobCount?: number;
    verifyJobCount?: number;
    pruneJobCount?: number;
    garbageJobCount?: number;
    connectionHealth?: string;
    affectedDatastoreCount?: number;
    affectedDatastores?: string[];
    affectedDatastoreSummary?: string;
    protectedWorkloadCount?: number;
    protectedWorkloadTypes?: string[];
    protectedWorkloadNames?: string[];
    protectedWorkloadSummary?: string;
    postureSummary?: string;
    storageRisk?: {
      level?: string;
      reasons?: Array<{
        code?: string;
        severity?: string;
        summary?: string;
      }>;
    };
  };
  storage?: {
    type?: string;
    content?: string;
    contentTypes?: string[];
    shared?: boolean;
    isCeph?: boolean;
    isZfs?: boolean;
    platform?: string;
    topology?: string;
    protection?: string;
    risk?: {
      level?: string;
      reasons?: Array<{
        code?: string;
        severity?: string;
        summary?: string;
      }>;
    };
    riskSummary?: string;
    consumerCount?: number;
    consumerTypes?: string[];
    topConsumers?: Array<{
      resourceId?: string;
      resourceType?: string;
      name?: string;
      diskCount?: number;
    }>;
    consumerImpactSummary?: string;
    postureSummary?: string;
    protectionReduced?: boolean;
    protectionSummary?: string;
    rebuildInProgress?: boolean;
    rebuildSummary?: string;
    nodes?: string[];
    pool?: string;
    path?: string;
    zfsPool?: unknown;
    zfsPoolState?: string;
    zfsReadErrors?: number;
    zfsWriteErrors?: number;
    zfsChecksumErrors?: number;
    arrayState?: string;
    syncAction?: string;
    syncProgress?: number;
    numProtected?: number;
    numDisabled?: number;
    numInvalid?: number;
    numMissing?: number;
  };
  pmg?: {
    instanceId?: string;
    hostname?: string;
    version?: string;
    nodeCount?: number;
    uptimeSeconds?: number;
    queueActive?: number;
    queueDeferred?: number;
    queueHold?: number;
    queueIncoming?: number;
    queueTotal?: number;
    mailCountTotal?: number;
    spamIn?: number;
    virusIn?: number;
    connectionHealth?: string;
    lastUpdated?: string;
  };
  kubernetes?: APIKubernetesData;
  vmware?: {
    connectionId?: string;
    connectionName?: string;
    vcenterHost?: string;
    managedObjectId?: string;
    entityType?: string;
    hostUuid?: string;
    datacenterId?: string;
    datacenterName?: string;
    computeResourceId?: string;
    computeResourceName?: string;
    clusterId?: string;
    clusterName?: string;
    folderId?: string;
    folderName?: string;
    resourcePoolId?: string;
    resourcePoolName?: string;
    runtimeHostId?: string;
    runtimeHostName?: string;
    connectionState?: string;
    powerState?: string;
    overallStatus?: string;
    cpuCount?: number;
    memorySizeMib?: number;
    datastoreType?: string;
    datastoreIds?: string[];
    datastoreNames?: string[];
    datastoreUrl?: string;
    datastoreAccessible?: boolean;
    multipleHostAccess?: boolean;
    maintenanceMode?: string;
    instanceUuid?: string;
    biosUuid?: string;
    guestOsFamily?: string;
    guestHostname?: string;
    guestIpAddresses?: string[];
    activeAlarmCount?: number;
    activeAlarmSummary?: string;
    recentTaskCount?: number;
    recentTaskSummary?: string;
    snapshotCount?: number;
  };
  availability?: ResourceAvailabilityMeta;
  availabilityChecks?: ResourceAvailabilityMeta[];
  recentChanges?: ResourceChange[];
  facetCounts?: ResourceFacetCounts;
  physicalDisk?: ResourcePhysicalDiskMeta;
  ceph?: {
    fsid?: string;
    healthStatus?: string;
    healthMessage?: string;
    healthChecks?: ResourceCephHealthCheckMeta[];
    poolHealth?: ResourcePoolHealth;
    numMons?: number;
    numMgrs?: number;
    numOsds?: number;
    numOsdsUp?: number;
    numOsdsIn?: number;
    numPGs?: number;
    pools?: Array<{
      name: string;
      storedBytes: number;
      availableBytes: number;
      objects: number;
      percentUsed: number;
    }>;
    services?: Array<{
      type: string;
      running: number;
      total: number;
    }>;
  };
  discoveryTarget?: {
    resourceType?: string;
    agentId?: string;
    resourceId?: string;
    hostname?: string;
  };
  metricsTarget?: {
    resourceType?: string;
    resourceId?: string;
  };
  canonicalIdentity?: {
    displayName?: string;
    hostname?: string;
    platformId?: string;
    primaryId?: string;
    aliases?: string[];
    supersededIds?: string[];
  };
  policy?: {
    sensitivity?: string;
    routing?: {
      scope?: string;
      redact?: string[];
    };
  };
  aiSafeSummary?: string;
  incidents?: Array<{
    provider?: string;
    nativeId?: string;
    code?: string;
    severity?: string;
    source?: string;
    summary?: string;
    startedAt?: string;
    confirmationsRequired?: number;
    recoveryConfirmationsRequired?: number;
  }>;
  incidentCount?: number;
  incidentCode?: string;
  incidentSeverity?: string;
  incidentSummary?: string;
  incidentCategory?: string;
  incidentLabel?: string;
  incidentPriority?: number;
  incidentImpactSummary?: string;
  incidentUrgency?: string;
  incidentAction?: string;
};

type APIListResponse = {
  data?: APIResource[];
  resources?: APIResource[];
  meta?: {
    totalPages?: number;
  };
  aggregations?: {
    total?: number;
    byType?: Partial<Record<string, number>>;
    policyPosture?: APIResourcePolicyPostureSummary;
  };
  facets?: ResourceListFacetsPayload;
};

export type UnifiedResourceAggregations = {
  total: number;
  byType: Partial<Record<ResourceType, number>>;
};

export type UnifiedResourceFacets = {
  byType: Partial<Record<ResourceType, number>>;
  incidentCount: number;
};

type APIResourcePolicyPostureSummary = {
  totalResources?: number;
  sensitivityCounts?: Partial<Record<string, number>>;
  routingCounts?: Partial<Record<string, number>>;
  redactionCounts?: Partial<Record<string, number>>;
};

type UnifiedResourcesSnapshot = {
  resources: Resource[];
  policyPosture: ResourcePolicyPostureSummary | null;
  aggregations: UnifiedResourceAggregations | null;
  facets: UnifiedResourceFacets | null;
};

type UnifiedResourcesCacheEntry = {
  // Opt-in scoped consumers must not forget a failed read on a quick remount.
  // Retained rows remain available internally; only a successful read clears it.
  readError?: unknown;
  resources: Resource[];
  policyPosture: ResourcePolicyPostureSummary | null;
  aggregations: UnifiedResourceAggregations | null;
  facets: UnifiedResourceFacets | null;
  hasSnapshot: boolean;
  cachedAt: number;
  lastFetchAt: number;
  // When this entry last received the REST snapshot itself. Websocket ticks
  // and all-resources seeding refresh cachedAt but carry none of the
  // REST-only enrichment (action readiness, source status, scoped facets),
  // so cachedAt alone cannot say whether that enrichment is current.
  canonicalAt: number;
  sharedFetch: Promise<Resource[]> | null;
  realtimeVersion: number;
};

const unifiedResourcesCaches = new Map<string, UnifiedResourcesCacheEntry>();
const ALL_RESOURCES_CACHE_KEY = 'all-resources';

const buildScopedUnifiedResourcesCacheKey = (cacheKey: string, orgScope: string): string =>
  `${encodeURIComponent(orgScope)}::${cacheKey}`;

const resolveStatus = (status?: string): ResourceStatus => {
  const normalized = (status || '').toLowerCase();
  if (normalized === 'online' || normalized === 'running') return 'online';
  if (normalized === 'offline' || normalized === 'stopped') return 'offline';
  if (normalized === 'warning' || normalized === 'degraded') return 'degraded';
  if (normalized === 'paused') return 'paused';
  return 'unknown';
};

const resolveType = (value?: string): ResourceType => {
  const normalized = (value || '').toLowerCase();
  const canonicalFrontendType = canonicalizeFrontendResourceType(normalized);
  switch (canonicalFrontendType) {
    case 'agent':
    case 'storage':
    case 'docker-host':
    case 'docker-image':
    case 'docker-volume':
    case 'docker-network':
    case 'docker-task':
    case 'docker-swarm-node':
    case 'docker-secret':
    case 'docker-config':
    case 'k8s-cluster':
    case 'k8s-node':
    case 'k8s-deployment':
    case 'k8s-replicaset':
    case 'k8s-service':
    case 'k8s-namespace':
    case 'k8s-statefulset':
    case 'k8s-daemonset':
    case 'k8s-job':
    case 'k8s-cronjob':
    case 'k8s-ingress':
    case 'k8s-endpoint-slice':
    case 'k8s-network-policy':
    case 'k8s-persistent-volume':
    case 'k8s-persistent-volume-claim':
    case 'k8s-storage-class':
    case 'k8s-configmap':
    case 'k8s-secret':
    case 'k8s-serviceaccount':
    case 'k8s-resource-quota':
    case 'k8s-limit-range':
    case 'k8s-pod-disruption-budget':
    case 'k8s-horizontal-pod-autoscaler':
    case 'k8s-event':
    case 'vm':
    case 'system-container':
    case 'oci-container':
    case 'app-container':
    case 'pod':
    case 'pbs':
    case 'pmg':
    case 'ceph':
    case 'network':
    case 'network-share':
    case 'network-endpoint':
      return canonicalFrontendType;
    case 'disk':
      return 'physical_disk';
    default:
      break;
  }

  switch (normalized) {
    case 'jail':
      return 'jail';
    case 'docker-service':
      return 'docker-service';
    case 'docker-image':
      return 'docker-image';
    case 'docker-volume':
      return 'docker-volume';
    case 'docker-network':
      return 'docker-network';
    case 'docker-task':
      return 'docker-task';
    case 'docker-swarm-node':
      return 'docker-swarm-node';
    case 'docker-secret':
      return 'docker-secret';
    case 'docker-config':
      return 'docker-config';
    case 'k8s-deployment':
      return 'k8s-deployment';
    case 'k8s-replicaset':
      return 'k8s-replicaset';
    case 'k8s-service':
      return 'k8s-service';
    case 'k8s-namespace':
      return 'k8s-namespace';
    case 'k8s-statefulset':
      return 'k8s-statefulset';
    case 'k8s-daemonset':
      return 'k8s-daemonset';
    case 'k8s-job':
      return 'k8s-job';
    case 'k8s-cronjob':
      return 'k8s-cronjob';
    case 'k8s-ingress':
      return 'k8s-ingress';
    case 'k8s-endpoint-slice':
      return 'k8s-endpoint-slice';
    case 'k8s-network-policy':
      return 'k8s-network-policy';
    case 'k8s-persistent-volume':
      return 'k8s-persistent-volume';
    case 'k8s-persistent-volume-claim':
      return 'k8s-persistent-volume-claim';
    case 'k8s-storage-class':
      return 'k8s-storage-class';
    case 'k8s-configmap':
      return 'k8s-configmap';
    case 'k8s-secret':
      return 'k8s-secret';
    case 'k8s-serviceaccount':
      return 'k8s-serviceaccount';
    case 'k8s-resource-quota':
      return 'k8s-resource-quota';
    case 'k8s-limit-range':
      return 'k8s-limit-range';
    case 'k8s-pod-disruption-budget':
      return 'k8s-pod-disruption-budget';
    case 'k8s-horizontal-pod-autoscaler':
      return 'k8s-horizontal-pod-autoscaler';
    case 'k8s-event':
      return 'k8s-event';
    case 'storage':
      return 'storage';
    case 'datastore':
      return 'datastore';
    case 'pool':
      return 'pool';
    case 'dataset':
      return 'dataset';
    case 'physical_disk':
    case 'physical-disk':
      return 'physical_disk';
    case 'network-endpoint':
    case 'network_endpoint':
    case 'availability':
      return 'network-endpoint';
    default:
      return 'agent';
  }
};

const resolveDiscoveryResourceType = (
  value?: string,
): ResourceDiscoveryTarget['resourceType'] | undefined => {
  const normalized = canonicalDiscoveryResourceType(value);
  switch (normalized) {
    case 'agent':
      return 'agent';
    case 'vm':
      return 'vm';
    case 'system-container':
      return 'system-container';
    case 'app-container':
      return 'app-container';
    case 'pod':
      return 'pod';
    case 'disk':
      return 'disk';
    case 'ceph':
      return 'ceph';
    default:
      return undefined;
  }
};

const resolveMetricsTarget = (
  resourceType: string | undefined,
  metricsTarget?: { resourceType?: string; resourceId?: string },
): ResourceMetricsTarget | undefined => {
  const canonicalType = metricsTarget?.resourceType
    ? canonicalizeMetricsHistoryTargetType(metricsTarget.resourceType, resourceType)
    : null;
  const resourceID = asTrimmedString(metricsTarget?.resourceId);
  return canonicalType && resourceID
    ? { resourceType: canonicalType, resourceId: resourceID }
    : undefined;
};

const metricToResourceMetric = (metric?: APIMetricValue) => {
  if (!metric) return undefined;
  const used = metric.used ?? undefined;
  const total = metric.total ?? undefined;
  const current = metric.percent ?? metric.value ?? 0;
  const observation = readMemoryObservation(metric.observation);
  const free = total !== undefined && used !== undefined ? total - used : undefined;
  return {
    current,
    ...(observation ? { observation } : {}),
    total,
    used,
    free,
  };
};

const toResource = (v2: APIResource): Resource => {
  const sources = (v2.sources || []).filter(
    (s): s is string => typeof s === 'string' && s.trim().length > 0,
  );
  const lastSeen = v2.lastSeen ? Date.parse(v2.lastSeen) : NaN;
  const canonical = v2.canonicalIdentity;
  const name = asTrimmedString(canonical?.displayName) || v2.name || v2.id;
  const platformId = asTrimmedString(canonical?.platformId) || getPreferredNormalizedPlatformId(v2);
  const resourceType = resolveType(v2.type);
  const platformType =
    resolvePlatformTypeFromSources(sources) ||
    (resourceType === 'network-endpoint' ? 'availability' : 'agent');
  const platformScopes = normalizeSourcePlatformScopes(v2.platformScopes, platformType);

  const discoveryResourceType = resolveDiscoveryResourceType(v2.discoveryTarget?.resourceType);
  const discoveryAgentId = v2.discoveryTarget?.agentId;
  const discoveryTarget =
    discoveryResourceType && discoveryAgentId && v2.discoveryTarget?.resourceId
      ? {
          resourceType: discoveryResourceType,
          agentId: discoveryAgentId,
          resourceId: v2.discoveryTarget.resourceId,
          hostname: v2.discoveryTarget.hostname,
        }
      : undefined;

  const metricsTarget = resolveMetricsTarget(v2.type, v2.metricsTarget);
  return {
    id: v2.id,
    type: resourceType,
    name,
    displayName: name,
    platformId,
    platformType,
    platformScopes,
    sources,
    sourceType: resolveSourceTypeFromSources(sources),
    parentId: v2.parentId,
    parentName: v2.parentName,
    clusterId: getExplicitResourceClusterName(v2),
    status: resolveStatus(v2.status),
    health: v2.health,
    incidents: (v2.incidents || [])
      .map((incident) => ({
        provider: incident.provider,
        nativeId: incident.nativeId,
        code: incident.code || '',
        severity: incident.severity || '',
        source: incident.source,
        summary: incident.summary || '',
        startedAt: incident.startedAt,
        confirmationsRequired: incident.confirmationsRequired,
        recoveryConfirmationsRequired: incident.recoveryConfirmationsRequired,
      }))
      .filter((incident) => incident.code.trim() || incident.summary.trim()),
    incidentCount: v2.incidentCount,
    incidentCode: v2.incidentCode,
    incidentSeverity: v2.incidentSeverity,
    incidentSummary: v2.incidentSummary,
    incidentCategory: v2.incidentCategory,
    incidentLabel: v2.incidentLabel,
    incidentPriority: v2.incidentPriority,
    incidentImpactSummary: v2.incidentImpactSummary,
    incidentUrgency: v2.incidentUrgency,
    incidentAction: v2.incidentAction,
    agent: v2.agent,
    kubernetes: v2.kubernetes,
    docker: v2.docker as ResourceDockerMeta | undefined,
    truenas: v2.truenas as ResourceTrueNASMeta | undefined,
    vmware: v2.vmware as ResourceVMwareMeta | undefined,
    pbs: v2.pbs as ResourcePBSMeta | undefined,
    // Mail Gateway rows read `resource.pmg` directly. Leaving it only under
    // platformData meant a REST-first paint rendered every PMG column as a
    // dash until a websocket row happened to replace it, and never did when
    // the REST snapshot landed last.
    pmg: v2.pmg as ResourcePMGMeta | undefined,
    // Same race for the per-source status, action readiness, and technology
    // facets: Docker lifecycle actions and storage freshness read them from
    // the top level, and the websocket rows carry them there.
    sourceStatus: v2.sourceStatus,
    actionReadiness: v2.actionReadiness,
    technology: v2.technology,
    availability: v2.availability as ResourceAvailabilityMeta | undefined,
    availabilityChecks: v2.availabilityChecks as ResourceAvailabilityMeta[] | undefined,
    physicalDisk: v2.physicalDisk,
    storage: v2.storage as ResourceStorageMeta | undefined,
    ceph: v2.ceph as ResourceCephMeta | undefined,
    proxmox: v2.proxmox
      ? {
          // Keep the canonical provider facet intact. The Proxmox overview
          // adapts this Resource back into a workload row, so dropping fields
          // here makes the first REST paint disagree with the later websocket
          // merge (notably uptime and completed-backup state).
          ...v2.proxmox,
          sourceId: v2.proxmox.sourceId,
          vmid: v2.proxmox.vmid,
          node: v2.proxmox.nodeName,
          nodeName: v2.proxmox.nodeName,
          instance: v2.proxmox.instance,
          clusterName: v2.proxmox.clusterName,
          host: v2.proxmox.host,
          guestUrl: v2.proxmox.guestUrl,
          connectionHealth: v2.proxmox.connectionHealth,
          cpus: v2.proxmox.cpus,
          template: v2.proxmox.template,
          disks: normalizeDiskArray(v2.proxmox.disks),
          networkInterfaces: v2.proxmox.networkInterfaces,
          swapUsed: v2.proxmox.swapUsed,
          swapTotal: v2.proxmox.swapTotal,
          balloon: v2.proxmox.balloon,
          pveVersion: v2.proxmox.pveVersion,
          kernelVersion: v2.proxmox.kernelVersion,
          temperatureDetails: v2.proxmox.temperatureDetails,
        }
      : undefined,
    virtualMachine: v2.virtualMachine,
    cpu: metricToResourceMetric(v2.metrics?.cpu),
    memory: metricToResourceMetric(v2.metrics?.memory),
    disk: metricToResourceMetric(v2.metrics?.disk),
    network:
      v2.metrics?.netIn || v2.metrics?.netOut
        ? {
            rxBytes: v2.metrics?.netIn?.value ?? 0,
            txBytes: v2.metrics?.netOut?.value ?? 0,
          }
        : undefined,
    diskIO:
      v2.metrics?.diskRead || v2.metrics?.diskWrite
        ? {
            readRate: v2.metrics?.diskRead?.value,
            writeRate: v2.metrics?.diskWrite?.value,
          }
        : undefined,
    uptime:
      v2.agent?.uptimeSeconds ??
      v2.proxmox?.uptime ??
      v2.pbs?.uptimeSeconds ??
      v2.pmg?.uptimeSeconds ??
      v2.kubernetes?.uptimeSeconds ??
      // Canonical Resource.Uptime is the universal fallback — vSphere
      // adapters populate only this field (no platform-specific carve-out
      // for hosts/datastores/networks), so the chain has to land here for
      // ESXi host uptime to surface on the unified-resources side. Same
      // fallback shape as the workloads mapping in useWorkloads.ts.
      v2.uptime,
    temperature:
      v2.agent?.temperature ??
      v2.proxmox?.temperature ??
      v2.docker?.temperature ??
      v2.kubernetes?.temperature ??
      v2.physicalDisk?.temperature,
    customUrl: asTrimmedString(v2.customUrl),
    tags: v2.tags,
    lastSeen: Number.isFinite(lastSeen) ? lastSeen : Date.now(),
    identity: {
      hostname: asTrimmedString(canonical?.hostname) || v2.identity?.hostnames?.[0],
      machineId: v2.identity?.machineId,
      ips: v2.identity?.ipAddresses,
    },
    discoveryTarget,
    metricsTarget,
    canonicalIdentity: v2.canonicalIdentity,
    policy: v2.policy as Resource['policy'],
    aiSafeSummary: v2.aiSafeSummary as Resource['aiSafeSummary'],
    recentChanges: v2.recentChanges,
    facetCounts: v2.facetCounts,
    platformData: {
      sources,
      platformScopes,
      sourceStatus: v2.sourceStatus,
      proxmox: v2.proxmox,
      agent: v2.agent,
      docker: v2.docker,
      truenas: v2.truenas,
      pbs: v2.pbs,
      storage: v2.storage,
      pmg: v2.pmg,
      kubernetes: v2.kubernetes,
      vmware: v2.vmware,
      availability: v2.availability,
      availabilityChecks: v2.availabilityChecks,
      physicalDisk: v2.physicalDisk,
      ceph: v2.ceph,
      metrics: v2.metrics,
      discoveryTarget: v2.discoveryTarget,
    },
  };
};

const normalizeUnifiedResourcesQuery = (query?: string): string =>
  (query || '').trim().replace(/^\?+/, '');

const buildUnifiedResourcesUrl = (query: string, page: number): string => {
  const params = new URLSearchParams(query);
  params.set('page', String(page));
  params.set('limit', String(UNIFIED_RESOURCES_PAGE_LIMIT));
  return `${UNIFIED_RESOURCES_BASE_URL}?${params.toString()}`;
};

const normalizeNonNegativeCount = (value: unknown): number | undefined => {
  const count = typeof value === 'number' ? value : Number(value);
  if (!Number.isFinite(count)) {
    return undefined;
  }
  return Math.max(0, Math.trunc(count));
};

const normalizeCountMap = <T extends string>(value: unknown): Partial<Record<T, number>> => {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    return {};
  }

  const counts: Partial<Record<T, number>> = {};
  for (const [key, rawCount] of Object.entries(value as Record<string, unknown>)) {
    const normalizedKey = asTrimmedString(key);
    const normalizedCount = normalizeNonNegativeCount(rawCount);
    if (normalizedKey && normalizedCount !== undefined) {
      counts[normalizedKey as T] = normalizedCount;
    }
  }
  return counts;
};

const normalizeResourcePolicyPosture = (
  value?: APIResourcePolicyPostureSummary | null,
): ResourcePolicyPostureSummary | null => {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    return null;
  }

  return {
    totalResources: normalizeNonNegativeCount(value.totalResources) ?? 0,
    sensitivityCounts: normalizeCountMap(value.sensitivityCounts),
    routingCounts: normalizeCountMap(value.routingCounts),
    redactionCounts: normalizeCountMap(value.redactionCounts),
  };
};

const normalizeUnifiedResourceAggregations = (
  value: APIListResponse['aggregations'],
): UnifiedResourceAggregations | null => {
  if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
  const byType: Partial<Record<ResourceType, number>> = {};
  for (const [rawType, rawCount] of Object.entries(value.byType ?? {})) {
    const count = normalizeNonNegativeCount(rawCount);
    if (count === undefined) continue;
    byType[resolveType(rawType)] = count;
  }
  return {
    total: normalizeNonNegativeCount(value.total) ?? 0,
    byType,
  };
};

const normalizeUnifiedResourceFacets = (
  value: APIListResponse['facets'],
): UnifiedResourceFacets | null => {
  if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
  const byType: Partial<Record<ResourceType, number>> = {};
  for (const [rawType, rawCount] of Object.entries(value.byType ?? {})) {
    const count = normalizeNonNegativeCount(rawCount);
    if (count === undefined) continue;
    byType[resolveType(rawType)] = count;
  }
  return {
    byType,
    incidentCount: normalizeNonNegativeCount(value.incidentCount) ?? 0,
  };
};

const resolveResourcesPayload = (
  payload: unknown,
): {
  data: APIResource[];
  totalPages: number;
  policyPosture: ResourcePolicyPostureSummary | null;
  aggregations: UnifiedResourceAggregations | null;
  facets: UnifiedResourceFacets | null;
} => {
  if (Array.isArray(payload)) {
    return {
      data: payload as APIResource[],
      totalPages: 1,
      policyPosture: null,
      aggregations: null,
      facets: null,
    };
  }
  if (!payload || typeof payload !== 'object') {
    return {
      data: [],
      totalPages: 1,
      policyPosture: null,
      aggregations: null,
      facets: null,
    };
  }
  const record = payload as APIListResponse;
  const data = Array.isArray(record.data)
    ? record.data
    : Array.isArray(record.resources)
      ? record.resources
      : [];
  const totalPages = Number.isFinite(record.meta?.totalPages)
    ? Math.max(1, Number(record.meta?.totalPages))
    : 1;
  return {
    data,
    totalPages,
    policyPosture: normalizeResourcePolicyPosture(record.aggregations?.policyPosture),
    aggregations: normalizeUnifiedResourceAggregations(record.aggregations),
    facets: normalizeUnifiedResourceFacets(record.facets),
  };
};

const dedupeResources = (resources: APIResource[]): APIResource[] => {
  const ids = new Set<string>();
  const deduped: APIResource[] = [];
  for (const resource of resources) {
    if (!resource?.id || ids.has(resource.id)) continue;
    ids.add(resource.id);
    deduped.push(resource);
  }
  return deduped;
};

const getUnifiedResourcesCacheEntry = (cacheKey: string): UnifiedResourcesCacheEntry => {
  const existing = unifiedResourcesCaches.get(cacheKey);
  if (existing) {
    return existing;
  }
  const created: UnifiedResourcesCacheEntry = {
    resources: [],
    policyPosture: null,
    aggregations: null,
    facets: null,
    hasSnapshot: false,
    cachedAt: 0,
    lastFetchAt: 0,
    canonicalAt: 0,
    sharedFetch: null,
    realtimeVersion: 0,
  };
  unifiedResourcesCaches.set(cacheKey, created);
  return created;
};

const hasFreshUnifiedResourcesCache = (entry: UnifiedResourcesCacheEntry) =>
  entry.hasSnapshot && Date.now() - entry.cachedAt <= UNIFIED_RESOURCES_CACHE_MAX_AGE_MS;

const hasFreshCanonicalSnapshot = (entry: UnifiedResourcesCacheEntry) =>
  entry.canonicalAt > 0 && Date.now() - entry.canonicalAt <= UNIFIED_RESOURCES_CACHE_MAX_AGE_MS;

const setUnifiedResourcesCache = (
  entry: UnifiedResourcesCacheEntry,
  resources: Resource[],
  at = Date.now(),
  policyPosture: ResourcePolicyPostureSummary | null = entry.policyPosture,
  aggregations: UnifiedResourceAggregations | null = entry.aggregations,
  facets: UnifiedResourceFacets | null = entry.facets,
) => {
  entry.resources = resources;
  entry.policyPosture = policyPosture;
  entry.aggregations = aggregations;
  entry.facets = facets;
  entry.hasSnapshot = true;
  entry.cachedAt = at;
};

const enrichCanonicalAllResourcesCache = (
  entry: UnifiedResourcesCacheEntry,
  resources: Resource[],
) => {
  if (!entry.hasSnapshot || resources.length === 0) return;

  const incomingById = new Map(resources.map((resource) => [resource.id, resource] as const));
  const enriched = entry.resources.map((existing) => {
    const incoming = incomingById.get(existing.id);
    if (!incoming) return existing;
    incomingById.delete(existing.id);
    return mergeCanonicalResource(incoming, existing);
  });
  enriched.push(...incomingById.values());
  setUnifiedResourcesCache(entry, enriched);
};

type UnifiedResourcesLocalFilter = {
  types: Set<ResourceType>;
  sources: Set<string>;
  excludedSources: Set<string>;
};

const normalizeUnifiedResourceSource = (source: string): string => {
  const normalized = source.trim().toLowerCase();
  switch (normalized) {
    case 'k8s':
      return 'kubernetes';
    case 'vmware-vsphere':
      return 'vmware';
    default:
      return normalized;
  }
};

const SUPPORTED_UNIFIED_RESOURCE_SOURCES = new Set([
  'proxmox',
  'agent',
  'docker',
  'pbs',
  'pmg',
  'kubernetes',
  'truenas',
  'vmware',
  'availability',
]);

const parseUnifiedResourcesLocalFilter = (query: string): UnifiedResourcesLocalFilter | null => {
  const normalizedQuery = normalizeUnifiedResourcesQuery(query);
  if (normalizedQuery === '') {
    return null;
  }

  const params = new URLSearchParams(normalizedQuery);
  const types = new Set<ResourceType>();
  const sources = new Set<string>();
  const excludedSources = new Set<string>();

  for (const [key, value] of params.entries()) {
    if (key !== 'type' && key !== 'source' && key !== 'excludeSource') {
      return null;
    }

    value
      .split(',')
      .map((candidate) => asTrimmedString(candidate))
      .filter((candidate): candidate is string => candidate !== undefined)
      .forEach((candidate) => {
        if (key === 'type') {
          types.add(resolveType(candidate));
          return;
        }
        const normalizedSource = normalizeUnifiedResourceSource(candidate);
        if (SUPPORTED_UNIFIED_RESOURCE_SOURCES.has(normalizedSource)) {
          if (key === 'excludeSource') {
            excludedSources.add(normalizedSource);
          } else {
            sources.add(normalizedSource);
          }
        }
      });
  }

  return types.size > 0 || sources.size > 0 || excludedSources.size > 0
    ? { types, sources, excludedSources }
    : null;
};

const filterCanonicalUnifiedResources = (
  resources: Resource[],
  query: string,
  localFilter: UnifiedResourcesLocalFilter | null,
): Resource[] | null => {
  const normalizedQuery = normalizeUnifiedResourcesQuery(query);
  if (normalizedQuery === '') {
    return resources;
  }
  if (!localFilter) {
    return null;
  }
  return resources.filter((resource) => {
    if (localFilter.types.size > 0 && !localFilter.types.has(resolveType(resource.type))) {
      return false;
    }
    const platformSources = resource.platformData?.sources;
    const resourceSources = [
      ...(resource.sources ?? []),
      ...(Array.isArray(platformSources)
        ? platformSources.filter((source): source is string => typeof source === 'string')
        : []),
    ].map(normalizeUnifiedResourceSource);
    if (
      localFilter.sources.size > 0 &&
      !resourceSources.some((source) => localFilter.sources.has(source))
    ) {
      return false;
    }
    return !resourceSources.some((source) => localFilter.excludedSources.has(source));
  });
};

const buildUnifiedResourceFacets = (resources: readonly Resource[]): UnifiedResourceFacets => {
  const byType: Partial<Record<ResourceType, number>> = {};
  let incidentCount = 0;
  for (const resource of resources) {
    const type = resolveType(resource.type);
    byType[type] = (byType[type] ?? 0) + 1;
    incidentCount += resource.incidentCount ?? 0;
  }
  return { byType, incidentCount };
};

/**
 * Resolve the small set of store rows that need reconciliation for one
 * canonical realtime revision. A structural mismatch returns `null` so the
 * caller can fall back to a full keyed reconcile for additions, removals, type
 * membership changes, or order changes. Each server row is one display row,
 * so only the changed rows need reconciling.
 */
export const resolveIncrementalResourcePatchIndices = (
  current: readonly Resource[],
  next: readonly Resource[],
  changedIds: ReadonlySet<string>,
): number[] | null => {
  if (current.length !== next.length) return null;

  const patchIndices: number[] = [];
  for (let index = 0; index < next.length; index += 1) {
    const currentResource = current[index];
    const nextResource = next[index];
    if (!currentResource || !nextResource || currentResource.id !== nextResource.id) {
      return null;
    }
    if (changedIds.has(nextResource.id)) {
      patchIndices.push(index);
    }
  }
  return patchIndices;
};

const seedUnifiedResourcesCacheFromAllResources = (
  entry: UnifiedResourcesCacheEntry,
  cacheKey: string,
  query: string,
  orgScope: string,
) => {
  if (entry.hasSnapshot || cacheKey === ALL_RESOURCES_CACHE_KEY) {
    return entry;
  }

  const localFilter = parseUnifiedResourcesLocalFilter(query);
  if (!localFilter) {
    return entry;
  }

  const allResourcesEntry = getUnifiedResourcesCacheEntry(
    buildScopedUnifiedResourcesCacheKey(ALL_RESOURCES_CACHE_KEY, orgScope),
  );
  if (!hasFreshUnifiedResourcesCache(allResourcesEntry)) {
    return entry;
  }

  entry.resources =
    filterCanonicalUnifiedResources(allResourcesEntry.resources, query, localFilter) ?? [];
  entry.policyPosture = allResourcesEntry.policyPosture;
  entry.aggregations = allResourcesEntry.aggregations;
  // The all-resources cache cannot supply source-scoped type facets without
  // replaying the server's non-type filter semantics. Keep them unknown until
  // the scoped REST revalidation returns the authoritative facet bundle.
  entry.facets = null;
  entry.hasSnapshot = true;
  entry.cachedAt = allResourcesEntry.cachedAt;
  entry.lastFetchAt = allResourcesEntry.lastFetchAt;

  return entry;
};

const fetchUnifiedResourcesPage = async (
  normalizedQuery: string,
  page: number,
): Promise<ReturnType<typeof resolveResourcesPayload>> => {
  const response = await apiFetch(buildUnifiedResourcesUrl(normalizedQuery, page), {
    cache: 'no-store',
  });
  if (!response.ok) {
    const message = await readAPIErrorMessage(
      response,
      `Failed to fetch unified resources (HTTP ${response.status})`,
    );
    throw new Error(message);
  }

  const payload = (await response.json()) as APIListResponse | APIResource[];
  return resolveResourcesPayload(payload);
};

async function fetchUnifiedResources(query: string): Promise<UnifiedResourcesSnapshot> {
  const normalizedQuery = normalizeUnifiedResourcesQuery(query);

  // Only the first page has to be fetched before we know how many there are.
  // Every page after it is independent, so fan them out instead of walking them
  // nose-to-tail: a serial walk costs a full round trip per page before the
  // first paint can happen, which is seconds of blank screen on any estate
  // larger than one page. The outer loop repeats only when the estate grew a
  // page while the fan-out was in flight, preserving the previous
  // keep-walking-while-totalPages-grows behaviour.
  const pages = [await fetchUnifiedResourcesPage(normalizedQuery, 1)];
  let totalPages = pages[0].totalPages;

  while (pages.length < totalPages) {
    const firstMissingPage = pages.length + 1;
    const fetched = await Promise.all(
      Array.from({ length: totalPages - pages.length }, (_, index) =>
        fetchUnifiedResourcesPage(normalizedQuery, firstMissingPage + index),
      ),
    );
    pages.push(...fetched);
    totalPages = fetched.reduce((max, resolved) => Math.max(max, resolved.totalPages), totalPages);
  }

  const allRawResources: APIResource[] = [];
  let policyPosture: ResourcePolicyPostureSummary | null = null;
  let aggregations: UnifiedResourceAggregations | null = null;
  let facets: UnifiedResourceFacets | null = null;
  for (const resolved of pages) {
    allRawResources.push(...resolved.data);
    policyPosture = policyPosture ?? resolved.policyPosture;
    aggregations = aggregations ?? resolved.aggregations;
    facets = facets ?? resolved.facets;
  }

  return {
    resources: dedupeResources(allRawResources).map((resource) => toResource(resource)),
    policyPosture,
    aggregations,
    facets,
  };
}

const fetchUnifiedResourcesShared = async (
  entry: UnifiedResourcesCacheEntry,
  query: string,
  force = false,
): Promise<Resource[]> => {
  if (!force && hasFreshUnifiedResourcesCache(entry)) {
    return entry.resources;
  }

  if (entry.sharedFetch) {
    return entry.sharedFetch;
  }

  const request = (async () => {
    const fetched = await fetchUnifiedResources(query);
    const now = Date.now();
    setUnifiedResourcesCache(
      entry,
      fetched.resources,
      now,
      fetched.policyPosture,
      fetched.aggregations,
      fetched.facets,
    );
    entry.lastFetchAt = now;
    entry.canonicalAt = now;
    return fetched.resources;
  })();

  entry.sharedFetch = request;

  try {
    return await request;
  } finally {
    if (entry.sharedFetch === request) {
      entry.sharedFetch = null;
    }
  }
};

const shouldThrottleWsRefetch = (entry: UnifiedResourcesCacheEntry) =>
  Date.now() - entry.lastFetchAt < UNIFIED_RESOURCES_WS_MIN_REFETCH_INTERVAL_MS;

export const __resetUnifiedResourcesCacheForTests = () => {
  unifiedResourcesCaches.clear();
};

export const getCachedUnifiedResources = (options?: {
  cacheKey?: string;
  orgID?: string | null;
}): Resource[] => {
  const cacheKey = (options?.cacheKey || ALL_RESOURCES_CACHE_KEY).trim();
  const scopedCacheKey = buildScopedUnifiedResourcesCacheKey(
    cacheKey,
    normalizeOrgScope(options?.orgID ?? getOrgID()),
  );
  return getUnifiedResourcesCacheEntry(scopedCacheKey).resources;
};

type UnifiedResourcesInitialHydration = 'immediate' | 'prefer-ws' | 'prefer-ws-then-rest';

type UseUnifiedResourcesOptions = {
  query?: string;
  cacheKey?: string;
  initialHydration?: UnifiedResourcesInitialHydration;
  enabled?: Accessor<boolean>;
  /** Keep cached/background data inert while another retained route is visible. */
  realtimeEnabled?: Accessor<boolean>;
  /** Retain scoped read failures across remounts until a successful read. */
  retainReadError?: boolean;
};

export function useUnifiedResources(options?: UseUnifiedResourcesOptions) {
  const query = normalizeUnifiedResourcesQuery(options?.query ?? DEFAULT_UNIFIED_RESOURCES_QUERY);
  const cacheKey = (options?.cacheKey || query || 'all').trim();
  const initialHydration = options?.initialHydration ?? 'immediate';
  const retainReadError = options?.retainReadError === true;
  const enabled = options?.enabled ?? (() => true);
  const realtimeEnabled = options?.realtimeEnabled ?? enabled;
  const localFilter = parseUnifiedResourcesLocalFilter(query);
  const supportsCanonicalWsHydration = query === '' || localFilter !== null;
  const prefersWsInitialHydration =
    (initialHydration === 'prefer-ws' || initialHydration === 'prefer-ws-then-rest') &&
    supportsCanonicalWsHydration;
  const revalidatesRestAfterWsInitialHydration =
    initialHydration === 'prefer-ws-then-rest' && supportsCanonicalWsHydration;
  const [orgScope, setOrgScope] = createSignal(normalizeOrgScope(getOrgID()));
  const resolveScopedCacheKey = () => buildScopedUnifiedResourcesCacheKey(cacheKey, orgScope());
  let cacheEntry = seedUnifiedResourcesCacheFromAllResources(
    getUnifiedResourcesCacheEntry(resolveScopedCacheKey()),
    cacheKey,
    query,
    orgScope(),
  );
  const initialResources = cacheEntry.resources;
  const initialPolicyPosture = cacheEntry.policyPosture;
  const initialAggregations = cacheEntry.aggregations;
  const initialFacets = cacheEntry.facets;
  const hasCachedResources = cacheEntry.hasSnapshot;

  const [resources, setResources] = createStore<Resource[]>(initialResources);
  const [resourceSnapshotChange, setResourceSnapshotChange] = createSignal<{
    version: number;
    changedIds: ReadonlySet<string> | null;
  }>({ version: 0, changedIds: null });
  let resourceSnapshotVersion = 0;
  const [policyPosture, setPolicyPosture] = createSignal<ResourcePolicyPostureSummary | null>(
    initialPolicyPosture,
  );
  const [aggregations, setAggregations] = createSignal<UnifiedResourceAggregations | null>(
    initialAggregations,
  );
  const [facets, setFacets] = createSignal<UnifiedResourceFacets | null>(initialFacets);
  const [loading, setLoading] = createSignal(!hasCachedResources);
  const [error, setError] = createSignal<unknown>(
    retainReadError ? cacheEntry.readError : undefined,
  );
  const wsStore = getGlobalWebSocketStore();
  let refreshHandle: ReturnType<typeof setTimeout> | undefined;
  let initialHydrationHandle: ReturnType<typeof setTimeout> | undefined;
  let canonicalRevalidationHandle: ReturnType<typeof setTimeout> | undefined;
  let inFlightRefetch: Promise<Resource[]> | null = null;
  let wsInitialized = false;
  let lastWsUpdateToken = '';
  let blockedWsHydrationToken: string | null = null;
  let scopeVersion = 0;

  const applyResources = (
    next: Resource[],
    targetEntry: UnifiedResourcesCacheEntry = cacheEntry,
  ) => {
    setUnifiedResourcesCache(targetEntry, next);
    if (targetEntry !== cacheEntry) {
      return;
    }
    setResources(reconcile(next, { key: 'id' }));
    setResourceSnapshotChange({ version: ++resourceSnapshotVersion, changedIds: null });
    setPolicyPosture(targetEntry.policyPosture);
    setAggregations(targetEntry.aggregations);
    setFacets(targetEntry.facets);
  };

  const runRefetch = async (options?: {
    force?: boolean;
    source?: 'initial' | 'ws' | 'manual';
    background?: boolean;
  }) => {
    if (!enabled()) {
      return resources as unknown as Resource[];
    }
    if (inFlightRefetch) {
      return inFlightRefetch;
    }

    const force = options?.force === true;
    const source = options?.source ?? 'manual';
    const background = options?.background === true;

    if (!force && source === 'ws' && shouldThrottleWsRefetch(cacheEntry)) {
      return resources as unknown as Resource[];
    }

    const shouldForceNetwork = force || source === 'ws';
    const shouldShowLoading = !background && (force || !cacheEntry.hasSnapshot);
    if (shouldShowLoading) {
      setLoading(true);
    }

    const requestVersion = scopeVersion;
    const entryForRequest = cacheEntry;
    let request!: Promise<Resource[]>;
    request = (async () => {
      const isCurrentRequest = () =>
        requestVersion === scopeVersion && entryForRequest === cacheEntry && enabled();

      try {
        const fetched = await fetchUnifiedResourcesShared(
          entryForRequest,
          query,
          shouldForceNetwork,
        );
        if (!isCurrentRequest()) {
          return resources as unknown as Resource[];
        }
        const allResourcesEntry = getUnifiedResourcesCacheEntry(
          buildScopedUnifiedResourcesCacheKey(ALL_RESOURCES_CACHE_KEY, orgScope()),
        );
        if (allResourcesEntry !== entryForRequest) {
          enrichCanonicalAllResourcesCache(allResourcesEntry, fetched);
          entryForRequest.realtimeVersion = allResourcesEntry.realtimeVersion;
        }
        if (retainReadError) entryForRequest.readError = undefined;
        batch(() => {
          applyResources(fetched, entryForRequest);
          setError(undefined);
        });
        return fetched;
      } catch (err) {
        if (isCurrentRequest()) {
          if (retainReadError) entryForRequest.readError = err;
          if (!background || retainReadError) setError(err);
        }
        throw err;
      } finally {
        if (inFlightRefetch === request) {
          inFlightRefetch = null;
        }
        if (shouldShowLoading && isCurrentRequest()) {
          setLoading(false);
        }
      }
    })();

    inFlightRefetch = request;
    return request;
  };

  const refetch = async () => {
    if (!enabled()) {
      return resources as unknown as Resource[];
    }
    return runRefetch({ force: true, source: 'manual' });
  };

  const mutate = (value: Resource[] | ((prev: Resource[]) => Resource[])) => {
    const current = resources as unknown as Resource[];
    const next = typeof value === 'function' ? value(current) : value;
    applyResources(next ?? []);
    return resources as unknown as Resource[];
  };

  const clearInitialHydrationTimeout = () => {
    if (initialHydrationHandle !== undefined) {
      clearTimeout(initialHydrationHandle);
      initialHydrationHandle = undefined;
    }
  };

  const clearRefreshTimeout = () => {
    if (refreshHandle !== undefined) {
      clearTimeout(refreshHandle);
      refreshHandle = undefined;
    }
  };

  const clearCanonicalRevalidationTimeout = () => {
    if (canonicalRevalidationHandle !== undefined) {
      clearTimeout(canonicalRevalidationHandle);
      canonicalRevalidationHandle = undefined;
    }
  };

  const shouldPreferWsInitialHydration = () => prefersWsInitialHydration && !cacheEntry.hasSnapshot;

  const hasWsInitialHydrationSnapshot = () =>
    wsStore.connected() && wsStore.initialDataReceived() && Array.isArray(wsStore.state.resources);

  // Holding the canonical fetch back only pays off for routes that would
  // otherwise never issue it. `prefer-ws-then-rest` routes always follow their
  // websocket paint with a REST revalidation, so delaying the fetch does not
  // save a request, it only postpones the canonical snapshot and leaves the
  // route blank whenever the websocket snapshot is slower than the wait. That
  // is worst on the largest estates, where the initial websocket frame is
  // biggest and the blank screen hurts most, so those routes race the two
  // transports instead and paint from whichever arrives first.
  const initialHydrationFallbackDelayMs = revalidatesRestAfterWsInitialHydration
    ? 0
    : UNIFIED_RESOURCES_WS_INITIAL_HYDRATION_WAIT_MS;

  const runInitialHydrationFallback = () => {
    if (cacheEntry.hasSnapshot || wsStore.initialDataReceived()) {
      return;
    }
    void runRefetch({ source: 'initial' }).catch((err) => {
      logger.warn('[useUnifiedResources] Failed deferred initial refresh', err);
    });
  };

  const scheduleInitialHydrationFallback = () => {
    if (initialHydrationHandle !== undefined) {
      return;
    }
    if (initialHydrationFallbackDelayMs === 0) {
      runInitialHydrationFallback();
      return;
    }
    initialHydrationHandle = setTimeout(() => {
      initialHydrationHandle = undefined;
      runInitialHydrationFallback();
    }, initialHydrationFallbackDelayMs);
  };

  const scheduleRefetch = () => {
    clearRefreshTimeout();

    const elapsedSinceFetch = Date.now() - cacheEntry.lastFetchAt;
    const minIntervalDelay = Math.max(
      0,
      UNIFIED_RESOURCES_WS_MIN_REFETCH_INTERVAL_MS - elapsedSinceFetch,
    );
    const delay = Math.max(UNIFIED_RESOURCES_WS_DEBOUNCE_MS, minIntervalDelay);

    refreshHandle = setTimeout(() => {
      refreshHandle = undefined;
      void runRefetch({ source: 'ws' }).catch((err) => {
        logger.debug('[useUnifiedResources] WebSocket-triggered refetch failed', err);
      });
    }, delay);
  };

  const scheduleCanonicalRevalidation = () => {
    if (canonicalRevalidationHandle !== undefined) {
      return;
    }
    // Let websocket-first routes finish their initial summary/table mount
    // before asking REST to enrich the thinner realtime snapshot.
    canonicalRevalidationHandle = setTimeout(() => {
      canonicalRevalidationHandle = undefined;
      if (!enabled() || cacheEntry.canonicalAt > 0) {
        return;
      }
      revalidateCanonicalSnapshot();
    }, UNIFIED_RESOURCES_WS_CANONICAL_REVALIDATE_DELAY_MS);
  };

  // Forced because websocket ticks keep cachedAt fresh: an unforced refetch
  // would return the websocket-derived rows and never reach REST.
  const revalidateCanonicalSnapshot = () => {
    void runRefetch({ force: true, source: 'initial', background: true }).catch((err) => {
      logger.debug('[useUnifiedResources] Background canonical revalidation failed', err);
    });
  };

  // A `prefer-ws-then-rest` route can mount on a snapshot it did not fetch:
  // seeded from the all-resources cache, or painted from the websocket on an
  // earlier visit. Either reads as fresh while lacking the REST-only
  // enrichment, so the route revalidates now instead of trusting it.
  const needsCanonicalRevalidationOnMount = () =>
    revalidatesRestAfterWsInitialHydration &&
    cacheEntry.hasSnapshot &&
    !hasFreshCanonicalSnapshot(cacheEntry);

  createEffect(() => {
    orgScope();

    if (!enabled()) {
      scopeVersion += 1;
      inFlightRefetch = null;
      clearInitialHydrationTimeout();
      clearCanonicalRevalidationTimeout();
      clearRefreshTimeout();
      setLoading(false);
      return;
    }

    if (hasFreshUnifiedResourcesCache(cacheEntry)) {
      clearInitialHydrationTimeout();
      if (untrack(needsCanonicalRevalidationOnMount)) {
        revalidateCanonicalSnapshot();
      }
      return;
    }

    if (shouldPreferWsInitialHydration()) {
      if (!hasWsInitialHydrationSnapshot()) {
        scheduleInitialHydrationFallback();
      }
      return;
    }

    void runRefetch({ source: 'initial' }).catch((err) => {
      logger.warn('[useUnifiedResources] Failed background refresh for stale cache', err);
    });
  });

  createEffect(() => {
    const currentOrgScope = orgScope();
    if (!enabled() || !realtimeEnabled()) {
      return;
    }
    if (!supportsCanonicalWsHydration) {
      return;
    }
    if (!wsStore.connected() || !wsStore.initialDataReceived()) {
      return;
    }

    const lastUpdateToken = String(wsStore.state.lastUpdate ?? '');
    if (blockedWsHydrationToken !== null) {
      if (
        lastUpdateToken.length === 0 ||
        lastUpdateToken === '0' ||
        lastUpdateToken === blockedWsHydrationToken
      ) {
        return;
      }
      blockedWsHydrationToken = null;
    }

    const resourceChange = wsStore.resourceChange?.() ?? {
      version: Number(lastUpdateToken) || 0,
      changedIds: null,
      changedKeys: null,
    };
    // For normal page loads, keep the first paint on the canonical REST contract.
    // Only explicit websocket-first consumers are allowed to render directly
    // from the thinner realtime transport before a canonical snapshot exists.
    if (!cacheEntry.hasSnapshot && !prefersWsInitialHydration) {
      return;
    }
    const shouldRevalidateCanonicalSnapshot =
      revalidatesRestAfterWsInitialHydration && !cacheEntry.hasSnapshot;
    const allResourcesEntry = getUnifiedResourcesCacheEntry(
      buildScopedUnifiedResourcesCacheKey(ALL_RESOURCES_CACHE_KEY, currentOrgScope),
    );
    // An instance that mounts or resumes more than one tick behind the shared
    // cache used to fall off the incremental path onto a full-estate remerge —
    // the dominant Alerts-entry / tab re-entry cost in the 2026-08-25 profile.
    // Catch up through the store's bounded changed-id history instead; a
    // single-tick gap keeps using the latest delta without touching history.
    // The per-key change shapes ride along so metrics-only rows keep fast-path
    // eligibility across the whole catch-up window.
    type CatchUpMeta = {
      changedIds: ReadonlySet<string>;
      changedKeys: ResourceChangedKeys | null;
    };
    const resolveCatchUpMeta = (sinceVersion: number): CatchUpMeta | null => {
      if (sinceVersion <= 0 || resourceChange.version <= sinceVersion) return null;
      if (resourceChange.version === sinceVersion + 1) {
        return resourceChange.changedIds
          ? {
              changedIds: resourceChange.changedIds,
              changedKeys: resourceChange.changedKeys ?? null,
            }
          : null;
      }
      if (wsStore.changedResourceMetaSince) {
        return wsStore.changedResourceMetaSince(sinceVersion);
      }
      const catchUpIds = wsStore.changedResourceIdsSince?.(sinceVersion) ?? null;
      return catchUpIds ? { changedIds: catchUpIds, changedKeys: null } : null;
    };
    const allEntryCatchUp =
      allResourcesEntry.realtimeVersion > 0
        ? resolveCatchUpMeta(allResourcesEntry.realtimeVersion)
        : null;
    const canApplyIncrementally = allEntryCatchUp !== null;
    const realtimeSnapshotAlreadyApplied =
      resourceChange.version > 0 &&
      allResourcesEntry.hasSnapshot &&
      allResourcesEntry.realtimeVersion === resourceChange.version;
    // Deep-unwrapping the whole resource store on every websocket tick was the
    // single largest idle main-thread cost in the 2026-08-25 audit profile.
    // The delta merge only dereferences raw subtrees for entries it will clone
    // (changed ids and ids it has no cached merge output for), so unwrap
    // exactly those and skip the store read entirely when this realtime
    // version is already applied.
    // untrack keeps the per-item id/type reads from registering thousands of
    // fine-grained dependencies; the effect is driven by resourceChange().
    const readWsResources = (catchUp: CatchUpMeta | null): Resource[] =>
      untrack(() => {
        const stored = wsStore.state.resources;
        if (!Array.isArray(stored)) return [];
        // The cache rows built from this read must not share nested arrays or
        // objects with the store's rows: the store reconciles server changes
        // into its own rows in place, and a shared `sources` array would flip
        // on a cached row while that row keeps its REST-only enrichment.
        if (catchUp === null) return structuredClone(unwrap(stored)) as Resource[];
        const cachedById = new Map(
          allResourcesEntry.resources.map((resource) => [resource.id, resource] as const),
        );
        return stored.map((resource) => {
          if (!cachedById.has(resource.id)) {
            return unwrap(resource) as Resource;
          }
          if (!catchUp.changedIds.has(resource.id)) {
            return resource as Resource;
          }
          // Fast-path rows are read per patched key inside the merge (an O(1)
          // proxy escape per subtree); only full-merge rows need the deep
          // unwrap that structuredClone requires.
          return getFastResourceMergePatchKeys(
            catchUp.changedKeys ?? undefined,
            resource.id,
            cachedById.get(resource.id),
          )
            ? (resource as Resource)
            : (unwrap(resource) as Resource);
        });
      });
    const mergedWsResources = realtimeSnapshotAlreadyApplied
      ? allResourcesEntry.resources
      : canApplyIncrementally
        ? untrack(() =>
            mergeCanonicalResourceDeltaSnapshot(
              readWsResources(allEntryCatchUp),
              allResourcesEntry.resources,
              allEntryCatchUp!.changedIds,
              allEntryCatchUp!.changedKeys ?? undefined,
            ),
          )
        : untrack(() =>
            mergeCanonicalResourceSnapshot(readWsResources(null), allResourcesEntry.resources),
          );
    const projectedResources = filterCanonicalUnifiedResources(
      mergedWsResources,
      query,
      localFilter,
    );
    const projectedFacetResources = filterCanonicalUnifiedResources(
      mergedWsResources,
      query,
      localFilter
        ? {
            ...localFilter,
            types: new Set<ResourceType>(),
          }
        : null,
    );
    const projectedFacets =
      projectedFacetResources === null
        ? cacheEntry.facets
        : buildUnifiedResourceFacets(projectedFacetResources);
    // The all-resources route shares this cache object. Keep its prior state
    // before publishing the new generation so it can use the same bounded
    // changed-ID path as source-scoped projections.
    const previousProjectedResources = cacheEntry.resources;
    const previousCacheRealtimeVersion = cacheEntry.realtimeVersion;
    const previousCacheHasSnapshot = cacheEntry.hasSnapshot;
    const now = Date.now();
    clearInitialHydrationTimeout();
    setUnifiedResourcesCache(allResourcesEntry, mergedWsResources, now);
    allResourcesEntry.lastFetchAt = now;
    allResourcesEntry.realtimeVersion = resourceChange.version;

    if (projectedResources === null) {
      return;
    }

    // A route can finish its richer REST hydration before the first websocket
    // snapshot exists. Preserve that one-time enrichment, then promote it into
    // the shared canonical cache so later incremental updates and retained tabs
    // can project directly without repeating an estate-wide merge.
    const resolvedProjectedResources =
      cacheEntry !== allResourcesEntry && cacheEntry.hasSnapshot && cacheEntry.realtimeVersion === 0
        ? mergeCanonicalResourceSnapshot(projectedResources, cacheEntry.resources)
        : projectedResources;
    if (resolvedProjectedResources !== projectedResources) {
      const enrichedById = new Map(
        resolvedProjectedResources.map((resource) => [resource.id, resource] as const),
      );
      setUnifiedResourcesCache(
        allResourcesEntry,
        allResourcesEntry.resources.map((resource) => enrichedById.get(resource.id) ?? resource),
        now,
      );
      allResourcesEntry.lastFetchAt = now;
      allResourcesEntry.realtimeVersion = resourceChange.version;
    }

    const cacheEntryCatchUp =
      previousCacheHasSnapshot &&
      previousCacheRealtimeVersion > 0 &&
      resolvedProjectedResources === projectedResources
        ? resolveCatchUpMeta(previousCacheRealtimeVersion)
        : null;
    const canPatchProjectionIncrementally = cacheEntryCatchUp !== null;
    const incrementalPatchIndices = canPatchProjectionIncrementally
      ? resolveIncrementalResourcePatchIndices(
          resources as unknown as Resource[],
          resolvedProjectedResources,
          cacheEntryCatchUp!.changedIds,
        )
      : null;

    setUnifiedResourcesCache(
      cacheEntry,
      resolvedProjectedResources,
      now,
      cacheEntry.policyPosture,
      cacheEntry.aggregations,
      projectedFacets,
    );
    cacheEntry.lastFetchAt = now;
    cacheEntry.realtimeVersion = resourceChange.version;
    batch(() => {
      if (incrementalPatchIndices === null) {
        setResources(reconcile(resolvedProjectedResources, { key: 'id' }));
      } else {
        incrementalPatchIndices.forEach((index) => {
          const nextRow = resolvedProjectedResources[index];
          const previousRow = previousProjectedResources[index];
          const fastKeys =
            previousRow && previousRow.id === nextRow.id
              ? getFastResourceMergePatchKeys(
                  cacheEntryCatchUp!.changedKeys ?? undefined,
                  nextRow.id,
                  previousRow,
                )
              : null;
          if (fastKeys) {
            // A fast-path row differs from the store row only in its patched
            // subtrees; committing those per key avoids the full-row reconcile
            // whose unwrap pass deep-walks every field.
            for (const op of buildFastResourceStorePatchOps(nextRow, fastKeys)) {
              if (op.leaf !== undefined) {
                setResources(
                  index,
                  op.key as keyof Resource,
                  op.leaf as never,
                  (op.mode === 'reconcile' ? reconcile(op.value) : op.value) as never,
                );
              } else {
                setResources(
                  index,
                  op.key as keyof Resource,
                  (op.mode === 'reconcile' ? reconcile(op.value) : op.value) as never,
                );
              }
            }
          } else {
            setResources(index, reconcile(nextRow, { key: 'id' }));
          }
        });
      }
      setResourceSnapshotChange({
        version: ++resourceSnapshotVersion,
        changedIds: incrementalPatchIndices === null ? null : cacheEntryCatchUp!.changedIds,
      });
      setPolicyPosture(cacheEntry.policyPosture);
      setAggregations(cacheEntry.aggregations);
      setFacets(cacheEntry.facets);
      // Realtime rows are not a successful canonical read. Opt-in selection
      // consumers keep a failed read (including on reconnect/remount) until
      // REST succeeds; other consumers retain their existing websocket policy.
      if (!retainReadError) setError(undefined);
      setLoading(false);
    });

    if (shouldRevalidateCanonicalSnapshot) {
      scheduleCanonicalRevalidation();
    }
  });

  createEffect(() => {
    orgScope();

    if (!enabled() || !realtimeEnabled()) {
      wsInitialized = false;
      lastWsUpdateToken = '';
      clearInitialHydrationTimeout();
      clearCanonicalRevalidationTimeout();
      clearRefreshTimeout();
      return;
    }

    if (!wsStore.connected() || !wsStore.initialDataReceived()) {
      wsInitialized = false;
      lastWsUpdateToken = '';
      return;
    }

    const lastUpdateToken = String(wsStore.state.lastUpdate ?? '');

    if (!wsInitialized) {
      wsInitialized = true;
      lastWsUpdateToken = lastUpdateToken;
      if (!supportsCanonicalWsHydration) {
        scheduleRefetch();
      }
      return;
    }

    if (lastUpdateToken === lastWsUpdateToken) {
      return;
    }

    lastWsUpdateToken = lastUpdateToken;
    if (!supportsCanonicalWsHydration) {
      scheduleRefetch();
    }
  });

  const unsubscribeOrgSwitch = eventBus.on('org_switched', (nextOrgID?: string) => {
    const nextOrgScope = normalizeOrgScope(nextOrgID);
    if (nextOrgScope === orgScope()) {
      return;
    }

    scopeVersion += 1;
    const nextCacheEntry = seedUnifiedResourcesCacheFromAllResources(
      getUnifiedResourcesCacheEntry(buildScopedUnifiedResourcesCacheKey(cacheKey, nextOrgScope)),
      cacheKey,
      query,
      nextOrgScope,
    );
    blockedWsHydrationToken = supportsCanonicalWsHydration
      ? String(wsStore.state.lastUpdate ?? '')
      : null;
    cacheEntry = nextCacheEntry;
    inFlightRefetch = null;
    wsInitialized = false;
    lastWsUpdateToken = '';
    clearInitialHydrationTimeout();
    clearCanonicalRevalidationTimeout();
    setOrgScope(nextOrgScope);

    const scopedResources = cacheEntry.resources;
    const scopedPolicyPosture = cacheEntry.policyPosture;
    const scopedAggregations = cacheEntry.aggregations;
    const scopedFacets = cacheEntry.facets;
    batch(() => {
      setError(retainReadError ? nextCacheEntry.readError : undefined);
      setResources(reconcile(scopedResources, { key: 'id' }));
      setResourceSnapshotChange({ version: ++resourceSnapshotVersion, changedIds: null });
      setPolicyPosture(scopedPolicyPosture);
      setAggregations(scopedAggregations);
      setFacets(scopedFacets);
      setLoading(enabled() && !cacheEntry.hasSnapshot);
    });

    if (enabled() && !hasFreshUnifiedResourcesCache(cacheEntry)) {
      if (shouldPreferWsInitialHydration()) {
        scheduleInitialHydrationFallback();
      } else {
        void runRefetch({ force: true, source: 'initial' }).catch(() => undefined);
      }
    } else if (enabled() && needsCanonicalRevalidationOnMount()) {
      revalidateCanonicalSnapshot();
    }
  });

  const handleResourceMetadataChanged = () => {
    if (!enabled()) {
      return;
    }
    const activeRequest = inFlightRefetch;
    void (async () => {
      // A save can land while the initial snapshot is still settling. Wait for
      // that request before forcing the post-save snapshot so the metadata
      // event cannot be swallowed by normal in-flight request coalescing.
      if (activeRequest) {
        await activeRequest.catch(() => undefined);
      }
      if (!enabled()) {
        return;
      }
      await runRefetch({ force: true, source: 'manual', background: true });
    })().catch((err) => {
      logger.debug('[useUnifiedResources] Metadata-triggered refetch failed', err);
    });
  };
  window.addEventListener(RESOURCE_METADATA_CHANGED_EVENT, handleResourceMetadataChanged);

  onCleanup(() => {
    unsubscribeOrgSwitch();
    window.removeEventListener(RESOURCE_METADATA_CHANGED_EVENT, handleResourceMetadataChanged);
    clearInitialHydrationTimeout();
    clearCanonicalRevalidationTimeout();
    clearRefreshTimeout();
  });

  return {
    resources: () => resources,
    resourceSnapshotChange,
    policyPosture,
    aggregations,
    facets,
    refetch,
    mutate,
    loading,
    error,
  };
}

export function useStorageRecoveryResources() {
  return useUnifiedResources({
    query: STORAGE_RECOVERY_UNIFIED_RESOURCES_QUERY,
    cacheKey: 'storage-recovery',
  });
}

export default useUnifiedResources;