replicas-engine 0.1.1372 → 0.1.1373

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.
@@ -6,7 +6,7 @@ import {
6
6
  getOriginDefaultBranch,
7
7
  monolithRequest,
8
8
  runGitCommand
9
- } from "./chunk-ATL32BCH.js";
9
+ } from "./chunk-Z2KNJBXZ.js";
10
10
  import {
11
11
  extractCommandProtectionCommandText,
12
12
  findCommandProtectionGitLookups,
@@ -14,7 +14,7 @@ import {
14
14
  findPrMergeSignals,
15
15
  findScoutRepoWriteSignals,
16
16
  parseCommandProtectionRequest
17
- } from "./chunk-TQKWK7JJ.js";
17
+ } from "./chunk-NOHCO22T.js";
18
18
 
19
19
  // src/services/command-protection-service.ts
20
20
  import { readFile, stat } from "fs/promises";
@@ -4,7 +4,7 @@ const require = __createRequire(import.meta.url);
4
4
  import {
5
5
  SUBPROCESS_MAX_BUFFER,
6
6
  execFileAsync
7
- } from "./chunk-ZDD76UIZ.js";
7
+ } from "./chunk-5ZFK4NQS.js";
8
8
  import {
9
9
  isRecord as isRecord2
10
10
  } from "./chunk-UZSNFLDQ.js";
@@ -21,7 +21,7 @@ import {
21
21
  provisionedDeviceSchema,
22
22
  readReplicasRuntimeEnv,
23
23
  serializePosixEnvFile
24
- } from "./chunk-TQKWK7JJ.js";
24
+ } from "./chunk-NOHCO22T.js";
25
25
 
26
26
  // src/runtime-env-loader.ts
27
27
  import { readFileSync } from "fs";
@@ -3,13 +3,13 @@ import { createRequire as __createRequire } from 'node:module';
3
3
  const require = __createRequire(import.meta.url);
4
4
  import {
5
5
  evaluateCommandProtection
6
- } from "./chunk-AQ6GLMWE.js";
7
- import "./chunk-ATL32BCH.js";
8
- import "./chunk-ZDD76UIZ.js";
6
+ } from "./chunk-O3PYQIRJ.js";
7
+ import "./chunk-Z2KNJBXZ.js";
8
+ import "./chunk-5ZFK4NQS.js";
9
9
  import {
10
10
  isRecord
11
11
  } from "./chunk-UZSNFLDQ.js";
12
- import "./chunk-TQKWK7JJ.js";
12
+ import "./chunk-NOHCO22T.js";
13
13
  import "./chunk-VEQXQN22.js";
14
14
 
15
15
  // src/command-protection-hook.ts
@@ -14,12 +14,12 @@ import {
14
14
  recoverCompletedTurn,
15
15
  spawnMuseMsp,
16
16
  startMuseSession
17
- } from "./chunk-K7OYSY2J.js";
17
+ } from "./chunk-2L4KW6VE.js";
18
18
  import {
19
19
  AppServerProcess,
20
20
  buildCodexAgentEnv
21
- } from "./chunk-5IYWSQXM.js";
22
- import "./chunk-ZDD76UIZ.js";
21
+ } from "./chunk-4NRKAJKV.js";
22
+ import "./chunk-5ZFK4NQS.js";
23
23
  import {
24
24
  AGENT,
25
25
  OPENCODE_GO_PROVIDER,
@@ -29,7 +29,7 @@ import {
29
29
  headlessAgentUsagePath,
30
30
  isRecord,
31
31
  withTimeout
32
- } from "./chunk-TQKWK7JJ.js";
32
+ } from "./chunk-NOHCO22T.js";
33
33
  import "./chunk-VEQXQN22.js";
34
34
 
35
35
  // src/headless-agent.ts
