"use strict";
/**
 * Lightweight resource stats for the chat-gate admission check.
 *
 * Pure-ish: reads `/proc/meminfo` + `os.loadavg()` synchronously. No
 * subprocess, no network. Both calls are sub-millisecond on Linux.
 * macOS has no /proc/meminfo — swap is reported as 0 free there, which
 * effectively disables the swap-check on macOS (the gate's default
 * `swapMinFreeMb=1024` would refuse every chat). Users on macOS should
 * lower swapMinFreeMb to 0 if they want the gate active.
 *
 * Output shape kept narrow so the admit-decision function (in chat-gate)
 * stays a pure function of (state, config) — no side effects, easy to
 * unit-test.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.readResourceStats = readResourceStats;
const fs = __importStar(require("node:fs"));
const node_os_1 = require("node:os");
/**
 * Read free swap from /proc/meminfo. Returns -1 (not 0) when
 * unavailable so callers can distinguish "platform reports nothing"
 * from "SwapFree is genuinely 0 — host is OOM-imminent".
 */
function readSwapFreeMb() {
    if ((0, node_os_1.platform)() !== 'linux')
        return -1;
    try {
        const meminfo = fs.readFileSync('/proc/meminfo', 'utf-8');
        const match = meminfo.match(/^SwapFree:\s+(\d+)\s+kB/m);
        if (!match)
            return -1;
        return Math.floor(parseInt(match[1], 10) / 1024);
    }
    catch {
        return -1;
    }
}
/** Snapshot of current resource pressure. Called per admission attempt. */
function readResourceStats() {
    return {
        swapFreeMb: readSwapFreeMb(),
        loadAvg1: (0, node_os_1.loadavg)()[0] ?? 0,
        cpuCount: (0, node_os_1.cpus)().length,
    };
}
//# sourceMappingURL=resource-stats.js.map