@modootoday/pmcp 0.26.1 → 0.27.0
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/.agents/plugins/marketplace.json +1 -1
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.mcp.json +1 -1
- package/dist/{apply-7HWNHSQB.js → apply-YNBY2GPK.js} +7 -7
- package/dist/{chunk-BSG74PTY.js → chunk-5TT7YAEP.js} +3 -3
- package/dist/chunk-674APJDK.js +128 -0
- package/dist/chunk-6TM7PLXG.js +154 -0
- package/dist/{chunk-5ZP3ABPI.js → chunk-6U6MLIS2.js} +2 -2
- package/dist/{chunk-GYL2C2Y5.js → chunk-ENFF7ZBC.js} +2 -2
- package/dist/{chunk-XPLT6DJC.js → chunk-GVKVMSWX.js} +25 -23
- package/dist/{chunk-T52AGUO7.js → chunk-JTKGDT2K.js} +2 -2
- package/dist/{chunk-UHKHXS3E.js → chunk-L5EJK24I.js} +2 -2
- package/dist/{chunk-3T7CJS4A.js → chunk-LKH57HLV.js} +2 -2
- package/dist/chunk-LXJTABBQ.js +1148 -0
- package/dist/{chunk-OHCUPXU4.js → chunk-LXWAU7AX.js} +65 -2
- package/dist/{chunk-ZT75ACJ2.js → chunk-M5GNZSOJ.js} +2 -2
- package/dist/chunk-MXVP3ICV.js +7 -0
- package/dist/{chunk-2VL3C52Y.js → chunk-OY7XYKVL.js} +2 -2
- package/dist/{chunk-M2XABD33.js → chunk-OZTEMCCM.js} +3 -3
- package/dist/{chunk-CJD2HFXF.js → chunk-PH3P5WEY.js} +3 -3
- package/dist/{chunk-MFQAQ4IJ.js → chunk-Q3XLIRIJ.js} +4 -4
- package/dist/{chunk-E7BPELYH.js → chunk-TVA2IN6H.js} +5 -5
- package/dist/chunk-TYXJBXZQ.js +441 -0
- package/dist/chunk-WG7KH46S.js +71 -0
- package/dist/{chunk-GIK4TROM.js → chunk-X2WDR3UK.js} +2 -2
- package/dist/{chunk-A7MF7RMO.js → chunk-X3LNF5KJ.js} +5 -5
- package/dist/{chunk-KWEW5EBE.js → chunk-XCIFD7G5.js} +2 -2
- package/dist/{chunk-JDVDA5HW.js → chunk-Y5WIIEG2.js} +2 -2
- package/dist/{chunk-RGTWJWQ7.js → chunk-ZO5CRS2T.js} +2 -2
- package/dist/cli.js +10 -10
- package/dist/{completion-O3KRUSJG.js → completion-WKIMMLNF.js} +6 -6
- package/dist/{diagnostics-PB5TKIAL.js → diagnostics-LCNF5NRK.js} +5 -5
- package/dist/doctor-NKFZNSRD.js +106 -0
- package/dist/{doctor-JVWINJT5.js → doctor-PSMPNQXS.js} +9 -9
- package/dist/{entry-CY74PLN3.js → entry-LMAWTZM2.js} +11 -11
- package/dist/{exec-mcp-HAKUGQZM.js → exec-mcp-VAP46MKO.js} +5 -5
- package/dist/{harness-N234LNM7.js → harness-ZCMU5MJD.js} +3 -3
- package/dist/harness-launch-gate.js +3 -3
- package/dist/harness-worker.js +5 -5
- package/dist/heavy-VBC4AGL6.js +1063 -0
- package/dist/{hooks-7K624JQC.js → hooks-AWAO7CQO.js} +3 -3
- package/dist/{host-CCJFOCZJ.js → host-4GFQQJUS.js} +7 -7
- package/dist/{host-PT7NRWG6.js → host-I4XIJTXR.js} +6 -6
- package/dist/host-OBTC2PGQ.js +32 -0
- package/dist/{host-run-RRBFTVYW.js → host-run-T3FC7L6I.js} +4 -4
- package/dist/http.js +1 -1
- package/dist/{import-mcp-GCNNSLWH.js → import-mcp-5JSGA5IS.js} +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -8
- package/dist/{init-A4NLTERR.js → init-PZYWBDZS.js} +2 -2
- package/dist/{invoke-G4LNWPTL.js → invoke-DCKJCMRB.js} +7 -7
- package/dist/jobs-FLHB3WER.js +26 -0
- package/dist/{list-EZYULVDW.js → list-ZEU7T3XK.js} +6 -6
- package/dist/{logout-BYO3UJUH.js → logout-CUSOVMKL.js} +5 -5
- package/dist/{mcp-CZNYDYTJ.js → mcp-ZHOPOFXI.js} +2 -2
- package/dist/measure-AUBPYT6L.js +129 -0
- package/dist/plan-46BTJQIV.js +98 -0
- package/dist/{project-GLT3SO7Y.js → project-T36TRAWT.js} +4 -4
- package/dist/{run-OBQBKBPV.js → run-37PVE74I.js} +4 -4
- package/dist/{run-J5O2V3MB.js → run-LMSHQJPQ.js} +4 -4
- package/dist/{serve-QCG4PCVS.js → serve-E4FD6PAL.js} +3 -3
- package/dist/{start-login-LRO3RTHD.js → start-login-JSKFXSLY.js} +5 -5
- package/dist/{status-BQLT7JRE.js → status-ZNWF4362.js} +5 -5
- package/dist/{status-EZTFSD5S.js → status-ZSK3PNM2.js} +6 -6
- package/dist/stdio.js +2 -2
- package/dist/{test-O7TN5SBA.js → test-OCTC2DSZ.js} +5 -5
- package/dist/{tui-ODLF6QWC.js → tui-LE2KPUMI.js} +4 -4
- package/dist/{worker-AK4GLGPZ.js → worker-6U67LWIP.js} +9 -9
- package/docs/commands.md +24 -8
- package/docs/project-configuration.md +1 -1
- package/gemini-extension.json +2 -2
- package/mcp.json +1 -1
- package/package.json +5 -5
- package/plugin.json +1 -1
- package/plugins/pmcp/.claude-plugin/plugin.json +1 -1
- package/plugins/pmcp/.mcp.json +1 -1
- package/plugins/pmcp/gemini-extension.json +2 -2
- package/plugins/pmcp/mcp.json +1 -1
- package/plugins/pmcp/plugin.json +1 -1
- package/dist/chunk-EIBKHS74.js +0 -7
- package/dist/heavy-X3KZ7WBU.js +0 -2211
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyIo,
|
|
3
|
+
cgroupHost,
|
|
4
|
+
ioDelegated,
|
|
5
|
+
scopeCgroup
|
|
6
|
+
} from "./chunk-WG7KH46S.js";
|
|
7
|
+
import {
|
|
8
|
+
flock
|
|
9
|
+
} from "./chunk-SGFTHKKS.js";
|
|
10
|
+
import {
|
|
11
|
+
inferSession
|
|
12
|
+
} from "./chunk-65BW7Y6O.js";
|
|
13
|
+
import {
|
|
14
|
+
flockAvailable
|
|
15
|
+
} from "./chunk-DZ2MRTYT.js";
|
|
16
|
+
import {
|
|
17
|
+
holdOnClosedPipe
|
|
18
|
+
} from "./chunk-X6W7JIED.js";
|
|
19
|
+
import {
|
|
20
|
+
BUSY_EXIT,
|
|
21
|
+
DISABLE_ENV
|
|
22
|
+
} from "./chunk-PZ6D3WKD.js";
|
|
23
|
+
import {
|
|
24
|
+
LANES,
|
|
25
|
+
epoch,
|
|
26
|
+
leaseHeld,
|
|
27
|
+
leasePaths,
|
|
28
|
+
liveRecords,
|
|
29
|
+
liveWaiters,
|
|
30
|
+
logEvent,
|
|
31
|
+
newId,
|
|
32
|
+
nowUtc,
|
|
33
|
+
oneLine,
|
|
34
|
+
pidAlive,
|
|
35
|
+
readRecord,
|
|
36
|
+
removeRecord,
|
|
37
|
+
writeRecord
|
|
38
|
+
} from "./chunk-N7ANFMA5.js";
|
|
39
|
+
|
|
40
|
+
// src/lease/queue.ts
|
|
41
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
42
|
+
|
|
43
|
+
// src/lease/host.ts
|
|
44
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
45
|
+
function hostSources(env) {
|
|
46
|
+
return {
|
|
47
|
+
loadavg: env["PMCP_HEAVY_LOADAVG_FILE"] || "/proc/loadavg",
|
|
48
|
+
pressure: env["PMCP_HEAVY_PRESSURE_FILE"] || "/proc/pressure/io"
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function read(file) {
|
|
52
|
+
try {
|
|
53
|
+
return readFileSync(file, "utf8");
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function readHost(sources) {
|
|
59
|
+
const load = read(sources.loadavg)?.split(" ")[0]?.trim() || null;
|
|
60
|
+
const some = read(sources.pressure)?.match(/^some (.*)$/mu)?.[1] ?? "";
|
|
61
|
+
const pressure = {};
|
|
62
|
+
for (const [, key, value] of some.matchAll(/(avg\d+)=([0-9.]+)/gu))
|
|
63
|
+
pressure[key] = value;
|
|
64
|
+
return { load, io: pressure["avg10"] ?? null, pressure };
|
|
65
|
+
}
|
|
66
|
+
function hostNow(reading) {
|
|
67
|
+
return `load ${reading.load ?? "n/a"} io ${reading.io ?? "n/a"}`;
|
|
68
|
+
}
|
|
69
|
+
function below(value, max) {
|
|
70
|
+
return value === null || Number(value) < max;
|
|
71
|
+
}
|
|
72
|
+
function hostSettled(settings, paths, sources) {
|
|
73
|
+
const reading = readHost(sources);
|
|
74
|
+
if (!below(reading.load, settings.maxLoad)) return false;
|
|
75
|
+
const io2 = reading.pressure[settings.ioWindow] ?? reading.io;
|
|
76
|
+
if (!below(io2, settings.maxIo)) return false;
|
|
77
|
+
try {
|
|
78
|
+
writeFileSync(paths.settled, `${nowUtc()}
|
|
79
|
+
`);
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
function sleepSeconds(seconds) {
|
|
85
|
+
if (seconds <= 0) return;
|
|
86
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, seconds * 1e3);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/lease/queue.ts
|
|
90
|
+
function sessionOf(env, cwd) {
|
|
91
|
+
const inferred = inferSession(env, [], []).session;
|
|
92
|
+
if (inferred) return inferred;
|
|
93
|
+
try {
|
|
94
|
+
const stat = readFileSync2("/proc/self/stat", "utf8");
|
|
95
|
+
const group = stat.slice(stat.lastIndexOf(")") + 2).split(" ")[2];
|
|
96
|
+
if (group) return `pgid:${group}`;
|
|
97
|
+
} catch {
|
|
98
|
+
}
|
|
99
|
+
return `cwd:${cwd}`;
|
|
100
|
+
}
|
|
101
|
+
var CAPPED = ["interactive", "bulk"];
|
|
102
|
+
function parkForSession(park) {
|
|
103
|
+
if (park.cap === 0 || !CAPPED.includes(park.lane)) return 0;
|
|
104
|
+
const started = epoch();
|
|
105
|
+
const queued = () => liveWaiters(park.paths).filter(
|
|
106
|
+
(record2) => record2.session === park.session && record2.lane === park.lane
|
|
107
|
+
).length;
|
|
108
|
+
let count = queued();
|
|
109
|
+
if (count < park.cap) return 0;
|
|
110
|
+
park.say(
|
|
111
|
+
`parked outside the queue: this session already has ${count} queued ${park.lane} job(s) (per_session ${park.cap})`
|
|
112
|
+
);
|
|
113
|
+
for (; ; ) {
|
|
114
|
+
if (epoch() - started >= park.maxWait) return null;
|
|
115
|
+
sleepSeconds(1);
|
|
116
|
+
count = queued();
|
|
117
|
+
if (count < park.cap) return epoch() - started;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/lease/scope.ts
|
|
122
|
+
import { spawnSync } from "child_process";
|
|
123
|
+
import { existsSync } from "fs";
|
|
124
|
+
import { join } from "path";
|
|
125
|
+
var BUS_FAILURE = /Failed to connect to (?:the )?bus/u;
|
|
126
|
+
var installed;
|
|
127
|
+
var probed = /* @__PURE__ */ new Map();
|
|
128
|
+
function probeScope(env) {
|
|
129
|
+
const run = spawnSync("systemd-run", ["--user", "--scope", "-q", "true"], {
|
|
130
|
+
env,
|
|
131
|
+
encoding: "utf8",
|
|
132
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
133
|
+
timeout: 1e4
|
|
134
|
+
});
|
|
135
|
+
if (run.status === 0) return null;
|
|
136
|
+
const line = (run.stderr ?? "").trim().split("\n")[0];
|
|
137
|
+
return line || `systemd-run --user exited ${run.status ?? "on a signal"}`;
|
|
138
|
+
}
|
|
139
|
+
function systemdRun() {
|
|
140
|
+
installed ??= spawnSync("systemd-run", ["--user", "--version"], { stdio: "ignore" }).status === 0;
|
|
141
|
+
return installed;
|
|
142
|
+
}
|
|
143
|
+
function userManager(exists = existsSync, probe = probeScope, platform = process.platform, present = systemdRun) {
|
|
144
|
+
return {
|
|
145
|
+
unreachable(env) {
|
|
146
|
+
if (platform !== "linux") return "not Linux";
|
|
147
|
+
if (!present()) return "systemd-run is not installed";
|
|
148
|
+
const runtime = env["XDG_RUNTIME_DIR"];
|
|
149
|
+
if (!runtime) return "XDG_RUNTIME_DIR is not set";
|
|
150
|
+
const socket = exists(join(runtime, "systemd", "private")) || exists(join(runtime, "bus")) || Boolean(env["DBUS_SESSION_BUS_ADDRESS"]);
|
|
151
|
+
if (!socket) return `no user manager socket in ${runtime}`;
|
|
152
|
+
if (!probed.has(runtime)) probed.set(runtime, probe(env));
|
|
153
|
+
return probed.get(runtime) ?? null;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function lost(memoryMax, io2) {
|
|
158
|
+
const parts = [
|
|
159
|
+
...memoryMax === void 0 ? [] : [`MemoryMax=${memoryMax}`],
|
|
160
|
+
...io2 ? ["io.weight"] : []
|
|
161
|
+
];
|
|
162
|
+
return parts.join(" and ");
|
|
163
|
+
}
|
|
164
|
+
function wantsIo(settings, lane, host) {
|
|
165
|
+
if (lane === null || settings.io.enabled === false) return false;
|
|
166
|
+
return ioDelegated(host);
|
|
167
|
+
}
|
|
168
|
+
async function weigh(unit, settings, lane, host, say, onCgroup) {
|
|
169
|
+
for (let tries = 0; tries < 30; tries += 1) {
|
|
170
|
+
const scope = scopeCgroup(unit);
|
|
171
|
+
if (scope !== null) {
|
|
172
|
+
onCgroup(join(host.root, scope));
|
|
173
|
+
const set = applyIo(
|
|
174
|
+
host,
|
|
175
|
+
scope,
|
|
176
|
+
settings.io.weights[lane],
|
|
177
|
+
settings.io.max[lane]
|
|
178
|
+
);
|
|
179
|
+
if (!set.weight)
|
|
180
|
+
say(`io control did not reach ${scope}; running without io.weight`);
|
|
181
|
+
if (set.max === "failed")
|
|
182
|
+
say(`io.max for the ${lane} lane was refused; the job runs without it`);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function scoped(argv, settings, lane, memoryMax, say, onCgroup = () => {
|
|
189
|
+
}, host = cgroupHost(), manager = userManager(), env = process.env) {
|
|
190
|
+
const io2 = wantsIo(settings, lane, host);
|
|
191
|
+
if (memoryMax === void 0 && !io2) return { argv };
|
|
192
|
+
const reason = manager.unreachable(env);
|
|
193
|
+
if (reason !== null) {
|
|
194
|
+
say(
|
|
195
|
+
`no systemd user scope (${reason}); running without ${lost(memoryMax, io2)}`
|
|
196
|
+
);
|
|
197
|
+
return { argv };
|
|
198
|
+
}
|
|
199
|
+
const unit = `pmcp-heavy-${process.pid}-${Date.now()}`;
|
|
200
|
+
const limits = memoryMax === void 0 ? [] : ["-p", `MemoryMax=${memoryMax}`, "-p", "MemorySwapMax=0"];
|
|
201
|
+
return {
|
|
202
|
+
argv: [
|
|
203
|
+
"systemd-run",
|
|
204
|
+
"--user",
|
|
205
|
+
"--scope",
|
|
206
|
+
"-q",
|
|
207
|
+
`--unit=${unit}`,
|
|
208
|
+
...limits,
|
|
209
|
+
"--",
|
|
210
|
+
...argv
|
|
211
|
+
],
|
|
212
|
+
unscoped: argv,
|
|
213
|
+
...io2 && lane !== null ? { started: () => weigh(unit, settings, lane, host, say, onCgroup) } : {}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/lease/usage.ts
|
|
218
|
+
import {
|
|
219
|
+
appendFileSync,
|
|
220
|
+
readFileSync as readFileSync3,
|
|
221
|
+
readdirSync,
|
|
222
|
+
renameSync,
|
|
223
|
+
statSync
|
|
224
|
+
} from "fs";
|
|
225
|
+
import { join as join2, relative } from "path";
|
|
226
|
+
var WORD = /^[a-z][a-z0-9:_-]{0,39}$/u;
|
|
227
|
+
function signature(argv) {
|
|
228
|
+
const [program = "", ...rest] = argv;
|
|
229
|
+
const words = [program.slice(program.lastIndexOf("/") + 1)];
|
|
230
|
+
for (const word of rest) {
|
|
231
|
+
if (!WORD.test(word)) break;
|
|
232
|
+
words.push(word);
|
|
233
|
+
}
|
|
234
|
+
return words.join(" ");
|
|
235
|
+
}
|
|
236
|
+
function packageDir(cwd, root) {
|
|
237
|
+
const path = relative(root, cwd);
|
|
238
|
+
return path === "" ? "." : path.startsWith("..") ? "(outside)" : path;
|
|
239
|
+
}
|
|
240
|
+
function appendUsage(file, usage, cap) {
|
|
241
|
+
try {
|
|
242
|
+
if (statSync(file).size >= cap) renameSync(file, `${file}.1`);
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
appendFileSync(file, `${JSON.stringify(usage)}
|
|
247
|
+
`);
|
|
248
|
+
} catch {
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function readUsage(file) {
|
|
252
|
+
const found = [];
|
|
253
|
+
for (const path of [`${file}.1`, file]) {
|
|
254
|
+
let text = "";
|
|
255
|
+
try {
|
|
256
|
+
text = readFileSync3(path, "utf8");
|
|
257
|
+
} catch {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
for (const line of text.split("\n")) {
|
|
261
|
+
if (!line.trim()) continue;
|
|
262
|
+
try {
|
|
263
|
+
found.push(JSON.parse(line));
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return found;
|
|
269
|
+
}
|
|
270
|
+
var METRICS = [
|
|
271
|
+
"wall_s",
|
|
272
|
+
"user_s",
|
|
273
|
+
"sys_s",
|
|
274
|
+
"max_rss_kb",
|
|
275
|
+
"read_bytes",
|
|
276
|
+
"write_bytes"
|
|
277
|
+
];
|
|
278
|
+
function skipShare(records) {
|
|
279
|
+
const ran = records.filter((record2) => record2.outcome === "ran");
|
|
280
|
+
return {
|
|
281
|
+
skipped: ran.filter((record2) => record2.skip !== void 0).length,
|
|
282
|
+
runs: ran.length
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function skipReasons(records) {
|
|
286
|
+
const honoured = {};
|
|
287
|
+
const invalid = {};
|
|
288
|
+
for (const record2 of records) {
|
|
289
|
+
if (record2.skip !== void 0)
|
|
290
|
+
honoured[record2.skip] = (honoured[record2.skip] ?? 0) + 1;
|
|
291
|
+
if (record2.skip_invalid !== void 0)
|
|
292
|
+
invalid[record2.skip_invalid] = (invalid[record2.skip_invalid] ?? 0) + 1;
|
|
293
|
+
}
|
|
294
|
+
return { honoured, invalid };
|
|
295
|
+
}
|
|
296
|
+
function percentile(values, share) {
|
|
297
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
298
|
+
return sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * share))] ?? 0;
|
|
299
|
+
}
|
|
300
|
+
function usageStats(records) {
|
|
301
|
+
const groups = /* @__PURE__ */ new Map();
|
|
302
|
+
for (const record2 of records) {
|
|
303
|
+
const key = `${record2.signature} @ ${record2.dir}`;
|
|
304
|
+
groups.set(key, [...groups.get(key) ?? [], record2]);
|
|
305
|
+
}
|
|
306
|
+
return [...groups.entries()].map(([key, list]) => {
|
|
307
|
+
const ran = list.filter((record2) => record2.outcome === "ran");
|
|
308
|
+
const pick = (share) => Object.fromEntries(
|
|
309
|
+
METRICS.map((metric) => [
|
|
310
|
+
metric,
|
|
311
|
+
percentile(
|
|
312
|
+
ran.map((record2) => record2[metric]),
|
|
313
|
+
share
|
|
314
|
+
)
|
|
315
|
+
])
|
|
316
|
+
);
|
|
317
|
+
return {
|
|
318
|
+
signature: key,
|
|
319
|
+
samples: ran.length,
|
|
320
|
+
failed: ran.filter((record2) => record2.exit !== 0).length,
|
|
321
|
+
timeouts: list.filter(
|
|
322
|
+
(record2) => record2.outcome !== "ran" && record2.outcome !== "invalid"
|
|
323
|
+
).length,
|
|
324
|
+
skipped: ran.filter((record2) => record2.skip !== void 0).length,
|
|
325
|
+
p50: pick(0.5),
|
|
326
|
+
p90: pick(0.9)
|
|
327
|
+
};
|
|
328
|
+
}).sort(
|
|
329
|
+
(a, b) => b.samples - a.samples || a.signature.localeCompare(b.signature)
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
var Sampler = class {
|
|
333
|
+
constructor(proc = "/proc") {
|
|
334
|
+
this.proc = proc;
|
|
335
|
+
this.before = this.children();
|
|
336
|
+
}
|
|
337
|
+
proc;
|
|
338
|
+
ticks = 100;
|
|
339
|
+
before;
|
|
340
|
+
peakRss = 0;
|
|
341
|
+
io = /* @__PURE__ */ new Map();
|
|
342
|
+
cgroup = null;
|
|
343
|
+
cgroupIo = { read: 0, write: 0 };
|
|
344
|
+
cgroupPeak = 0;
|
|
345
|
+
children() {
|
|
346
|
+
try {
|
|
347
|
+
const stat = readFileSync3(`${this.proc}/self/stat`, "utf8");
|
|
348
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
349
|
+
return { user: Number(fields[13]), sys: Number(fields[14]) };
|
|
350
|
+
} catch {
|
|
351
|
+
return { user: 0, sys: 0 };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
useCgroup(dir) {
|
|
355
|
+
this.cgroup = dir;
|
|
356
|
+
}
|
|
357
|
+
sample(root) {
|
|
358
|
+
if (this.cgroup) this.sampleCgroup(this.cgroup);
|
|
359
|
+
if (root === void 0) return;
|
|
360
|
+
const parents = /* @__PURE__ */ new Map();
|
|
361
|
+
let names = [];
|
|
362
|
+
try {
|
|
363
|
+
names = readdirSync(this.proc).filter((name) => /^\d+$/u.test(name));
|
|
364
|
+
} catch {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
for (const name of names) {
|
|
368
|
+
try {
|
|
369
|
+
const stat = readFileSync3(`${this.proc}/${name}/stat`, "utf8");
|
|
370
|
+
parents.set(
|
|
371
|
+
Number(name),
|
|
372
|
+
Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1])
|
|
373
|
+
);
|
|
374
|
+
} catch {
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const inTree = (pid) => {
|
|
378
|
+
for (let at = pid, hops = 0; at > 1 && hops < 64; hops += 1) {
|
|
379
|
+
if (at === root) return true;
|
|
380
|
+
at = parents.get(at) ?? 0;
|
|
381
|
+
}
|
|
382
|
+
return false;
|
|
383
|
+
};
|
|
384
|
+
let rss = 0;
|
|
385
|
+
for (const pid of parents.keys()) {
|
|
386
|
+
if (!inTree(pid)) continue;
|
|
387
|
+
try {
|
|
388
|
+
const status = readFileSync3(`${this.proc}/${pid}/status`, "utf8");
|
|
389
|
+
rss += Number(status.match(/^VmRSS:\s+(\d+)/mu)?.[1] ?? 0);
|
|
390
|
+
const io2 = readFileSync3(`${this.proc}/${pid}/io`, "utf8");
|
|
391
|
+
const value = (key) => Number(io2.match(new RegExp(`^${key}: (\\d+)`, "mu"))?.[1] ?? 0);
|
|
392
|
+
this.io.set(pid, {
|
|
393
|
+
read: value("read_bytes"),
|
|
394
|
+
write: value("write_bytes")
|
|
395
|
+
});
|
|
396
|
+
} catch {
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
this.peakRss = Math.max(this.peakRss, rss);
|
|
400
|
+
}
|
|
401
|
+
sampleCgroup(dir) {
|
|
402
|
+
try {
|
|
403
|
+
const peak = Number(
|
|
404
|
+
readFileSync3(join2(dir, "memory.peak"), "utf8").trim()
|
|
405
|
+
);
|
|
406
|
+
if (peak > 0)
|
|
407
|
+
this.cgroupPeak = Math.max(this.cgroupPeak, Math.round(peak / 1024));
|
|
408
|
+
} catch {
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
let read2 = 0;
|
|
412
|
+
let write = 0;
|
|
413
|
+
for (const line of readFileSync3(join2(dir, "io.stat"), "utf8").split(
|
|
414
|
+
"\n"
|
|
415
|
+
)) {
|
|
416
|
+
read2 += Number(line.match(/rbytes=(\d+)/u)?.[1] ?? 0);
|
|
417
|
+
write += Number(line.match(/wbytes=(\d+)/u)?.[1] ?? 0);
|
|
418
|
+
}
|
|
419
|
+
this.cgroupIo = { read: read2, write };
|
|
420
|
+
} catch {
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
finish() {
|
|
424
|
+
const after = this.children();
|
|
425
|
+
const cpu = {
|
|
426
|
+
user_s: Math.max(0, after.user - this.before.user) / this.ticks,
|
|
427
|
+
sys_s: Math.max(0, after.sys - this.before.sys) / this.ticks
|
|
428
|
+
};
|
|
429
|
+
if (this.cgroup && (this.cgroupPeak > 0 || this.cgroupIo.read + this.cgroupIo.write > 0))
|
|
430
|
+
return {
|
|
431
|
+
...cpu,
|
|
432
|
+
max_rss_kb: this.cgroupPeak || this.peakRss,
|
|
433
|
+
read_bytes: this.cgroupIo.read,
|
|
434
|
+
write_bytes: this.cgroupIo.write,
|
|
435
|
+
source: "cgroup"
|
|
436
|
+
};
|
|
437
|
+
let read2 = 0;
|
|
438
|
+
let write = 0;
|
|
439
|
+
for (const value of this.io.values()) {
|
|
440
|
+
read2 += value.read;
|
|
441
|
+
write += value.write;
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
...cpu,
|
|
445
|
+
max_rss_kb: this.peakRss,
|
|
446
|
+
read_bytes: read2,
|
|
447
|
+
write_bytes: write,
|
|
448
|
+
source: "proc"
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
// src/lease/run.ts
|
|
454
|
+
import { spawn } from "child_process";
|
|
455
|
+
import { constants } from "os";
|
|
456
|
+
|
|
457
|
+
// src/lease/acquire.ts
|
|
458
|
+
import {
|
|
459
|
+
closeSync as closeSync2,
|
|
460
|
+
mkdirSync,
|
|
461
|
+
openSync as openSync2,
|
|
462
|
+
readFileSync as readFileSync4,
|
|
463
|
+
rmSync,
|
|
464
|
+
writeFileSync as writeFileSync2
|
|
465
|
+
} from "fs";
|
|
466
|
+
|
|
467
|
+
// src/lease/admit.ts
|
|
468
|
+
import { closeSync, openSync } from "fs";
|
|
469
|
+
import { join as join3 } from "path";
|
|
470
|
+
function admissible(settings, running, waiters) {
|
|
471
|
+
const free = { ...settings.slots };
|
|
472
|
+
let budget = settings.budget;
|
|
473
|
+
for (const job of running) {
|
|
474
|
+
if (job.weight === 0) continue;
|
|
475
|
+
free[job.lane] -= 1;
|
|
476
|
+
budget -= job.weight;
|
|
477
|
+
}
|
|
478
|
+
const found = new Set(
|
|
479
|
+
waiters.filter((waiter) => waiter.weight === 0).map((w) => w.pid)
|
|
480
|
+
);
|
|
481
|
+
for (const waiter of waiters) {
|
|
482
|
+
if (waiter.weight === 0 || free[waiter.lane] <= 0) continue;
|
|
483
|
+
if (waiter.weight > budget) break;
|
|
484
|
+
free[waiter.lane] -= 1;
|
|
485
|
+
budget -= waiter.weight;
|
|
486
|
+
found.add(waiter.pid);
|
|
487
|
+
}
|
|
488
|
+
return found;
|
|
489
|
+
}
|
|
490
|
+
function runningWeight(running) {
|
|
491
|
+
return running.reduce((sum, job) => sum + job.weight, 0);
|
|
492
|
+
}
|
|
493
|
+
function laneUse(settings, running) {
|
|
494
|
+
const used = (lane) => running.filter((job) => job.lane === lane).length;
|
|
495
|
+
const lanes = LANES.map(
|
|
496
|
+
(lane) => `${lane} ${used(lane)}/${settings.slots[lane]}`
|
|
497
|
+
).join(" ");
|
|
498
|
+
return `${lanes}, weight ${runningWeight(running)}/${settings.budget}`;
|
|
499
|
+
}
|
|
500
|
+
function underAdmitLock(paths, act) {
|
|
501
|
+
const fd = openSync(paths.admit, "a");
|
|
502
|
+
try {
|
|
503
|
+
if (flock(fd, ["-w", "10"]) !== 0) return null;
|
|
504
|
+
return act();
|
|
505
|
+
} finally {
|
|
506
|
+
closeSync(fd);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function readQueue(paths, prune = true) {
|
|
510
|
+
return {
|
|
511
|
+
running: liveRecords(paths.running, prune),
|
|
512
|
+
waiters: liveRecords(paths.waiters, prune)
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function runningFile(paths, pid) {
|
|
516
|
+
return join3(paths.running, String(pid));
|
|
517
|
+
}
|
|
518
|
+
function waiterFile(paths, pid) {
|
|
519
|
+
return join3(paths.waiters, String(pid));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// src/lease/settle.ts
|
|
523
|
+
function io(settings, reading) {
|
|
524
|
+
return reading.pressure[settings.ioWindow] ?? reading.io;
|
|
525
|
+
}
|
|
526
|
+
function over(value, max) {
|
|
527
|
+
return value !== null && Number(value) >= max;
|
|
528
|
+
}
|
|
529
|
+
function settled(settings, reading) {
|
|
530
|
+
return !over(reading.load, settings.maxLoad) && !over(io(settings, reading), settings.maxIo);
|
|
531
|
+
}
|
|
532
|
+
function severe(settings, reading) {
|
|
533
|
+
const factor = settings.hardFactor;
|
|
534
|
+
return over(reading.load, settings.maxLoad * factor) || over(io(settings, reading), settings.maxIo * factor);
|
|
535
|
+
}
|
|
536
|
+
function hostVerdict(settings, reading, waiting) {
|
|
537
|
+
if (settled(settings, reading)) return "settled";
|
|
538
|
+
if (severe(settings, reading)) return "severe";
|
|
539
|
+
if (waiting.lane === "hook") return "hook";
|
|
540
|
+
if (waiting.weight === 0) return "tiny";
|
|
541
|
+
const window = settings.settleWindow;
|
|
542
|
+
if (window > 0 && waiting.sinceRelease !== null && waiting.sinceRelease >= window)
|
|
543
|
+
return "settle";
|
|
544
|
+
if (waiting.grace > 0 && waiting.waited >= waiting.grace && waiting.runningWeight === 0)
|
|
545
|
+
return "grace";
|
|
546
|
+
return "wait";
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// src/lease/skip.ts
|
|
550
|
+
var NESTED = "nested";
|
|
551
|
+
var OPERATOR = "operator";
|
|
552
|
+
var LEGACY_NESTED = /^inside pmcp heavy \d+$/u;
|
|
553
|
+
function allowedReasons(settings) {
|
|
554
|
+
return [
|
|
555
|
+
.../* @__PURE__ */ new Set([
|
|
556
|
+
NESTED,
|
|
557
|
+
OPERATOR,
|
|
558
|
+
...settings.skipReasons,
|
|
559
|
+
...settings.skipAlways
|
|
560
|
+
])
|
|
561
|
+
];
|
|
562
|
+
}
|
|
563
|
+
function skipRefusal(settings, reason, checks) {
|
|
564
|
+
if (reason === NESTED || LEGACY_NESTED.test(reason))
|
|
565
|
+
return checks.nested() ? null : `${NESTED} is the reason pmcp sets inside a leased job, and no leased job encloses this one`;
|
|
566
|
+
if (reason === OPERATOR)
|
|
567
|
+
return checks.operator() ? null : `${OPERATOR} is the operator's own skip, from the operator's terminal or with the operator token`;
|
|
568
|
+
if (settings.skipReasons.includes(reason)) return null;
|
|
569
|
+
if (settings.skipAlways.includes(reason)) return null;
|
|
570
|
+
return `"${reason}" is not a lease skip reason`;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// src/lease/acquire.ts
|
|
574
|
+
var STEP_SECONDS = 1;
|
|
575
|
+
function skipSource(settings, env) {
|
|
576
|
+
for (const variable of [DISABLE_ENV, settings.legacySkipEnv]) {
|
|
577
|
+
const reason = variable ? env[variable] : void 0;
|
|
578
|
+
if (variable && reason) return { reason, variable };
|
|
579
|
+
}
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
function skipReason(settings, env) {
|
|
583
|
+
return skipSource(settings, env)?.reason ?? null;
|
|
584
|
+
}
|
|
585
|
+
function reentrant(settings, env) {
|
|
586
|
+
const paths = leasePaths(settings.dir);
|
|
587
|
+
const names = [settings.markerEnv, settings.legacyMarkerEnv];
|
|
588
|
+
const markers = names.flatMap(
|
|
589
|
+
(name) => name && env[name] ? [env[name]] : []
|
|
590
|
+
);
|
|
591
|
+
if (markers.length === 0) return false;
|
|
592
|
+
if (liveRecords(paths.running).some((job) => markers.includes(String(job.pid))))
|
|
593
|
+
return true;
|
|
594
|
+
const holder = readRecord(paths.holder);
|
|
595
|
+
if (holder === null || !markers.includes(String(holder.pid))) return false;
|
|
596
|
+
return pidAlive(holder.pid) && leaseHeld(paths);
|
|
597
|
+
}
|
|
598
|
+
function profileLane(profile) {
|
|
599
|
+
if (profile === "gate" || profile === "hook") return profile;
|
|
600
|
+
return "interactive";
|
|
601
|
+
}
|
|
602
|
+
function position(paths, pid) {
|
|
603
|
+
const index = liveRecords(paths.waiters).findIndex((r) => r.pid === pid);
|
|
604
|
+
return index < 0 ? 0 : index + 1;
|
|
605
|
+
}
|
|
606
|
+
function tryAdmit(options, paths, fd, start, registered) {
|
|
607
|
+
let fields = start;
|
|
608
|
+
const pid = process.pid;
|
|
609
|
+
const outcome = underAdmitLock(paths, () => {
|
|
610
|
+
const mine = readRecord(waiterFile(paths, pid));
|
|
611
|
+
if (mine === null || mine.cancel)
|
|
612
|
+
return {
|
|
613
|
+
admitted: false,
|
|
614
|
+
verdict: "cancelled",
|
|
615
|
+
reason: mine?.cancel || "removed"
|
|
616
|
+
};
|
|
617
|
+
fields = { ...fields, lane: mine.lane };
|
|
618
|
+
if (mine.promote) {
|
|
619
|
+
flock(fd, ["-s", "-n"]);
|
|
620
|
+
writeRecord(runningFile(paths, pid), pid, {
|
|
621
|
+
...fields,
|
|
622
|
+
bypass: mine.promote
|
|
623
|
+
});
|
|
624
|
+
writeRecord(paths.holder, pid, fields);
|
|
625
|
+
rmSync(waiterFile(paths, pid), { force: true });
|
|
626
|
+
logEvent(
|
|
627
|
+
paths,
|
|
628
|
+
pid,
|
|
629
|
+
"bypass",
|
|
630
|
+
`reason=${mine.promote} cwd=${fields.cwd} label=${fields.label} lane=${fields.lane} weight=${fields.weight} id=${fields.id}`
|
|
631
|
+
);
|
|
632
|
+
return { admitted: true, verdict: "promoted", lane: fields.lane };
|
|
633
|
+
}
|
|
634
|
+
const { running, waiters } = readQueue(paths);
|
|
635
|
+
if (!admissible(options.settings, running, waiters).has(pid))
|
|
636
|
+
return { admitted: false, verdict: "queue" };
|
|
637
|
+
const grace = options.settings.profiles[options.profile].loadGrace;
|
|
638
|
+
const reading = readHost(hostSources(options.env));
|
|
639
|
+
const released = lastRelease(paths);
|
|
640
|
+
const verdict = hostVerdict(options.settings, reading, {
|
|
641
|
+
lane: fields.lane,
|
|
642
|
+
weight: fields.weight,
|
|
643
|
+
grace,
|
|
644
|
+
waited: epoch() - registered,
|
|
645
|
+
// Only a release while this job waited counts: older ones explain nothing now.
|
|
646
|
+
sinceRelease: released !== null && released >= registered ? epoch() - released : null,
|
|
647
|
+
runningWeight: runningWeight(running)
|
|
648
|
+
});
|
|
649
|
+
if (verdict === "wait" || verdict === "severe")
|
|
650
|
+
return { admitted: false, verdict };
|
|
651
|
+
if (flock(fd, ["-s", "-n"]) !== 0)
|
|
652
|
+
return { admitted: false, verdict: "legacy" };
|
|
653
|
+
writeRecord(runningFile(paths, pid), pid, {
|
|
654
|
+
...fields,
|
|
655
|
+
queued: Date.now(),
|
|
656
|
+
grace: verdict === "grace"
|
|
657
|
+
});
|
|
658
|
+
writeRecord(paths.holder, pid, fields);
|
|
659
|
+
rmSync(waiterFile(paths, pid), { force: true });
|
|
660
|
+
if (verdict !== "settled")
|
|
661
|
+
logEvent(
|
|
662
|
+
paths,
|
|
663
|
+
pid,
|
|
664
|
+
verdict,
|
|
665
|
+
`waited=${epoch() - registered} ${hostNow(reading)} label=${fields.label}`
|
|
666
|
+
);
|
|
667
|
+
return { admitted: true, verdict, lane: fields.lane };
|
|
668
|
+
});
|
|
669
|
+
return outcome ?? { admitted: false, verdict: "queue" };
|
|
670
|
+
}
|
|
671
|
+
function lastRelease(paths) {
|
|
672
|
+
try {
|
|
673
|
+
return Number(readFileSync4(paths.released, "utf8").trim()) || null;
|
|
674
|
+
} catch {
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
function markRelease(paths) {
|
|
679
|
+
try {
|
|
680
|
+
writeFileSync2(paths.released, `${epoch()}
|
|
681
|
+
`);
|
|
682
|
+
} catch {
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
function release(paths, pid, fd, rc, ran) {
|
|
686
|
+
underAdmitLock(paths, () => {
|
|
687
|
+
rmSync(runningFile(paths, pid), { force: true });
|
|
688
|
+
removeRecord(paths.holder, pid);
|
|
689
|
+
markRelease(paths);
|
|
690
|
+
const next = liveRecords(paths.running)[0];
|
|
691
|
+
if (next && readRecord(paths.holder) === null)
|
|
692
|
+
writeRecord(paths.holder, next.pid, { ...next, queued: next.queued });
|
|
693
|
+
});
|
|
694
|
+
logEvent(paths, pid, "released", `ran=${ran} rc=${rc}`);
|
|
695
|
+
closeSync2(fd);
|
|
696
|
+
}
|
|
697
|
+
function held(options, paths, fd, start) {
|
|
698
|
+
const { settings } = options;
|
|
699
|
+
const pid = String(process.pid);
|
|
700
|
+
const env = { [settings.markerEnv]: pid };
|
|
701
|
+
if (settings.legacyMarkerEnv) env[settings.legacyMarkerEnv] = pid;
|
|
702
|
+
if (settings.legacySkipEnv) env[settings.legacySkipEnv] = NESTED;
|
|
703
|
+
let open = true;
|
|
704
|
+
return {
|
|
705
|
+
kind: "held",
|
|
706
|
+
env,
|
|
707
|
+
start,
|
|
708
|
+
release(rc, ran) {
|
|
709
|
+
if (!open) return;
|
|
710
|
+
open = false;
|
|
711
|
+
release(paths, process.pid, fd, rc, ran);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function acquireLease(options) {
|
|
716
|
+
const { settings, env, label, say } = options;
|
|
717
|
+
if (!settings.enabled) return { kind: "disabled" };
|
|
718
|
+
const paths = leasePaths(settings.dir);
|
|
719
|
+
const skip = skipSource(settings, env);
|
|
720
|
+
const invalid = skip === null ? null : skipRefusal(settings, skip.reason, {
|
|
721
|
+
nested: () => reentrant(settings, env),
|
|
722
|
+
operator: () => options.operator?.() ?? false
|
|
723
|
+
});
|
|
724
|
+
if (skip !== null && invalid !== null) {
|
|
725
|
+
if (reentrant(settings, env)) return { kind: "reentrant" };
|
|
726
|
+
say(
|
|
727
|
+
`${invalid} (${skip.variable}); allowed: ${allowedReasons(settings).join(", ")}. List a reason in [host.lease] skip_reasons, or run without ${skip.variable} and wait for the lease.`
|
|
728
|
+
);
|
|
729
|
+
logEvent(
|
|
730
|
+
paths,
|
|
731
|
+
process.pid,
|
|
732
|
+
"skip-invalid",
|
|
733
|
+
`reason=${skip.reason} env=${skip.variable} cwd=${options.cwd} label=${label}`
|
|
734
|
+
);
|
|
735
|
+
return { kind: "invalid", reason: skip.reason, variable: skip.variable };
|
|
736
|
+
}
|
|
737
|
+
const refused = skip !== null && options.lane === "bulk" && !settings.skipAlways.includes(skip.reason);
|
|
738
|
+
if (refused && reentrant(settings, env)) return { kind: "reentrant" };
|
|
739
|
+
if (skip !== null && refused) {
|
|
740
|
+
say(
|
|
741
|
+
`the lease skip (${skip.reason}) is not honoured for a bulk job (--lane bulk, or a [[host.lease.commands]] entry with lane = "bulk"); it queues in the bulk lane. For an emergency run: pmcp heavy --now --reason <text> -- <command>`
|
|
742
|
+
);
|
|
743
|
+
logEvent(
|
|
744
|
+
paths,
|
|
745
|
+
process.pid,
|
|
746
|
+
"skip-refused",
|
|
747
|
+
`reason=${skip.reason} env=${skip.variable} cwd=${options.cwd} label=${label}`
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
if (skip !== null && !refused) {
|
|
751
|
+
say(`running without the host lease: ${skip.reason}`);
|
|
752
|
+
logEvent(
|
|
753
|
+
paths,
|
|
754
|
+
process.pid,
|
|
755
|
+
"skip",
|
|
756
|
+
`reason=${skip.reason} env=${skip.variable} cwd=${options.cwd} label=${label}`
|
|
757
|
+
);
|
|
758
|
+
return { kind: "skipped", ...skip };
|
|
759
|
+
}
|
|
760
|
+
if (!flockAvailable()) {
|
|
761
|
+
say("flock is not installed; running without the host lease");
|
|
762
|
+
return { kind: "skipped", reason: "flock is not installed" };
|
|
763
|
+
}
|
|
764
|
+
if (reentrant(settings, env)) return { kind: "reentrant" };
|
|
765
|
+
const profile = settings.profiles[options.profile];
|
|
766
|
+
if (profile === void 0)
|
|
767
|
+
throw new Error(`unknown profile ${options.profile}`);
|
|
768
|
+
const lane = options.lane ?? profileLane(options.profile);
|
|
769
|
+
const weight = Math.min(
|
|
770
|
+
options.weight ?? settings.weights[lane],
|
|
771
|
+
settings.budget
|
|
772
|
+
);
|
|
773
|
+
const session = sessionOf(env, options.cwd);
|
|
774
|
+
const fields = {
|
|
775
|
+
cwd: options.cwd,
|
|
776
|
+
label,
|
|
777
|
+
lane,
|
|
778
|
+
weight,
|
|
779
|
+
session,
|
|
780
|
+
id: options.id ?? newId()
|
|
781
|
+
};
|
|
782
|
+
mkdirSync(paths.waiters, { recursive: true });
|
|
783
|
+
mkdirSync(paths.running, { recursive: true });
|
|
784
|
+
if (options.now) return bypass(options, paths, fields);
|
|
785
|
+
const started = epoch();
|
|
786
|
+
const deadline = started + profile.maxWait;
|
|
787
|
+
const parked = parkForSession({
|
|
788
|
+
paths,
|
|
789
|
+
lane,
|
|
790
|
+
session,
|
|
791
|
+
cap: settings.perSession,
|
|
792
|
+
maxWait: profile.maxWait,
|
|
793
|
+
say
|
|
794
|
+
});
|
|
795
|
+
if (parked === null) return busy(options, paths, lane, 0, "parked", started);
|
|
796
|
+
const fd = openSync2(paths.lock, "a");
|
|
797
|
+
writeRecord(waiterFile(paths, process.pid), process.pid, fields);
|
|
798
|
+
const registered = epoch();
|
|
799
|
+
let beat = 0;
|
|
800
|
+
let last = "queue";
|
|
801
|
+
let admittedLane = lane;
|
|
802
|
+
try {
|
|
803
|
+
for (; ; ) {
|
|
804
|
+
const attempt = tryAdmit(options, paths, fd, fields, registered);
|
|
805
|
+
if (attempt.admitted) {
|
|
806
|
+
last = attempt.verdict;
|
|
807
|
+
admittedLane = attempt.lane ?? lane;
|
|
808
|
+
break;
|
|
809
|
+
}
|
|
810
|
+
if (attempt.verdict === "cancelled") {
|
|
811
|
+
closeSync2(fd);
|
|
812
|
+
say(`cancelled in the queue: ${attempt.reason}`);
|
|
813
|
+
return { kind: "cancelled", reason: attempt.reason ?? "" };
|
|
814
|
+
}
|
|
815
|
+
if (last === "queue" && attempt.verdict !== "queue")
|
|
816
|
+
say(
|
|
817
|
+
`next in line; ${describeWait(attempt.verdict, hostNow(readHost(hostSources(env))))}`
|
|
818
|
+
);
|
|
819
|
+
last = attempt.verdict;
|
|
820
|
+
if (epoch() >= deadline) {
|
|
821
|
+
closeSync2(fd);
|
|
822
|
+
const place = position(paths, process.pid);
|
|
823
|
+
const reason = last === "severe" ? "host" : last === "legacy" ? "legacy" : "queue";
|
|
824
|
+
return busy(options, paths, lane, place, reason, started);
|
|
825
|
+
}
|
|
826
|
+
if (epoch() - beat >= settings.heartbeat) {
|
|
827
|
+
const { running } = readQueue(paths);
|
|
828
|
+
say(
|
|
829
|
+
`waiting ${epoch() - started}s; ${lane} position ${position(paths, process.pid)}, running weight ${runningWeight(running)}/${settings.budget}, ${hostNow(readHost(hostSources(env)))}`
|
|
830
|
+
);
|
|
831
|
+
beat = epoch();
|
|
832
|
+
}
|
|
833
|
+
sleepSeconds(Math.min(STEP_SECONDS, deadline - epoch()));
|
|
834
|
+
}
|
|
835
|
+
} finally {
|
|
836
|
+
rmSync(waiterFile(paths, process.pid), { force: true });
|
|
837
|
+
}
|
|
838
|
+
hostSettled(settings, paths, hostSources(env));
|
|
839
|
+
logEvent(
|
|
840
|
+
paths,
|
|
841
|
+
process.pid,
|
|
842
|
+
"acquired",
|
|
843
|
+
`waited=${epoch() - started} cwd=${options.cwd} label=${label} lane=${admittedLane} weight=${weight}`
|
|
844
|
+
);
|
|
845
|
+
const start = last === "promoted" ? "bypass" : last === "grace" ? "grace" : "queue";
|
|
846
|
+
return held(options, paths, fd, start);
|
|
847
|
+
}
|
|
848
|
+
function bypass(options, paths, fields) {
|
|
849
|
+
const fd = openSync2(paths.lock, "a");
|
|
850
|
+
const reason = options.now;
|
|
851
|
+
underAdmitLock(paths, () => {
|
|
852
|
+
flock(fd, ["-s", "-n"]);
|
|
853
|
+
writeRecord(runningFile(paths, process.pid), process.pid, {
|
|
854
|
+
...fields,
|
|
855
|
+
bypass: reason
|
|
856
|
+
});
|
|
857
|
+
});
|
|
858
|
+
options.say(
|
|
859
|
+
`BYPASS: running now, outside the queue and the host check: ${reason}`
|
|
860
|
+
);
|
|
861
|
+
logEvent(
|
|
862
|
+
paths,
|
|
863
|
+
process.pid,
|
|
864
|
+
"bypass",
|
|
865
|
+
`reason=${reason} cwd=${fields.cwd} label=${fields.label} lane=${fields.lane} weight=${fields.weight}`
|
|
866
|
+
);
|
|
867
|
+
return held(options, paths, fd, "bypass");
|
|
868
|
+
}
|
|
869
|
+
function describeWait(verdict, host) {
|
|
870
|
+
if (verdict === "severe")
|
|
871
|
+
return `the host is at hard_factor x a limit (${host})`;
|
|
872
|
+
if (verdict === "wait") return `waiting for the host to settle (${host})`;
|
|
873
|
+
if (verdict === "legacy") return "a shell wrapper holds the whole lease";
|
|
874
|
+
return "waiting";
|
|
875
|
+
}
|
|
876
|
+
function busy(options, paths, lane, place, reason, started) {
|
|
877
|
+
const profile = options.settings.profiles[options.profile];
|
|
878
|
+
const host = hostNow(readHost(hostSources(options.env)));
|
|
879
|
+
logEvent(
|
|
880
|
+
paths,
|
|
881
|
+
process.pid,
|
|
882
|
+
"timeout",
|
|
883
|
+
`waited=${profile.maxWait} label=${options.label} lane=${lane} position=${place} reason=${reason}`
|
|
884
|
+
);
|
|
885
|
+
return {
|
|
886
|
+
kind: "busy",
|
|
887
|
+
waited: epoch() - started,
|
|
888
|
+
lane,
|
|
889
|
+
position: place,
|
|
890
|
+
reason,
|
|
891
|
+
host
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// src/lease/run.ts
|
|
896
|
+
var DRAIN_MS = 2e3;
|
|
897
|
+
var INVALID_EXIT = 2;
|
|
898
|
+
function prefixed(argv, prefix) {
|
|
899
|
+
const name = (word = "") => word.slice(word.lastIndexOf("/") + 1);
|
|
900
|
+
return argv.length >= prefix.length && prefix.every((word, at) => (at === 0 ? name(argv[0]) : argv[at]) === word);
|
|
901
|
+
}
|
|
902
|
+
function laneFor(run) {
|
|
903
|
+
const command = run.settings.commands.find(
|
|
904
|
+
(entry) => prefixed(run.argv, entry.argv)
|
|
905
|
+
);
|
|
906
|
+
const fromProfile = run.profile === "gate" || run.profile === "hook";
|
|
907
|
+
const lane = run.lane ?? (fromProfile ? profileLane(run.profile) : command?.lane ?? "interactive");
|
|
908
|
+
const tiny = run.settings.tiny.some((prefix) => prefixed(run.argv, prefix));
|
|
909
|
+
const weight = run.weight ?? command?.weight ?? (tiny ? 0 : run.settings.weights[lane]);
|
|
910
|
+
return { lane, weight };
|
|
911
|
+
}
|
|
912
|
+
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
913
|
+
var HEAD = 2048;
|
|
914
|
+
function execute(argv, run, extra, watch) {
|
|
915
|
+
const [bin, ...rest] = argv;
|
|
916
|
+
const kept = run.output;
|
|
917
|
+
const echo = run.echo !== false;
|
|
918
|
+
return new Promise((resolve) => {
|
|
919
|
+
const child = spawn(bin ?? "true", rest, {
|
|
920
|
+
cwd: run.cwd,
|
|
921
|
+
env: { ...run.env, ...extra },
|
|
922
|
+
stdio: [
|
|
923
|
+
"inherit",
|
|
924
|
+
kept ? "pipe" : "inherit",
|
|
925
|
+
kept || watch.stderrHead ? "pipe" : "inherit"
|
|
926
|
+
]
|
|
927
|
+
});
|
|
928
|
+
let head = "";
|
|
929
|
+
child.stdout?.on("data", (chunk) => {
|
|
930
|
+
if (echo) process.stdout.write(chunk);
|
|
931
|
+
kept?.write(chunk);
|
|
932
|
+
});
|
|
933
|
+
child.stderr?.on("data", (chunk) => {
|
|
934
|
+
if (echo) process.stderr.write(chunk);
|
|
935
|
+
kept?.write(chunk);
|
|
936
|
+
if (head.length >= HEAD) return;
|
|
937
|
+
head += chunk.toString("utf8");
|
|
938
|
+
watch.stderrHead?.(head.slice(0, HEAD));
|
|
939
|
+
});
|
|
940
|
+
void watch.started?.();
|
|
941
|
+
const forward = (signal) => child.kill(signal);
|
|
942
|
+
for (const signal of SIGNALS) process.on(signal, forward);
|
|
943
|
+
const release2 = holdOnClosedPipe();
|
|
944
|
+
const timer = setInterval(() => watch.tick(child.pid), 1e3);
|
|
945
|
+
let finished = false;
|
|
946
|
+
const done = (code) => {
|
|
947
|
+
if (finished) return;
|
|
948
|
+
finished = true;
|
|
949
|
+
release2();
|
|
950
|
+
clearInterval(timer);
|
|
951
|
+
for (const signal of SIGNALS) process.off(signal, forward);
|
|
952
|
+
resolve(code);
|
|
953
|
+
};
|
|
954
|
+
child.on("error", (error) => {
|
|
955
|
+
run.say(`${bin}: ${error.message}`);
|
|
956
|
+
done(127);
|
|
957
|
+
});
|
|
958
|
+
child.on("exit", (code, signal) => {
|
|
959
|
+
const exit = code ?? 128 + (signal && constants.signals[signal] || 15);
|
|
960
|
+
if (!child.stdout && !child.stderr) return done(exit);
|
|
961
|
+
const drained = setTimeout(() => done(exit), DRAIN_MS);
|
|
962
|
+
child.on("close", () => {
|
|
963
|
+
clearTimeout(drained);
|
|
964
|
+
done(exit);
|
|
965
|
+
});
|
|
966
|
+
});
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
function holdWarner(run, since) {
|
|
970
|
+
let warned = false;
|
|
971
|
+
return () => {
|
|
972
|
+
const held2 = epoch() - since;
|
|
973
|
+
if (warned || held2 < run.settings.holdWarn) return;
|
|
974
|
+
warned = true;
|
|
975
|
+
run.say(
|
|
976
|
+
`holding the host lease for ${held2}s (hold_warn ${run.settings.holdWarn}s); others may be waiting`
|
|
977
|
+
);
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
var BECAUSE = {
|
|
981
|
+
queue: "the jobs ahead kept the budget",
|
|
982
|
+
parked: "this session already had a job queued in the lane (per_session)",
|
|
983
|
+
host: "the host stayed at hard_factor x a limit or more",
|
|
984
|
+
legacy: "a shell wrapper kept the whole lease"
|
|
985
|
+
};
|
|
986
|
+
function busyReason(busy2) {
|
|
987
|
+
return `gave up after ${busy2.waited}s in the ${busy2.lane} lane (position ${busy2.position}): ${BECAUSE[busy2.reason]}; ${busy2.host}`;
|
|
988
|
+
}
|
|
989
|
+
function record(run, fields) {
|
|
990
|
+
const usage = {
|
|
991
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
992
|
+
signature: signature(run.argv),
|
|
993
|
+
dir: packageDir(run.cwd, run.root ?? run.cwd),
|
|
994
|
+
...fields
|
|
995
|
+
};
|
|
996
|
+
appendUsage(leasePaths(run.settings.dir).usage, usage, run.settings.usageCap);
|
|
997
|
+
run.onUsage?.(usage);
|
|
998
|
+
}
|
|
999
|
+
function skipFields(run, outcome) {
|
|
1000
|
+
if (outcome.kind === "invalid")
|
|
1001
|
+
return { skip_invalid: outcome.reason, skip_env: outcome.variable };
|
|
1002
|
+
if (outcome.kind === "skipped")
|
|
1003
|
+
return {
|
|
1004
|
+
skip: outcome.reason,
|
|
1005
|
+
...outcome.variable ? { skip_env: outcome.variable } : {}
|
|
1006
|
+
};
|
|
1007
|
+
if (outcome.kind === "disabled" || outcome.kind === "reentrant") return {};
|
|
1008
|
+
const refused = skipSource(run.settings, run.env);
|
|
1009
|
+
return refused ? { skip_refused: refused.reason, skip_env: refused.variable } : {};
|
|
1010
|
+
}
|
|
1011
|
+
var CANCELLED_EXIT = 130;
|
|
1012
|
+
var NOTHING = {
|
|
1013
|
+
wall_s: 0,
|
|
1014
|
+
user_s: 0,
|
|
1015
|
+
sys_s: 0,
|
|
1016
|
+
max_rss_kb: 0,
|
|
1017
|
+
read_bytes: 0,
|
|
1018
|
+
write_bytes: 0,
|
|
1019
|
+
source: "none"
|
|
1020
|
+
};
|
|
1021
|
+
async function runHeavy(run) {
|
|
1022
|
+
const label = oneLine(run.label || run.argv.join(" "));
|
|
1023
|
+
const { lane, weight } = laneFor(run);
|
|
1024
|
+
let outcome;
|
|
1025
|
+
if (!run.settings.enabled) {
|
|
1026
|
+
run.say("lease disabled ([host.lease] enabled = false); running now");
|
|
1027
|
+
outcome = { kind: "disabled" };
|
|
1028
|
+
} else {
|
|
1029
|
+
outcome = acquireLease({ ...run, label, lane, weight });
|
|
1030
|
+
}
|
|
1031
|
+
if (outcome.kind === "invalid") {
|
|
1032
|
+
record(run, {
|
|
1033
|
+
...NOTHING,
|
|
1034
|
+
lane,
|
|
1035
|
+
weight,
|
|
1036
|
+
start: "none",
|
|
1037
|
+
outcome: "invalid",
|
|
1038
|
+
exit: INVALID_EXIT,
|
|
1039
|
+
...skipFields(run, outcome)
|
|
1040
|
+
});
|
|
1041
|
+
return INVALID_EXIT;
|
|
1042
|
+
}
|
|
1043
|
+
if (outcome.kind === "cancelled") {
|
|
1044
|
+
record(run, {
|
|
1045
|
+
...NOTHING,
|
|
1046
|
+
lane,
|
|
1047
|
+
weight,
|
|
1048
|
+
start: "none",
|
|
1049
|
+
outcome: "cancelled",
|
|
1050
|
+
exit: CANCELLED_EXIT,
|
|
1051
|
+
...skipFields(run, outcome)
|
|
1052
|
+
});
|
|
1053
|
+
return CANCELLED_EXIT;
|
|
1054
|
+
}
|
|
1055
|
+
if (outcome.kind === "busy") {
|
|
1056
|
+
run.say(`not run: host busy; ${busyReason(outcome)}`);
|
|
1057
|
+
if (outcome.lane === "bulk" && outcome.reason === "queue")
|
|
1058
|
+
run.say(
|
|
1059
|
+
"a bulk job waits behind every gate, hook and interactive job; this one reached its max_wait"
|
|
1060
|
+
);
|
|
1061
|
+
run.say(
|
|
1062
|
+
`run it again later, or set ${DISABLE_ENV}=<reason> to run beside it`
|
|
1063
|
+
);
|
|
1064
|
+
record(run, {
|
|
1065
|
+
...NOTHING,
|
|
1066
|
+
lane,
|
|
1067
|
+
weight,
|
|
1068
|
+
start: "none",
|
|
1069
|
+
outcome: "timeout",
|
|
1070
|
+
exit: BUSY_EXIT,
|
|
1071
|
+
...skipFields(run, outcome)
|
|
1072
|
+
});
|
|
1073
|
+
return BUSY_EXIT;
|
|
1074
|
+
}
|
|
1075
|
+
const memoryMax = run.settings.profiles[run.profile]?.memoryMax;
|
|
1076
|
+
const held2 = outcome.kind === "held" ? outcome : null;
|
|
1077
|
+
const sampler = new Sampler();
|
|
1078
|
+
const { argv, started, unscoped } = run.placed ? { argv: run.argv, started: void 0, unscoped: void 0 } : scoped(
|
|
1079
|
+
run.argv,
|
|
1080
|
+
run.settings,
|
|
1081
|
+
held2 ? lane : null,
|
|
1082
|
+
memoryMax,
|
|
1083
|
+
run.say,
|
|
1084
|
+
(dir) => sampler.useCgroup(dir),
|
|
1085
|
+
void 0,
|
|
1086
|
+
void 0,
|
|
1087
|
+
run.env
|
|
1088
|
+
);
|
|
1089
|
+
const warn = held2 ? holdWarner(run, epoch()) : () => {
|
|
1090
|
+
};
|
|
1091
|
+
const tick = (pid) => {
|
|
1092
|
+
warn();
|
|
1093
|
+
sampler.sample(pid);
|
|
1094
|
+
};
|
|
1095
|
+
const begun = Date.now();
|
|
1096
|
+
let code = 1;
|
|
1097
|
+
let head = "";
|
|
1098
|
+
try {
|
|
1099
|
+
code = await execute(argv, run, held2?.env ?? {}, {
|
|
1100
|
+
tick,
|
|
1101
|
+
...started ? { started } : {},
|
|
1102
|
+
...unscoped ? { stderrHead: (text) => head = text } : {}
|
|
1103
|
+
});
|
|
1104
|
+
if (code !== 0 && unscoped && BUS_FAILURE.test(head.split("\n")[0] ?? "")) {
|
|
1105
|
+
run.say(
|
|
1106
|
+
"systemd-run could not reach the user manager; running the command again without the scope"
|
|
1107
|
+
);
|
|
1108
|
+
code = await execute(unscoped, run, held2?.env ?? {}, { tick });
|
|
1109
|
+
}
|
|
1110
|
+
} finally {
|
|
1111
|
+
const wall = Math.round((Date.now() - begun) / 100) / 10;
|
|
1112
|
+
held2?.release(code, Math.round(wall));
|
|
1113
|
+
if (outcome.kind !== "disabled")
|
|
1114
|
+
record(run, {
|
|
1115
|
+
...sampler.finish(),
|
|
1116
|
+
wall_s: wall,
|
|
1117
|
+
lane,
|
|
1118
|
+
weight,
|
|
1119
|
+
start: held2 ? held2.start : "none",
|
|
1120
|
+
outcome: "ran",
|
|
1121
|
+
exit: code,
|
|
1122
|
+
...skipFields(run, outcome)
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
return code;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
export {
|
|
1129
|
+
hostSources,
|
|
1130
|
+
readHost,
|
|
1131
|
+
hostSettled,
|
|
1132
|
+
sleepSeconds,
|
|
1133
|
+
sessionOf,
|
|
1134
|
+
userManager,
|
|
1135
|
+
runningWeight,
|
|
1136
|
+
laneUse,
|
|
1137
|
+
underAdmitLock,
|
|
1138
|
+
readQueue,
|
|
1139
|
+
settled,
|
|
1140
|
+
skipSource,
|
|
1141
|
+
skipReason,
|
|
1142
|
+
readUsage,
|
|
1143
|
+
skipShare,
|
|
1144
|
+
skipReasons,
|
|
1145
|
+
usageStats,
|
|
1146
|
+
runHeavy
|
|
1147
|
+
};
|
|
1148
|
+
//# sourceMappingURL=chunk-LXJTABBQ.js.map
|