@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.js CHANGED
@@ -14671,6 +14671,7 @@ function pruneAgentConversations(cache) {
14671
14671
  }
14672
14672
 
14673
14673
  // src/services/host-pressure/hostPressure.ts
14674
+ import { execFile as execFile4 } from "child_process";
14674
14675
  import { cpus, freemem, loadavg, totalmem } from "os";
14675
14676
  import { monitorEventLoopDelay } from "perf_hooks";
14676
14677
  var HOST_PRESSURE_SAMPLE_MS = 5e3;
@@ -14705,6 +14706,59 @@ var HOST_PRESSURE_BARS = {
14705
14706
  };
14706
14707
  var REASON_ORDER = ["memory", "event_loop", "load", "agents"];
14707
14708
  var RANK = { ok: 0, elevated: 1, critical: 2 };
14709
+ function parseMacMemoryPressureFreeRatio(output) {
14710
+ const match = output.match(/System-wide memory free percentage:\s*(\d+(?:\.\d+)?)%/);
14711
+ if (!match) return void 0;
14712
+ const percentage = Number(match[1]);
14713
+ return percentage <= 100 ? percentage / 100 : void 0;
14714
+ }
14715
+ function runMacMemoryPressure() {
14716
+ return new Promise((resolve2, reject) => {
14717
+ execFile4(
14718
+ "/usr/bin/memory_pressure",
14719
+ ["-Q"],
14720
+ { encoding: "utf8", timeout: 1e3 },
14721
+ (error, stdout) => {
14722
+ if (error) {
14723
+ reject(error);
14724
+ return;
14725
+ }
14726
+ resolve2(stdout);
14727
+ }
14728
+ );
14729
+ });
14730
+ }
14731
+ async function readMacMemoryPressureFreeRatio(run2 = runMacMemoryPressure) {
14732
+ try {
14733
+ return parseMacMemoryPressureFreeRatio(await run2());
14734
+ } catch {
14735
+ return void 0;
14736
+ }
14737
+ }
14738
+ var MacMemoryPressureProbe = class {
14739
+ constructor(read) {
14740
+ this.read = read;
14741
+ }
14742
+ read;
14743
+ current;
14744
+ pending = false;
14745
+ refresh() {
14746
+ if (this.pending) return;
14747
+ this.pending = true;
14748
+ void this.read().then((ratio) => {
14749
+ this.current = ratio;
14750
+ }).finally(() => {
14751
+ this.pending = false;
14752
+ });
14753
+ }
14754
+ value() {
14755
+ return this.current;
14756
+ }
14757
+ };
14758
+ function hostMemoryFreeRatio(platform3, totalBytes, freeBytes, macPressureFreeRatio) {
14759
+ if (platform3 === "darwin") return macPressureFreeRatio;
14760
+ return totalBytes > 0 ? freeBytes / totalBytes : 1;
14761
+ }
14708
14762
  function worst(levels) {
14709
14763
  return levels.reduce(
14710
14764
  (acc, level) => RANK[level] > RANK[acc] ? level : acc,
@@ -14764,7 +14818,7 @@ function cpuBusyRatio(previous, next) {
14764
14818
  return 1 - idle / total;
14765
14819
  }
14766
14820
  function classifyHostPressure(sample, previous, platform3) {
14767
- const memRaw = schmittLowIsWorse(
14821
+ const mem = sample.memFreeRatio === void 0 ? "ok" : schmittLowIsWorse(
14768
14822
  sample.memFreeRatio,
14769
14823
  previous,
14770
14824
  HOST_PRESSURE_BARS.memFreeRatio.enterElevated,
@@ -14772,7 +14826,6 @@ function classifyHostPressure(sample, previous, platform3) {
14772
14826
  HOST_PRESSURE_BARS.memFreeRatio.enterCritical,
14773
14827
  HOST_PRESSURE_BARS.memFreeRatio.leaveCritical
14774
14828
  );
14775
- const mem = platform3 === "darwin" && memRaw === "critical" ? "elevated" : memRaw;
14776
14829
  const eventLoop = schmittHighIsWorse(
14777
14830
  sample.eventLoopP99Ms,
14778
14831
  previous,
@@ -14856,23 +14909,28 @@ var HostPressureMonitor = class {
14856
14909
  };
14857
14910
  function createHostPressureMonitor(wsHub, liveAgents) {
14858
14911
  const histogram = monitorEventLoopDelay({ resolution: 20 });
14859
- const windows = process.platform === "win32";
14912
+ const platform3 = process.platform;
14913
+ const windows = platform3 === "win32";
14914
+ const macMemoryPressure = platform3 === "darwin" ? new MacMemoryPressureProbe(readMacMemoryPressureFreeRatio) : null;
14860
14915
  let prevCpuTimes = null;
14916
+ macMemoryPressure?.refresh();
14861
14917
  const monitor = new HostPressureMonitor({
14862
14918
  wsHub,
14863
14919
  histogram,
14864
14920
  readSample: () => {
14865
14921
  const eventLoopP99Ms = histogram.percentile(99) / 1e6;
14866
14922
  histogram.reset();
14867
- const total = totalmem();
14923
+ const total = platform3 === "darwin" ? 0 : totalmem();
14924
+ const free = platform3 === "darwin" ? 0 : freemem();
14868
14925
  const cpuList = cpus();
14869
14926
  const sample = {
14870
14927
  liveAgents: liveAgents(),
14871
- memFreeRatio: total > 0 ? freemem() / total : 1,
14928
+ memFreeRatio: hostMemoryFreeRatio(platform3, total, free, macMemoryPressure?.value()),
14872
14929
  eventLoopP99Ms,
14873
14930
  load1: loadavg()[0],
14874
14931
  ncpu: cpuList.length
14875
14932
  };
14933
+ macMemoryPressure?.refresh();
14876
14934
  if (windows) {
14877
14935
  const cpuTimes = cpuList.map((cpu) => cpu.times);
14878
14936
  sample.cpuBusyRatio = cpuBusyRatio(prevCpuTimes, cpuTimes);