@madarco/agentbox 0.11.0 → 0.11.2

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.
@@ -1870,42 +1870,71 @@ async function loadQueue() {
1870
1870
  out.sort((a, b) => a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0);
1871
1871
  return out;
1872
1872
  }
1873
+ function processAlive(pid) {
1874
+ try {
1875
+ process.kill(pid, 0);
1876
+ return true;
1877
+ } catch {
1878
+ return false;
1879
+ }
1880
+ }
1881
+ function countInFlightCreateJobs(jobs, accountedBoxIds) {
1882
+ let n = 0;
1883
+ for (const j of jobs) {
1884
+ if (j.status !== "running") continue;
1885
+ if (j.boxId && accountedBoxIds.has(j.boxId)) continue;
1886
+ if (typeof j.pid === "number" && !processAlive(j.pid)) continue;
1887
+ n += 1;
1888
+ }
1889
+ return n;
1890
+ }
1873
1891
  var RUNNING_COUNT_CACHE_MS = 3e3;
1874
- var runningCountCache = null;
1892
+ var boxStateCache = null;
1875
1893
  async function defaultCountRunningBoxes() {
1894
+ const { boxCount, stateIds } = await cachedBoxState();
1895
+ let jobs;
1896
+ try {
1897
+ jobs = await loadQueue();
1898
+ } catch {
1899
+ return boxCount;
1900
+ }
1901
+ return boxCount + countInFlightCreateJobs(jobs, stateIds);
1902
+ }
1903
+ async function cachedBoxState() {
1876
1904
  const now = Date.now();
1877
- if (runningCountCache && runningCountCache.expiresAt > now) {
1878
- return runningCountCache.value;
1905
+ if (boxStateCache && boxStateCache.expiresAt > now) {
1906
+ return { boxCount: boxStateCache.boxCount, stateIds: boxStateCache.stateIds };
1879
1907
  }
1880
- const value = await uncachedCountRunningBoxes();
1881
- runningCountCache = { value, expiresAt: now + RUNNING_COUNT_CACHE_MS };
1882
- return value;
1908
+ const fresh = await uncachedBoxStateCount();
1909
+ boxStateCache = { ...fresh, expiresAt: now + RUNNING_COUNT_CACHE_MS };
1910
+ return fresh;
1883
1911
  }
1884
- async function uncachedCountRunningBoxes() {
1912
+ async function uncachedBoxStateCount() {
1885
1913
  let boxes;
1886
1914
  try {
1887
1915
  boxes = (await readState(STATE_FILE)).boxes;
1888
1916
  } catch {
1889
- return 0;
1917
+ return { boxCount: 0, stateIds: /* @__PURE__ */ new Set() };
1890
1918
  }
1891
- if (boxes.length === 0) return 0;
1892
- let count = 0;
1919
+ const stateIds = new Set(boxes.map((b) => b.id));
1920
+ if (boxes.length === 0) return { boxCount: 0, stateIds };
1921
+ let boxCount = 0;
1893
1922
  const dockerBoxes = [];
1894
1923
  for (const b of boxes) {
1895
1924
  const provider = b.provider ?? "docker";
1896
1925
  if (provider === "docker") {
1897
1926
  dockerBoxes.push(b);
1898
1927
  } else {
1899
- count += 1;
1928
+ boxCount += 1;
1900
1929
  }
1901
1930
  }
1902
1931
  if (dockerBoxes.length > 0) {
1903
1932
  const states = await Promise.all(dockerBoxes.map((b) => inspectDockerState(b.container)));
1904
1933
  for (const s of states) {
1905
- if (s === "running") count += 1;
1934
+ if (s === "running") boxCount += 1;
1906
1935
  }
1907
1936
  }
1908
- return count;
1937
+ return { boxCount, stateIds };
1909
1938
  }
1910
1939
  function inspectDockerState(containerName) {
1911
1940
  return new Promise((resolveP) => {
@@ -5464,7 +5493,7 @@ async function ensureRelay(opts = {}) {
5464
5493
  await reclaimRelay(health.pid, log);
5465
5494
  } else {
5466
5495
  const existingPid = await readPidFile();
5467
- if (existingPid !== null && await processAlive(existingPid)) {
5496
+ if (existingPid !== null && await processAlive2(existingPid)) {
5468
5497
  for (let i = 0; i < 10; i++) {
5469
5498
  if (await pingHealthz(300)) return ENDPOINT;
5470
5499
  await delay2(200);
@@ -5493,7 +5522,7 @@ async function reclaimRelay(reportedPid, log) {
5493
5522
  for (const pid of [reportedPid, pidFromFile]) {
5494
5523
  if (typeof pid !== "number" || pid <= 0 || seen.has(pid)) continue;
5495
5524
  seen.add(pid);
5496
- if (!await processAlive(pid)) continue;
5525
+ if (!await processAlive2(pid)) continue;
5497
5526
  log(`stopping crippled relay pid ${String(pid)}`);
5498
5527
  await killPid(pid);
5499
5528
  }
@@ -5512,7 +5541,7 @@ async function killPid(pid) {
5512
5541
  return;
5513
5542
  }
5514
5543
  for (let i = 0; i < 20; i++) {
5515
- if (!await processAlive(pid)) return;
5544
+ if (!await processAlive2(pid)) return;
5516
5545
  await delay2(100);
5517
5546
  }
5518
5547
  try {
@@ -5663,7 +5692,7 @@ async function stopRelay() {
5663
5692
  if (pid === null) {
5664
5693
  return { stopped: false, pid: null };
5665
5694
  }
5666
- if (!await processAlive(pid)) {
5695
+ if (!await processAlive2(pid)) {
5667
5696
  await unlink2(PID_FILE).catch(() => {
5668
5697
  });
5669
5698
  return { stopped: false, pid };
@@ -5673,10 +5702,10 @@ async function stopRelay() {
5673
5702
  } catch {
5674
5703
  }
5675
5704
  for (let i = 0; i < 20; i++) {
5676
- if (!await processAlive(pid)) break;
5705
+ if (!await processAlive2(pid)) break;
5677
5706
  await delay2(100);
5678
5707
  }
5679
- if (await processAlive(pid)) {
5708
+ if (await processAlive2(pid)) {
5680
5709
  try {
5681
5710
  process.kill(pid, "SIGKILL");
5682
5711
  } catch {
@@ -5688,7 +5717,7 @@ async function stopRelay() {
5688
5717
  }
5689
5718
  async function getRelayStatus() {
5690
5719
  const pid = await readPidFile();
5691
- const pidAlive2 = pid !== null && await processAlive(pid);
5720
+ const pidAlive2 = pid !== null && await processAlive2(pid);
5692
5721
  const health = await fetchHealthz(300);
5693
5722
  return {
5694
5723
  running: health !== null,
@@ -5772,7 +5801,7 @@ async function readPidFile() {
5772
5801
  return null;
5773
5802
  }
5774
5803
  }
5775
- async function processAlive(pid) {
5804
+ async function processAlive2(pid) {
5776
5805
  try {
5777
5806
  process.kill(pid, 0);
5778
5807
  return true;
@@ -8449,4 +8478,4 @@ export {
8449
8478
  browserSessionActive,
8450
8479
  ensureBoxBrowser
8451
8480
  };
8452
- //# sourceMappingURL=chunk-GUNUBIRB.js.map
8481
+ //# sourceMappingURL=chunk-ZJXTIH6C.js.map