@threadbase-sh/streamer 1.70.5 → 1.70.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -14706,6 +14706,7 @@ function pruneAgentConversations(cache) {
14706
14706
  }
14707
14707
 
14708
14708
  // src/services/host-pressure/hostPressure.ts
14709
+ var import_node_child_process3 = require("child_process");
14709
14710
  var import_os13 = require("os");
14710
14711
  var import_perf_hooks = require("perf_hooks");
14711
14712
  var HOST_PRESSURE_SAMPLE_MS = 5e3;
@@ -14740,6 +14741,59 @@ var HOST_PRESSURE_BARS = {
14740
14741
  };
14741
14742
  var REASON_ORDER = ["memory", "event_loop", "load", "agents"];
14742
14743
  var RANK = { ok: 0, elevated: 1, critical: 2 };
14744
+ function parseMacMemoryPressureFreeRatio(output) {
14745
+ const match = output.match(/System-wide memory free percentage:\s*(\d+(?:\.\d+)?)%/);
14746
+ if (!match) return void 0;
14747
+ const percentage = Number(match[1]);
14748
+ return percentage <= 100 ? percentage / 100 : void 0;
14749
+ }
14750
+ function runMacMemoryPressure() {
14751
+ return new Promise((resolve2, reject) => {
14752
+ (0, import_node_child_process3.execFile)(
14753
+ "/usr/bin/memory_pressure",
14754
+ ["-Q"],
14755
+ { encoding: "utf8", timeout: 1e3 },
14756
+ (error, stdout) => {
14757
+ if (error) {
14758
+ reject(error);
14759
+ return;
14760
+ }
14761
+ resolve2(stdout);
14762
+ }
14763
+ );
14764
+ });
14765
+ }
14766
+ async function readMacMemoryPressureFreeRatio(run2 = runMacMemoryPressure) {
14767
+ try {
14768
+ return parseMacMemoryPressureFreeRatio(await run2());
14769
+ } catch {
14770
+ return void 0;
14771
+ }
14772
+ }
14773
+ var MacMemoryPressureProbe = class {
14774
+ constructor(read) {
14775
+ this.read = read;
14776
+ }
14777
+ read;
14778
+ current;
14779
+ pending = false;
14780
+ refresh() {
14781
+ if (this.pending) return;
14782
+ this.pending = true;
14783
+ void this.read().then((ratio) => {
14784
+ this.current = ratio;
14785
+ }).finally(() => {
14786
+ this.pending = false;
14787
+ });
14788
+ }
14789
+ value() {
14790
+ return this.current;
14791
+ }
14792
+ };
14793
+ function hostMemoryFreeRatio(platform3, totalBytes, freeBytes, macPressureFreeRatio) {
14794
+ if (platform3 === "darwin") return macPressureFreeRatio;
14795
+ return totalBytes > 0 ? freeBytes / totalBytes : 1;
14796
+ }
14743
14797
  function worst(levels) {
14744
14798
  return levels.reduce(
14745
14799
  (acc, level) => RANK[level] > RANK[acc] ? level : acc,
@@ -14799,7 +14853,7 @@ function cpuBusyRatio(previous, next) {
14799
14853
  return 1 - idle / total;
14800
14854
  }
14801
14855
  function classifyHostPressure(sample, previous, platform3) {
14802
- const memRaw = schmittLowIsWorse(
14856
+ const mem = sample.memFreeRatio === void 0 ? "ok" : schmittLowIsWorse(
14803
14857
  sample.memFreeRatio,
14804
14858
  previous,
14805
14859
  HOST_PRESSURE_BARS.memFreeRatio.enterElevated,
@@ -14807,7 +14861,6 @@ function classifyHostPressure(sample, previous, platform3) {
14807
14861
  HOST_PRESSURE_BARS.memFreeRatio.enterCritical,
14808
14862
  HOST_PRESSURE_BARS.memFreeRatio.leaveCritical
14809
14863
  );
14810
- const mem = platform3 === "darwin" && memRaw === "critical" ? "elevated" : memRaw;
14811
14864
  const eventLoop = schmittHighIsWorse(
14812
14865
  sample.eventLoopP99Ms,
14813
14866
  previous,
@@ -14891,23 +14944,28 @@ var HostPressureMonitor = class {
14891
14944
  };
14892
14945
  function createHostPressureMonitor(wsHub, liveAgents) {
14893
14946
  const histogram = (0, import_perf_hooks.monitorEventLoopDelay)({ resolution: 20 });
14894
- const windows = process.platform === "win32";
14947
+ const platform3 = process.platform;
14948
+ const windows = platform3 === "win32";
14949
+ const macMemoryPressure = platform3 === "darwin" ? new MacMemoryPressureProbe(readMacMemoryPressureFreeRatio) : null;
14895
14950
  let prevCpuTimes = null;
14951
+ macMemoryPressure?.refresh();
14896
14952
  const monitor = new HostPressureMonitor({
14897
14953
  wsHub,
14898
14954
  histogram,
14899
14955
  readSample: () => {
14900
14956
  const eventLoopP99Ms = histogram.percentile(99) / 1e6;
14901
14957
  histogram.reset();
14902
- const total = (0, import_os13.totalmem)();
14958
+ const total = platform3 === "darwin" ? 0 : (0, import_os13.totalmem)();
14959
+ const free = platform3 === "darwin" ? 0 : (0, import_os13.freemem)();
14903
14960
  const cpuList = (0, import_os13.cpus)();
14904
14961
  const sample = {
14905
14962
  liveAgents: liveAgents(),
14906
- memFreeRatio: total > 0 ? (0, import_os13.freemem)() / total : 1,
14963
+ memFreeRatio: hostMemoryFreeRatio(platform3, total, free, macMemoryPressure?.value()),
14907
14964
  eventLoopP99Ms,
14908
14965
  load1: (0, import_os13.loadavg)()[0],
14909
14966
  ncpu: cpuList.length
14910
14967
  };
14968
+ macMemoryPressure?.refresh();
14911
14969
  if (windows) {
14912
14970
  const cpuTimes = cpuList.map((cpu) => cpu.times);
14913
14971
  sample.cpuBusyRatio = cpuBusyRatio(prevCpuTimes, cpuTimes);