package/dist/src/index.js CHANGED
@@ -27,7 +27,7 @@ import {
27
27
  evaluateCommandProtection,
28
28
  extractToolCommand,
29
29
  reportCommandProtectionBlock
30
- } from "./chunk-AQ6GLMWE.js";
30
+ } from "./chunk-O3PYQIRJ.js";
31
31
  import {
32
32
  ACCOUNT_RATE_LIMITS_UPDATED_METHOD,
33
33
  AGENT_MESSAGE_DELTA_METHOD,
@@ -65,10 +65,10 @@ import {
65
65
  spawnMuseMsp,
66
66
  startMuseSession,
67
67
  uuidV7
68
- } from "./chunk-K7OYSY2J.js";
68
+ } from "./chunk-2L4KW6VE.js";
69
69
  import {
70
70
  publishedPullRequestUrls
71
- } from "./chunk-OBKIRWWN.js";
71
+ } from "./chunk-AVI4QYDL.js";
72
72
  import {
73
73
  ACCOUNT_RATE_LIMITS_READ_METHOD,
74
74
  AsyncLock,
@@ -143,7 +143,7 @@ import {
143
143
  timestampFromSeconds,
144
144
  upsertCredentialFileLines,
145
145
  writeSecureCredentialFile
146
- } from "./chunk-7KM3ZXVX.js";
146
+ } from "./chunk-K4BSSV5L.js";
147
147
  import {
148
148
  ENGINE_ENV,
149
149
  monolithRequest,
@@ -153,15 +153,15 @@ import {
153
153
  resolveDiffBase,
154
154
  setAgentCredentialSnapshot,
155
155
  setLinearSessionId
156
- } from "./chunk-ATL32BCH.js";
156
+ } from "./chunk-Z2KNJBXZ.js";
157
157
  import {
158
158
  getCodexModelProvider
159
- } from "./chunk-5IYWSQXM.js";
159
+ } from "./chunk-4NRKAJKV.js";
160
160
  import {
161
161
  SUBPROCESS_MAX_BUFFER,
162
162
  execAsync,
163
163
  execFileAsync
164
- } from "./chunk-ZDD76UIZ.js";
164
+ } from "./chunk-5ZFK4NQS.js";
165
165
  import {
166
166
  isRecord as isRecord2
167
167
  } from "./chunk-UZSNFLDQ.js";
