/** * Swarm session lifecycle labels for operators (non-authoritative; driven by env + peer count). * @param {Record | null | undefined} env * @param {number} peerCount */ export function bareOsSwarmLifecycleSnapshot(env, peerCount) { const peers = Math.max(0, Math.floor(peerCount)) const syncing = env && (env.BARE_OS_SWARM_SYNCING === '1' || env.BARE_OS_SWARM_SYNCING === 'true') const degraded = env && (env.BARE_OS_SWARM_DEGRADED === '1' || env.BARE_OS_SWARM_DEGRADED === 'true') /** @type {'discovering' | 'syncing' | 'steady' | 'degraded'} */ let state = 'steady' if (degraded) state = 'degraded' else if (syncing) state = 'syncing' else if (peers === 0) state = 'discovering' /** @type {number | undefined} */ let dhtActiveQueries /** @type {number | undefined} */ let udxEphemeralPort const raw = env && env.BARE_OS_HYPERDHT_STATS_JSON if (raw != null && String(raw).trim()) { try { const j = JSON.parse(String(raw)) if (typeof j.activeQueries === 'number') dhtActiveQueries = j.activeQueries if (typeof j.udpPort === 'number') udxEphemeralPort = j.udpPort } catch { /* ignore invalid operator JSON */ } } return { schema: 1, state, peerCount: peers, note: 'Lifecycle is derived from BARE_OS_SWARM_* env hints and live peer count; host may override via env.', hyperdhtSketch: { schema: 1, note: 'Capped non-secret hints from BARE_OS_HYPERDHT_STATS_JSON when the host sets it.', dhtActiveQueries, udxEphemeralPort }, atMs: Date.now() } }