@@ -233,6 +233,7 @@ import {
233
233
  MAX_CANVAS_FILE_BYTES,
234
234
  MAX_CANVAS_PREVIEW_BYTES,
235
235
  MAX_CODEX_GOAL_OBJECTIVE_CHARS,
236
+ MAX_PENDING_RESOURCE_MINUTES,
236
237
  MAX_REPO_FILE_CONTENT_BYTES,
237
238
  MERGED_MESSAGE_SEPARATOR,
238
239
  MOTHERSHIP_EVENT_MESSAGE_TYPE,
@@ -422,7 +423,7 @@ import {
422
423
  workspaceTokenRequestSchema,
423
424
  workspaceTokenResponseSchema,
424
425
  wrapInTag
425
- } from "./chunk-TQKWK7JJ.js";
426
+ } from "./chunk-NOHCO22T.js";
426
427
  import {
427
428
  cloneMcpConfig,
428
429
  ensureCompatibilityImports,
@@ -27031,10 +27032,10 @@ async function runInputRequiredDriver(args) {
27031
27032
  const { config, method, originalParams, requestOptions, hooks, signal } = args;
27032
27033
  const startedAt = args.flowStartedAt ?? Date.now();
27033
27034
  let payload = args.firstPayload;
27034
- let round = 0;
27035
+ let round2 = 0;
27035
27036
  while (true) {
27036
- round += 1;
27037
- if (round > config.maxRounds) throw new SdkError(SdkErrorCode.InputRequiredRoundsExceeded, inputRequiredRoundsExceededMessage(method, config.maxRounds), {
27037
+ round2 += 1;
27038
+ if (round2 > config.maxRounds) throw new SdkError(SdkErrorCode.InputRequiredRoundsExceeded, inputRequiredRoundsExceededMessage(method, config.maxRounds), {
27038
27039
  rounds: config.maxRounds,
27039
27040
  lastResult: {
27040
27041
  inputRequests: payload.inputRequests,
@@ -27042,8 +27043,8 @@ async function runInputRequiredDriver(args) {
27042
27043
  }
27043
27044
  });
27044
27045
  requestOptions.onprogress?.({
27045
- progress: round,
27046
- message: `Fulfilling input required by '${method}' (round ${round})`
27046
+ progress: round2,
27047
+ message: `Fulfilling input required by '${method}' (round ${round2})`
27047
27048
  });
27048
27049
  const entries = Object.entries(payload.inputRequests ?? {});
27049
27050
  let responses;
@@ -64310,6 +64311,144 @@ function attachTerminalSocket(id, socket) {
64310
64311
  });
64311
64312
  }
64312
64313
 
64314
+ // src/services/resource-monitor.ts
64315
+ import { readFile as readFile22 } from "fs/promises";
64316
+ var SAMPLE_INTERVAL_MS = 5e3;
64317
+ var readOptional = (path7) => readFile22(path7, "utf8").catch(() => null);
64318
+ function parseCounters(text) {
64319
+ const counters = {};
64320
+ for (const line of text?.split("\n") ?? []) {
64321
+ const [key, value] = line.split(/:?\s+/);
64322
+ if (key && value) counters[key] = Number(value);
64323
+ }
64324
+ return counters;
64325
+ }
64326
+ function parsePressure(text, kind) {
64327
+ const match = text?.match(new RegExp(`^${kind} avg10=([\\d.]+)`, "m"));
64328
+ return match ? Number(match[1]) : null;
64329
+ }
64330
+ var round = (value, digits = 0) => Math.round(value * 10 ** digits) / 10 ** digits;
64331
+ var average = (values) => values.reduce((sum, value) => sum + value, 0) / values.length;
64332
+ function maxPressure(samples, key) {
64333
+ const values = samples.map((sample) => sample.pressure[key]).filter((value) => value !== null);
64334
+ return values.length ? Math.max(...values) : null;
64335
+ }
64336
+ var ResourceMonitor = class {
64337
+ timer = null;
64338
+ sampling = false;
64339
+ previousCpu = null;
64340
+ oomBaseline = null;
64341
+ minute = null;
64342
+ pending = [];
64343
+ cgroupEventsPath = readOptional("/proc/self/cgroup").then((text) => text?.match(/^0::(.*)$/m)?.[1]).then((path7) => path7 ? `/sys/fs/cgroup${path7.replace(/\/$/, "")}/memory.events` : null);
64344
+ start() {
64345
+ if (this.timer) return;
64346
+ this.oomBaseline = this.readOomCounters();
64347
+ void this.sample();
64348
+ this.timer = setInterval(() => void this.sample(), SAMPLE_INTERVAL_MS);
64349
+ this.timer.unref();
64350
+ }
64351
+ stop() {
64352
+ if (this.timer) clearInterval(this.timer);
64353
+ this.timer = null;
64354
+ this.previousCpu = null;
64355
+ if (this.minute) void this.finishMinute(this.minute);
64356
+ this.minute = null;
64357
+ }
64358
+ pendingMinutes() {
64359
+ return [...this.pending];
64360
+ }
64361
+ acknowledge(sent) {
64362
+ const last = sent.at(-1)?.minute;
64363
+ if (last) this.pending = this.pending.filter((minute) => minute.minute > last);
64364
+ }
64365
+ async readOomCounters() {
64366
+ const eventsPath = await this.cgroupEventsPath;
64367
+ const [vmstat, events] = await Promise.all([readOptional("/proc/vmstat"), eventsPath ? readOptional(eventsPath) : null]);
64368
+ return { vmstat: parseCounters(vmstat).oom_kill ?? 0, cgroup: parseCounters(events).oom_kill ?? 0 };
64369
+ }
64370
+ async sample() {
64371
+ if (this.sampling) return;
64372
+ this.sampling = true;
64373
+ try {
64374
+ const [stat8, meminfo, cpuPressure, memoryPressure, ioPressure] = await Promise.all([
64375
+ readFile22("/proc/stat", "utf8"),
64376
+ readFile22("/proc/meminfo", "utf8"),
64377
+ readOptional("/proc/pressure/cpu"),
64378
+ readOptional("/proc/pressure/memory"),
64379
+ readOptional("/proc/pressure/io")
64380
+ ]);
64381
+ if (!this.timer) return;
64382
+ const lines = stat8.split("\n");
64383
+ const jiffies = lines[0].trim().split(/\s+/).slice(1, 9).map(Number);
64384
+ const total = jiffies.reduce((sum, value) => sum + value, 0);
64385
+ const cpu = { busy: total - jiffies[3] - jiffies[4], total };
64386
+ const previous = this.previousCpu;
64387
+ this.previousCpu = cpu;
64388
+ if (!previous || cpu.total <= previous.total) return;
64389
+ const cpuCount = lines.filter((line) => /^cpu\d/.test(line)).length;
64390
+ const memory = parseCounters(meminfo);
64391
+ const cacheKb = memory.Buffers + memory.Cached + (memory.SReclaimable ?? 0);
64392
+ const sample = {
64393
+ cpuCount,
64394
+ memoryTotalMb: memory.MemTotal / 1024,
64395
+ busyCpus: (cpu.busy - previous.busy) / (cpu.total - previous.total) * cpuCount,
64396
+ usedMb: (memory.MemTotal - memory.MemFree - cacheKb) / 1024,
64397
+ cacheMb: cacheKb / 1024,
64398
+ availableMb: memory.MemAvailable / 1024,
64399
+ pressure: {
64400
+ cpuSome: parsePressure(cpuPressure, "some"),
64401
+ cpuFull: parsePressure(cpuPressure, "full"),
64402
+ memorySome: parsePressure(memoryPressure, "some"),
64403
+ memoryFull: parsePressure(memoryPressure, "full"),
64404
+ ioSome: parsePressure(ioPressure, "some"),
64405
+ ioFull: parsePressure(ioPressure, "full")
64406
+ }
64407
+ };
64408
+ const start = Math.floor(Date.now() / 6e4) * 6e4;
64409
+ if (this.minute && this.minute.start !== start) {
64410
+ const finished = this.minute;
64411
+ this.minute = null;
64412
+ await this.finishMinute(finished);
64413
+ }
64414
+ this.minute ??= { start, samples: [] };
64415
+ this.minute.samples.push(sample);
64416
+ } catch (error) {
64417
+ console.warn("[ResourceMonitor] Sample failed:", error);
64418
+ } finally {
64419
+ this.sampling = false;
64420
+ }
64421
+ }
64422
+ async finishMinute({ start, samples }) {
64423
+ const counters = this.readOomCounters();
64424
+ const [baseline, current] = await Promise.all([this.oomBaseline, counters]);
64425
+ this.oomBaseline = counters;
64426
+ const latest = samples[samples.length - 1];
64427
+ this.pending.push({
64428
+ minute: new Date(start).toISOString(),
64429
+ samples: samples.length,
64430
+ cpuCount: latest.cpuCount,
64431
+ memoryTotalMb: round(latest.memoryTotalMb),
64432
+ cpuBusyAvg: round(average(samples.map((sample) => sample.busyCpus)), 2),
64433
+ cpuBusyMax: round(Math.max(...samples.map((sample) => sample.busyCpus)), 2),
64434
+ memoryUsedAvgMb: round(average(samples.map((sample) => sample.usedMb))),
64435
+ memoryUsedMaxMb: round(Math.max(...samples.map((sample) => sample.usedMb))),
64436
+ memoryCacheAvgMb: round(average(samples.map((sample) => sample.cacheMb))),
64437
+ memoryAvailableMinMb: round(Math.min(...samples.map((sample) => sample.availableMb))),
64438
+ cpuPressureSomeMax: maxPressure(samples, "cpuSome"),
64439
+ cpuPressureFullMax: maxPressure(samples, "cpuFull"),
64440
+ memoryPressureSomeMax: maxPressure(samples, "memorySome"),
64441
+ memoryPressureFullMax: maxPressure(samples, "memoryFull"),
64442
+ ioPressureSomeMax: maxPressure(samples, "ioSome"),
64443
+ ioPressureFullMax: maxPressure(samples, "ioFull"),
64444
+ // /proc/vmstat counts every kill in a VM; the cgroup counter covers sandboxes where /proc shows the host.
64445
+ oomKills: baseline ? Math.max(current.vmstat - baseline.vmstat, current.cgroup - baseline.cgroup, 0) : 0
64446
+ });
64447
+ if (this.pending.length > MAX_PENDING_RESOURCE_MINUTES) this.pending.splice(0, this.pending.length - MAX_PENDING_RESOURCE_MINUTES);
64448
+ }
64449
+ };
64450
+ var resourceMonitor = new ResourceMonitor();
64451
+
64313
64452
  // src/services/heartbeat-service.ts
64314
64453
  function unrefTimer(timer) {
64315
64454
  if (typeof timer === "object" && timer !== null && "unref" in timer && typeof timer.unref === "function") {
@@ -64325,6 +64464,7 @@ var HeartbeatService = class _HeartbeatService {
64325
64464
  start(bootTimeMs2) {
64326
64465
  if (!ENGINE_ENV.REPLICAS_WORKSPACE_ID || this.interval) return;
64327
64466
  this.bootTimeMs = bootTimeMs2;
64467
+ resourceMonitor.start();
64328
64468
  void this.ping();
64329
64469
  this.interval = setInterval(() => {
64330
64470
  void this.ping();
@@ -64334,6 +64474,7 @@ var HeartbeatService = class _HeartbeatService {
64334
64474
  stop() {
64335
64475
  if (this.interval) clearInterval(this.interval);
64336
64476
  this.interval = null;
64477
+ resourceMonitor.stop();
64337
64478
  }
64338
64479
  async ping() {
64339
64480
  if (this.inFlight || !this.bootTimeMs) return;
@@ -64341,17 +64482,22 @@ var HeartbeatService = class _HeartbeatService {
64341
64482
  const controller = new AbortController();
64342
64483
  const timeout = setTimeout(() => controller.abort(), _HeartbeatService.REQUEST_TIMEOUT_MS);
64343
64484
  unrefTimer(timeout);
64485
+ const resources = resourceMonitor.pendingMinutes();
64344
64486
  try {
64345
64487
  const body = {
64346
64488
  bootTimeMs: this.bootTimeMs,
64347
64489
  uptimeSeconds: Math.floor((Date.now() - this.bootTimeMs) / 1e3),
64348
- status: "active"
64490
+ status: "active",
64491
+ ...resources.length ? { resources } : {}
64349
64492
  };
64350
64493
  const response = await monolithRequest("/v1/engine/heartbeat", {
64351
64494
  body,
64352
64495
  signal: controller.signal
64353
64496
  });
64354
- if (!response.ok) {
64497
+ if (response.ok) {
64498
+ const reply = await response.json().catch(() => null);
64499
+ if (isRecord(reply) && reply.resourcesRecorded === true) resourceMonitor.acknowledge(resources);
64500
+ } else {
64355
64501
  console.warn(`[EngineHeartbeat] Ping returned ${response.status}`);
64356
64502
  }
64357
64503
  } catch (error) {
@@ -3,13 +3,13 @@ import { createRequire as __createRequire } from 'node:module';
3
3
  const require = __createRequire(import.meta.url);
4
4
  import {
5
5
  publishedPullRequestUrls
6
- } from "./chunk-OBKIRWWN.js";
6
+ } from "./chunk-AVI4QYDL.js";
7
7
  import {
8
8
  isRecord
9
9
  } from "./chunk-UZSNFLDQ.js";
10
10
  import {
11
11
  shouldRefreshPullRequests
12
- } from "./chunk-TQKWK7JJ.js";
12
+ } from "./chunk-NOHCO22T.js";
13
13
  import "./chunk-VEQXQN22.js";
14
14
 
15
15
  // src/services/post-tool-pr-notifier.ts
@@ -11,7 +11,7 @@ import {
11
11
  spawnSubagentRequestSchema,
12
12
  subagentResponseSchema,
13
13
  waitSubagentRequestSchema
14
- } from "./chunk-TQKWK7JJ.js";
14
+ } from "./chunk-NOHCO22T.js";
15
15
  import "./chunk-VEQXQN22.js";
16
16
 
17
17
  // src/subagent-mcp.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replicas-engine",
3
- "version": "0.1.1372",
3
+ "version": "0.1.1373",
4
4
  "description": "Lightweight API server for Replicas workspaces",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",