@modootoday/pmcp 0.26.0 → 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-HJUCYQDQ.js → apply-YNBY2GPK.js} +7 -7
- package/dist/{chunk-FXDGW2IH.js → chunk-5TT7YAEP.js} +3 -3
- package/dist/chunk-674APJDK.js +128 -0
- package/dist/chunk-6TM7PLXG.js +154 -0
- package/dist/{chunk-2JUHFM4V.js → chunk-6U6MLIS2.js} +2 -2
- package/dist/{chunk-KQV2EPAP.js → chunk-ENFF7ZBC.js} +2 -2
- package/dist/{chunk-GIL3RBRX.js → chunk-GVKVMSWX.js} +25 -23
- package/dist/{chunk-LCPCRZ7N.js → chunk-JTKGDT2K.js} +2 -2
- package/dist/{chunk-UVPIVSZB.js → chunk-L5EJK24I.js} +2 -2
- package/dist/{chunk-3JOFDNL4.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-Q5BQIRM3.js → chunk-M5GNZSOJ.js} +2 -2
- package/dist/chunk-MXVP3ICV.js +7 -0
- package/dist/{chunk-3KISMGVP.js → chunk-OY7XYKVL.js} +2 -2
- package/dist/{chunk-C7Y2MBCN.js → chunk-OZTEMCCM.js} +3 -3
- package/dist/{chunk-6XE3WW7S.js → chunk-PH3P5WEY.js} +3 -3
- package/dist/{chunk-NQZMGQFG.js → chunk-Q3XLIRIJ.js} +4 -4
- package/dist/{chunk-OG2ESRQP.js → chunk-TVA2IN6H.js} +5 -5
- package/dist/chunk-TYXJBXZQ.js +441 -0
- package/dist/chunk-WG7KH46S.js +71 -0
- package/dist/{chunk-2GL5L2G5.js → chunk-X2WDR3UK.js} +2 -2
- package/dist/{chunk-OBCBCLS2.js → chunk-X3LNF5KJ.js} +5 -5
- package/dist/chunk-X6W7JIED.js +25 -0
- package/dist/{chunk-7FHOSWNQ.js → chunk-XCIFD7G5.js} +2 -2
- package/dist/{chunk-Y7FYCVUK.js → chunk-Y5WIIEG2.js} +2 -2
- package/dist/{chunk-7PBWLYCO.js → chunk-ZO5CRS2T.js} +2 -2
- package/dist/cli.js +13 -19
- package/dist/{completion-ZKWGPMEK.js → completion-WKIMMLNF.js} +6 -6
- package/dist/{diagnostics-IM2OPAPL.js → diagnostics-LCNF5NRK.js} +5 -5
- package/dist/doctor-NKFZNSRD.js +106 -0
- package/dist/{doctor-VZTHEPVC.js → doctor-PSMPNQXS.js} +9 -9
- package/dist/{entry-Q4EI4PNO.js → entry-LMAWTZM2.js} +11 -11
- package/dist/{exec-mcp-AWGUQVT2.js → exec-mcp-VAP46MKO.js} +5 -5
- package/dist/{harness-L4C5XWKR.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-2E6PJJQK.js → hooks-AWAO7CQO.js} +3 -3
- package/dist/{host-RJJGTKWA.js → host-4GFQQJUS.js} +7 -7
- package/dist/{host-OQDBQJB2.js → host-I4XIJTXR.js} +6 -6
- package/dist/host-OBTC2PGQ.js +32 -0
- package/dist/{host-run-PWCIXJDN.js → host-run-T3FC7L6I.js} +4 -4
- package/dist/http.js +1 -1
- package/dist/{import-mcp-2H2NRUCL.js → import-mcp-5JSGA5IS.js} +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -8
- package/dist/{init-O2UCU2WT.js → init-PZYWBDZS.js} +2 -2
- package/dist/{invoke-SJ3JHRJU.js → invoke-DCKJCMRB.js} +7 -7
- package/dist/jobs-FLHB3WER.js +26 -0
- package/dist/{list-SJVXZGKK.js → list-ZEU7T3XK.js} +6 -6
- package/dist/{logout-ROKXWR3T.js → logout-CUSOVMKL.js} +5 -5
- package/dist/{mcp-U7MVFAB4.js → mcp-ZHOPOFXI.js} +2 -2
- package/dist/measure-AUBPYT6L.js +129 -0
- package/dist/plan-46BTJQIV.js +98 -0
- package/dist/{project-MGUYHUF7.js → project-T36TRAWT.js} +4 -4
- package/dist/{run-TBO5JO4Y.js → run-37PVE74I.js} +4 -4
- package/dist/{run-DDWLS6FY.js → run-LMSHQJPQ.js} +4 -4
- package/dist/{serve-RBQYB3FP.js → serve-E4FD6PAL.js} +3 -3
- package/dist/{start-login-SOTNDUP6.js → start-login-JSKFXSLY.js} +5 -5
- package/dist/{status-TSTOFNZU.js → status-ZNWF4362.js} +5 -5
- package/dist/{status-EBIUQIGY.js → status-ZSK3PNM2.js} +6 -6
- package/dist/stdio.js +2 -2
- package/dist/{test-4PZHHWOQ.js → test-OCTC2DSZ.js} +5 -5
- package/dist/{tui-QD2LMXHV.js → tui-LE2KPUMI.js} +4 -4
- package/dist/{worker-SPJ4HFWV.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-J43Z4O3G.js +0 -7
- package/dist/heavy-OG3UVZNY.js +0 -2206
|
@@ -0,0 +1,1063 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hostSettled,
|
|
3
|
+
hostSources,
|
|
4
|
+
laneUse,
|
|
5
|
+
readHost,
|
|
6
|
+
readQueue,
|
|
7
|
+
readUsage,
|
|
8
|
+
runHeavy,
|
|
9
|
+
runningWeight,
|
|
10
|
+
sessionOf,
|
|
11
|
+
settled,
|
|
12
|
+
skipReason,
|
|
13
|
+
skipReasons,
|
|
14
|
+
skipShare,
|
|
15
|
+
skipSource,
|
|
16
|
+
sleepSeconds,
|
|
17
|
+
underAdmitLock,
|
|
18
|
+
usageStats
|
|
19
|
+
} from "./chunk-LXJTABBQ.js";
|
|
20
|
+
import "./chunk-WG7KH46S.js";
|
|
21
|
+
import "./chunk-SGFTHKKS.js";
|
|
22
|
+
import "./chunk-65BW7Y6O.js";
|
|
23
|
+
import {
|
|
24
|
+
isOperator,
|
|
25
|
+
requireOperatorFor
|
|
26
|
+
} from "./chunk-OIKT4SYO.js";
|
|
27
|
+
import "./chunk-DZ2MRTYT.js";
|
|
28
|
+
import "./chunk-NQCAKDRY.js";
|
|
29
|
+
import "./chunk-Z6WRFOFT.js";
|
|
30
|
+
import {
|
|
31
|
+
heavySpec
|
|
32
|
+
} from "./chunk-LXWAU7AX.js";
|
|
33
|
+
import "./chunk-X6W7JIED.js";
|
|
34
|
+
import "./chunk-JPYQDOEW.js";
|
|
35
|
+
import {
|
|
36
|
+
ArgumentError,
|
|
37
|
+
EXIT,
|
|
38
|
+
one
|
|
39
|
+
} from "./chunk-Y6NFETNG.js";
|
|
40
|
+
import {
|
|
41
|
+
BUSY_EXIT,
|
|
42
|
+
DEFAULT_JOB_LOG_BYTES,
|
|
43
|
+
JobLog,
|
|
44
|
+
NOW_ENV,
|
|
45
|
+
leaseSettingsFor,
|
|
46
|
+
readJobLog
|
|
47
|
+
} from "./chunk-PZ6D3WKD.js";
|
|
48
|
+
import {
|
|
49
|
+
LANES,
|
|
50
|
+
describeRecord,
|
|
51
|
+
epoch,
|
|
52
|
+
leaseHeld,
|
|
53
|
+
leasePaths,
|
|
54
|
+
liveRecords,
|
|
55
|
+
logEvent,
|
|
56
|
+
pidAlive,
|
|
57
|
+
readRecord,
|
|
58
|
+
writeRecord
|
|
59
|
+
} from "./chunk-N7ANFMA5.js";
|
|
60
|
+
import "./chunk-T6C3TSTO.js";
|
|
61
|
+
import "./chunk-GUHOEIHP.js";
|
|
62
|
+
import "./chunk-IJBSOWHK.js";
|
|
63
|
+
import {
|
|
64
|
+
ConfigError,
|
|
65
|
+
findConfig
|
|
66
|
+
} from "./chunk-GJXOFDJT.js";
|
|
67
|
+
import "./chunk-PMJG2CXF.js";
|
|
68
|
+
|
|
69
|
+
// src/commands/heavy.ts
|
|
70
|
+
import { dirname } from "path";
|
|
71
|
+
|
|
72
|
+
// src/lease/status.ts
|
|
73
|
+
import { mkdirSync, readFileSync as readFileSync2 } from "fs";
|
|
74
|
+
|
|
75
|
+
// src/lease/estimate.ts
|
|
76
|
+
import { closeSync, fstatSync, openSync, readSync } from "fs";
|
|
77
|
+
var TAIL_BYTES = 512 * 1024;
|
|
78
|
+
function tail(file) {
|
|
79
|
+
let fd;
|
|
80
|
+
try {
|
|
81
|
+
fd = openSync(file, "r");
|
|
82
|
+
} catch {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const size = fstatSync(fd).size;
|
|
87
|
+
const length = Math.min(size, TAIL_BYTES);
|
|
88
|
+
const buffer = Buffer.alloc(length);
|
|
89
|
+
readSync(fd, buffer, 0, length, size - length);
|
|
90
|
+
return buffer.toString("utf8");
|
|
91
|
+
} finally {
|
|
92
|
+
closeSync(fd);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function median(values) {
|
|
96
|
+
if (values.length === 0) return void 0;
|
|
97
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
98
|
+
return sorted[Math.floor(sorted.length / 2)];
|
|
99
|
+
}
|
|
100
|
+
function logLines(log) {
|
|
101
|
+
return tail(log).split("\n").map((line) => line.split(" ")).filter((fields) => fields.length >= 4);
|
|
102
|
+
}
|
|
103
|
+
function runMedians(lines) {
|
|
104
|
+
const open = /* @__PURE__ */ new Map();
|
|
105
|
+
const runs = { all: [] };
|
|
106
|
+
for (const [, pid = "", event, detail = ""] of lines) {
|
|
107
|
+
if (event === "acquired") {
|
|
108
|
+
const named = detail.match(/ lane=(\w+)/u)?.[1];
|
|
109
|
+
open.set(pid, LANES.includes(named) ? named : "interactive");
|
|
110
|
+
} else if (event === "released") {
|
|
111
|
+
const lane = open.get(pid);
|
|
112
|
+
const ran = Number(detail.match(/^ran=(\d+)/u)?.[1]);
|
|
113
|
+
if (lane === void 0 || !Number.isFinite(ran)) continue;
|
|
114
|
+
open.delete(pid);
|
|
115
|
+
(runs[lane] ??= []).push(ran);
|
|
116
|
+
runs["all"].push(ran);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const found = {};
|
|
120
|
+
for (const [key, values] of Object.entries(runs)) {
|
|
121
|
+
const value = median(values);
|
|
122
|
+
if (value !== void 0) found[key] = value;
|
|
123
|
+
}
|
|
124
|
+
return found;
|
|
125
|
+
}
|
|
126
|
+
function graceRuns(lines, now = Date.now()) {
|
|
127
|
+
const recent = lines.filter(
|
|
128
|
+
([stamp = "", , event]) => event === "grace" && now - Date.parse(stamp) <= 864e5
|
|
129
|
+
);
|
|
130
|
+
const last = recent.at(-1);
|
|
131
|
+
return {
|
|
132
|
+
count: recent.length,
|
|
133
|
+
last: last ? `${last[0]} (${last[3]})` : null
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function etas(medians, budget, running, waiters, now) {
|
|
137
|
+
if (medians["all"] === void 0) return waiters.map(() => null);
|
|
138
|
+
const typical = (lane) => medians[lane] ?? medians["all"] ?? 0;
|
|
139
|
+
let work = running.reduce(
|
|
140
|
+
(sum, job2) => sum + Math.max(0, typical(job2.lane) - (now - job2.since)) * job2.weight,
|
|
141
|
+
0
|
|
142
|
+
);
|
|
143
|
+
return waiters.map((waiter) => {
|
|
144
|
+
const mine = Math.round(work / Math.max(1, budget));
|
|
145
|
+
work += typical(waiter.lane) * waiter.weight;
|
|
146
|
+
return mine;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/lease/procs.ts
|
|
151
|
+
import { readFileSync, readdirSync } from "fs";
|
|
152
|
+
var TICKS_PER_SECOND = 100;
|
|
153
|
+
function read(file) {
|
|
154
|
+
try {
|
|
155
|
+
return readFileSync(file, "utf8");
|
|
156
|
+
} catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function sample(proc) {
|
|
161
|
+
const found = /* @__PURE__ */ new Map();
|
|
162
|
+
let names;
|
|
163
|
+
try {
|
|
164
|
+
names = readdirSync(proc);
|
|
165
|
+
} catch {
|
|
166
|
+
return found;
|
|
167
|
+
}
|
|
168
|
+
for (const name of names) {
|
|
169
|
+
if (!/^\d+$/u.test(name)) continue;
|
|
170
|
+
const stat = read(`${proc}/${name}/stat`);
|
|
171
|
+
if (stat === null) continue;
|
|
172
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
173
|
+
const io = read(`${proc}/${name}/io`) ?? "";
|
|
174
|
+
const bytes = (key) => Number(io.match(new RegExp(`^${key}: (\\d+)`, "mu"))?.[1] ?? 0);
|
|
175
|
+
found.set(Number(name), {
|
|
176
|
+
ppid: Number(fields[1]),
|
|
177
|
+
ticks: Number(fields[11]) + Number(fields[12]),
|
|
178
|
+
bytes: bytes("read_bytes") + bytes("write_bytes")
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return found;
|
|
182
|
+
}
|
|
183
|
+
function under(pid, roots, all) {
|
|
184
|
+
for (let at = pid, hops = 0; at > 1 && hops < 64; hops += 1) {
|
|
185
|
+
if (roots.has(at)) return true;
|
|
186
|
+
at = all.get(at)?.ppid ?? 0;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
function outsideLease(options) {
|
|
191
|
+
const proc = options.proc ?? "/proc";
|
|
192
|
+
const interval = options.interval ?? 1;
|
|
193
|
+
const before = sample(proc);
|
|
194
|
+
if (before.size === 0) return [];
|
|
195
|
+
sleepSeconds(interval);
|
|
196
|
+
const after = sample(proc);
|
|
197
|
+
const roots = /* @__PURE__ */ new Set([process.pid]);
|
|
198
|
+
for (const pid of options.holders) roots.add(pid);
|
|
199
|
+
const found = [];
|
|
200
|
+
for (const [pid, now] of after) {
|
|
201
|
+
const then = before.get(pid);
|
|
202
|
+
if (then === void 0 || now.ppid === 2 || pid === 2) continue;
|
|
203
|
+
if (under(pid, roots, after)) continue;
|
|
204
|
+
const cpu = (now.ticks - then.ticks) / TICKS_PER_SECOND / interval * 100;
|
|
205
|
+
const ioMbps = (now.bytes - then.bytes) / interval / 1e6;
|
|
206
|
+
if (cpu < 20 && ioMbps < 5) continue;
|
|
207
|
+
const command = (read(`${proc}/${pid}/cmdline`) ?? "").split("\0").join(" ").trim().slice(0, 100);
|
|
208
|
+
found.push({ pid, cpu, ioMbps, command });
|
|
209
|
+
}
|
|
210
|
+
return found.sort((a, b) => b.cpu + b.ioMbps * 10 - (a.cpu + a.ioMbps * 10)).slice(0, options.limit ?? 5);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/lease/status.ts
|
|
214
|
+
function hostLine(settings2, load, io) {
|
|
215
|
+
return `host: load1 ${load ?? "n/a"} (max ${settings2.maxLoad}), io some avg10 ${io ?? "n/a"} (max ${settings2.maxIo})`;
|
|
216
|
+
}
|
|
217
|
+
function holdLine(settings2, job2) {
|
|
218
|
+
const held = epoch() - job2.since;
|
|
219
|
+
const limits = `warn ${settings2.holdWarn}s${settings2.holdMax ? `, max ${settings2.holdMax}s` : ""}`;
|
|
220
|
+
let verdict = "ok";
|
|
221
|
+
if (held >= settings2.holdWarn) verdict = "warning: over hold_warn";
|
|
222
|
+
if (settings2.holdMax && held >= settings2.holdMax)
|
|
223
|
+
verdict = "over-limit: over hold_max";
|
|
224
|
+
return `hold: pid ${job2.pid} ${held}s (${limits}) ${verdict} lane ${job2.lane} weight ${job2.weight}`;
|
|
225
|
+
}
|
|
226
|
+
function outsideLines(found) {
|
|
227
|
+
if (found.length === 0) return ["outside: none"];
|
|
228
|
+
return found.map(
|
|
229
|
+
(item) => `outside: pid ${item.pid} cpu ${item.cpu.toFixed(0)}% io ${item.ioMbps.toFixed(1)}MB/s ${item.command}`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
function settledLine(settings2, env) {
|
|
233
|
+
const paths = leasePaths(settings2.dir);
|
|
234
|
+
if (hostSettled(settings2, paths, hostSources(env))) return "settled: now";
|
|
235
|
+
let last = "never";
|
|
236
|
+
try {
|
|
237
|
+
last = readFileSync2(paths.settled, "utf8").trim() || "never";
|
|
238
|
+
} catch {
|
|
239
|
+
}
|
|
240
|
+
return `settled: last seen ${last}`;
|
|
241
|
+
}
|
|
242
|
+
function leaseStatus(options) {
|
|
243
|
+
const { settings: settings2, env } = options;
|
|
244
|
+
const paths = leasePaths(settings2.dir);
|
|
245
|
+
const reading = readHost(hostSources(env));
|
|
246
|
+
const host = hostLine(settings2, reading.load, reading.io);
|
|
247
|
+
if (!settings2.enabled)
|
|
248
|
+
return ["lease: disabled ([host.lease] enabled = false)", host];
|
|
249
|
+
try {
|
|
250
|
+
mkdirSync(paths.dir, { recursive: true });
|
|
251
|
+
} catch {
|
|
252
|
+
}
|
|
253
|
+
const lines = [`lease: ${paths.lock}`, host, settledLine(settings2, env)];
|
|
254
|
+
const held = leaseHeld(paths);
|
|
255
|
+
lines.push(
|
|
256
|
+
held ? `holder: ${describeRecord(readRecord(paths.holder))}` : "holder: none"
|
|
257
|
+
);
|
|
258
|
+
const { running, waiters } = readQueue(paths);
|
|
259
|
+
const log = logLines(paths.log);
|
|
260
|
+
const eta = etas(runMedians(log), settings2.budget, running, waiters, epoch());
|
|
261
|
+
waiters.forEach((waiter, at) => {
|
|
262
|
+
const guess = eta[at];
|
|
263
|
+
const tail2 = guess === null || guess === void 0 ? "" : ` eta ~${guess}s`;
|
|
264
|
+
lines.push(
|
|
265
|
+
`waiter: ${describeRecord(waiter)} lane ${waiter.lane} weight ${waiter.weight} position ${at + 1}${tail2}${waiter.id ? ` id ${waiter.id}` : ""}`
|
|
266
|
+
);
|
|
267
|
+
});
|
|
268
|
+
if (waiters.length === 0) lines.push("waiters: none");
|
|
269
|
+
lines.push(`lanes: ${laneUse(settings2, running)}`);
|
|
270
|
+
for (const job2 of running) lines.push(`running: ${describeRecord(job2)}`);
|
|
271
|
+
for (const job2 of running.filter((entry) => entry.bypass))
|
|
272
|
+
lines.push(
|
|
273
|
+
`bypass running: pid ${job2.pid}, reason ${job2.bypass}, since ${epoch() - job2.since}s`
|
|
274
|
+
);
|
|
275
|
+
for (const job2 of running) lines.push(holdLine(settings2, job2));
|
|
276
|
+
const window = reading.pressure[settings2.ioWindow] ?? "n/a";
|
|
277
|
+
lines.push(
|
|
278
|
+
`io: the host check reads some ${settings2.ioWindow} ${window} (max ${settings2.maxIo}, hard ${settings2.maxIo * settings2.hardFactor})`
|
|
279
|
+
);
|
|
280
|
+
const grace = graceRuns(log);
|
|
281
|
+
lines.push(
|
|
282
|
+
grace.count === 0 ? "grace: no job started on load_grace in the last 24h" : `grace: ${grace.count} job(s) started on load_grace in the last 24h, last ${grace.last}`
|
|
283
|
+
);
|
|
284
|
+
const outside = options.outside ?? ((pids) => outsideLease({ holders: pids }));
|
|
285
|
+
lines.push(...outsideLines(outside(running.map((job2) => job2.pid))));
|
|
286
|
+
const skip = skipSource(settings2, env);
|
|
287
|
+
if (skip !== null)
|
|
288
|
+
lines.push(
|
|
289
|
+
`skip: this shell runs without the lease (${skip.variable}): ${skip.reason}`
|
|
290
|
+
);
|
|
291
|
+
return lines;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// src/lease/tickets.ts
|
|
295
|
+
import { randomBytes } from "crypto";
|
|
296
|
+
import {
|
|
297
|
+
mkdirSync as mkdirSync2,
|
|
298
|
+
openSync as openSync2,
|
|
299
|
+
closeSync as closeSync2,
|
|
300
|
+
readFileSync as readFileSync3,
|
|
301
|
+
readdirSync as readdirSync2,
|
|
302
|
+
renameSync,
|
|
303
|
+
rmSync,
|
|
304
|
+
statSync,
|
|
305
|
+
writeFileSync
|
|
306
|
+
} from "fs";
|
|
307
|
+
import { join } from "path";
|
|
308
|
+
import { spawn } from "child_process";
|
|
309
|
+
function ticketExists(dir, id) {
|
|
310
|
+
try {
|
|
311
|
+
return readJson(join(ticketDir(dir, id), "request.json")) !== null;
|
|
312
|
+
} catch {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
var ID = /^\d{10}-[0-9a-f]{6}$/u;
|
|
317
|
+
var KEEP_MS = 7 * 864e5;
|
|
318
|
+
function ticketsDir(dir) {
|
|
319
|
+
return join(dir, "tickets");
|
|
320
|
+
}
|
|
321
|
+
function ticketDir(dir, id) {
|
|
322
|
+
if (!ID.test(id)) throw new Error(`not a ticket id: ${id}`);
|
|
323
|
+
return join(ticketsDir(dir), id);
|
|
324
|
+
}
|
|
325
|
+
function readJson(file) {
|
|
326
|
+
try {
|
|
327
|
+
return JSON.parse(readFileSync3(file, "utf8"));
|
|
328
|
+
} catch {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function writeJson(file, value) {
|
|
333
|
+
const temporary = `${file}.tmp.${process.pid}`;
|
|
334
|
+
writeFileSync(temporary, `${JSON.stringify(value)}
|
|
335
|
+
`);
|
|
336
|
+
renameSync(temporary, file);
|
|
337
|
+
}
|
|
338
|
+
function writeResult(dir, id, result, keepExisting = false) {
|
|
339
|
+
const file = join(ticketDir(dir, id), "result.json");
|
|
340
|
+
if (keepExisting && readJson(file) !== null) return;
|
|
341
|
+
writeJson(file, result);
|
|
342
|
+
}
|
|
343
|
+
function prune(dir) {
|
|
344
|
+
let names = [];
|
|
345
|
+
try {
|
|
346
|
+
names = readdirSync2(ticketsDir(dir));
|
|
347
|
+
} catch {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
for (const name of names) {
|
|
351
|
+
const path = join(ticketsDir(dir), name);
|
|
352
|
+
try {
|
|
353
|
+
const done = statSync(join(path, "result.json"));
|
|
354
|
+
if (Date.now() - done.mtimeMs > KEEP_MS)
|
|
355
|
+
rmSync(path, { recursive: true, force: true });
|
|
356
|
+
} catch {
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function ticketOwner(dir, id) {
|
|
361
|
+
const request = readJson(
|
|
362
|
+
join(ticketDir(dir, id), "request.json")
|
|
363
|
+
);
|
|
364
|
+
return typeof request?.session === "string" ? request.session : null;
|
|
365
|
+
}
|
|
366
|
+
function openTicket(request, blocking) {
|
|
367
|
+
prune(request.dir);
|
|
368
|
+
const id = `${Math.floor(Date.now() / 1e3)}-${randomBytes(3).toString("hex")}`;
|
|
369
|
+
const path = ticketDir(request.dir, id);
|
|
370
|
+
mkdirSync2(path, { recursive: true });
|
|
371
|
+
writeJson(join(path, "request.json"), {
|
|
372
|
+
version: 1,
|
|
373
|
+
ticket: id,
|
|
374
|
+
label: request.label,
|
|
375
|
+
cwd: request.cwd,
|
|
376
|
+
...request.session ? { session: request.session } : {},
|
|
377
|
+
...blocking ? { blocking: true } : {},
|
|
378
|
+
created: (/* @__PURE__ */ new Date()).toISOString()
|
|
379
|
+
});
|
|
380
|
+
return id;
|
|
381
|
+
}
|
|
382
|
+
function blockingTicket(request) {
|
|
383
|
+
const id = openTicket(request, true);
|
|
384
|
+
writeFileSync(join(ticketDir(request.dir, id), "pid"), `${process.pid}
|
|
385
|
+
`);
|
|
386
|
+
return id;
|
|
387
|
+
}
|
|
388
|
+
function jobLogPath(dir, id) {
|
|
389
|
+
return join(ticketDir(dir, id), "log");
|
|
390
|
+
}
|
|
391
|
+
function submit(request) {
|
|
392
|
+
const id = openTicket(request, false);
|
|
393
|
+
const path = ticketDir(request.dir, id);
|
|
394
|
+
const log = openSync2(join(path, "runner.log"), "a");
|
|
395
|
+
const [bin, ...args] = request.runner(id);
|
|
396
|
+
const child = spawn(bin, args, {
|
|
397
|
+
cwd: request.cwd,
|
|
398
|
+
env: request.env,
|
|
399
|
+
detached: true,
|
|
400
|
+
stdio: ["ignore", log, log]
|
|
401
|
+
});
|
|
402
|
+
closeSync2(log);
|
|
403
|
+
writeFileSync(join(path, "pid"), `${child.pid}
|
|
404
|
+
`);
|
|
405
|
+
child.unref();
|
|
406
|
+
return id;
|
|
407
|
+
}
|
|
408
|
+
function ticketView(dir, id) {
|
|
409
|
+
const path = ticketDir(dir, id);
|
|
410
|
+
const request = readJson(join(path, "request.json"));
|
|
411
|
+
if (request === null) return null;
|
|
412
|
+
const result = readJson(join(path, "result.json"));
|
|
413
|
+
const pidText = readJson(join(path, "pid"));
|
|
414
|
+
const pid = typeof pidText === "number" && pidText > 0 ? pidText : null;
|
|
415
|
+
const base = { id, label: request.label, pid, result };
|
|
416
|
+
if (result) return { ...base, state: "done", position: null };
|
|
417
|
+
if (pid === null) return { ...base, state: "queued", position: null };
|
|
418
|
+
if (!pidAlive(pid)) return { ...base, state: "lost", position: null };
|
|
419
|
+
const paths = leasePaths(dir);
|
|
420
|
+
const waiting = liveRecords(paths.waiters, false).findIndex(
|
|
421
|
+
(r) => r.pid === pid
|
|
422
|
+
);
|
|
423
|
+
if (waiting >= 0) return { ...base, state: "queued", position: waiting + 1 };
|
|
424
|
+
const running = liveRecords(paths.running, false).some((r) => r.pid === pid);
|
|
425
|
+
return { ...base, state: running ? "running" : "queued", position: null };
|
|
426
|
+
}
|
|
427
|
+
function recentTickets(dir) {
|
|
428
|
+
let names = [];
|
|
429
|
+
try {
|
|
430
|
+
names = readdirSync2(ticketsDir(dir)).filter((name) => ID.test(name));
|
|
431
|
+
} catch {
|
|
432
|
+
return [];
|
|
433
|
+
}
|
|
434
|
+
return names.sort().map((name) => ticketView(dir, name)).filter((view) => {
|
|
435
|
+
if (view === null) return false;
|
|
436
|
+
if (view.state !== "done") return true;
|
|
437
|
+
try {
|
|
438
|
+
const done = statSync(join(ticketDir(dir, view.id), "result.json"));
|
|
439
|
+
return Date.now() - done.mtimeMs < 36e5;
|
|
440
|
+
} catch {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
function logTail(dir, id, lines = 20) {
|
|
446
|
+
try {
|
|
447
|
+
return readJobLog(jobLogPath(dir, id)).split("\n").slice(-lines - 1).join("\n");
|
|
448
|
+
} catch {
|
|
449
|
+
return "";
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function cancel(dir, id, force) {
|
|
453
|
+
const view = ticketView(dir, id);
|
|
454
|
+
if (view === null) return "unknown";
|
|
455
|
+
if (view.state === "done") return "done";
|
|
456
|
+
if (view.state === "running" && !force) return "running";
|
|
457
|
+
if (view.pid !== null && pidAlive(view.pid)) {
|
|
458
|
+
try {
|
|
459
|
+
process.kill(-view.pid, "SIGTERM");
|
|
460
|
+
} catch {
|
|
461
|
+
process.kill(view.pid, "SIGTERM");
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
writeResult(dir, id, {
|
|
465
|
+
version: 1,
|
|
466
|
+
ticket: id,
|
|
467
|
+
exit: 130,
|
|
468
|
+
outcome: "cancelled"
|
|
469
|
+
});
|
|
470
|
+
return "cancelled";
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/lease/status-json.ts
|
|
474
|
+
var STATUS_SCHEMA = "pmcp.heavy.status.v1";
|
|
475
|
+
function flags(settings2, job2, now) {
|
|
476
|
+
const held = now - job2.since;
|
|
477
|
+
return [
|
|
478
|
+
...job2.grace ? ["grace"] : [],
|
|
479
|
+
...job2.bypass ? ["bypass"] : [],
|
|
480
|
+
...held >= settings2.holdWarn ? ["over_hold_warn"] : [],
|
|
481
|
+
...settings2.holdMax && held >= settings2.holdMax ? ["over_hold_max"] : []
|
|
482
|
+
];
|
|
483
|
+
}
|
|
484
|
+
function job(settings2, record, now) {
|
|
485
|
+
return {
|
|
486
|
+
id: record.id || null,
|
|
487
|
+
pid: record.pid,
|
|
488
|
+
label: record.label,
|
|
489
|
+
cwd: record.cwd,
|
|
490
|
+
lane: record.lane,
|
|
491
|
+
weight: record.weight,
|
|
492
|
+
session: record.session || null,
|
|
493
|
+
started: record.started,
|
|
494
|
+
running_s: now - record.since,
|
|
495
|
+
flags: flags(settings2, record, now),
|
|
496
|
+
bypass_reason: record.bypass || null
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
function number(value) {
|
|
500
|
+
return value === null || value === void 0 ? null : Number(value);
|
|
501
|
+
}
|
|
502
|
+
function statusJson(settings2, env, outside = (pids) => outsideLease({ holders: pids, interval: 0.25 })) {
|
|
503
|
+
const paths = leasePaths(settings2.dir);
|
|
504
|
+
const reading = readHost(hostSources(env));
|
|
505
|
+
const now = epoch();
|
|
506
|
+
const { running, waiters } = settings2.enabled ? readQueue(paths, false) : { running: [], waiters: [] };
|
|
507
|
+
const log = settings2.enabled ? logLines(paths.log) : [];
|
|
508
|
+
const eta = etas(runMedians(log), settings2.budget, running, waiters, now);
|
|
509
|
+
return {
|
|
510
|
+
version: 1,
|
|
511
|
+
schema: STATUS_SCHEMA,
|
|
512
|
+
enabled: settings2.enabled,
|
|
513
|
+
dir: settings2.dir,
|
|
514
|
+
budget: settings2.budget,
|
|
515
|
+
running_weight: runningWeight(running),
|
|
516
|
+
// A shell wrapper holds the lock file exclusively and counts as the whole budget.
|
|
517
|
+
legacy_holder: settings2.enabled && running.length === 0 && leaseHeld(paths),
|
|
518
|
+
host: {
|
|
519
|
+
load1: number(reading.load),
|
|
520
|
+
io_some_avg10: number(reading.pressure["avg10"]),
|
|
521
|
+
io_some_avg60: number(reading.pressure["avg60"]),
|
|
522
|
+
io_some_avg300: number(reading.pressure["avg300"]),
|
|
523
|
+
io_window: settings2.ioWindow,
|
|
524
|
+
max_load: settings2.maxLoad,
|
|
525
|
+
max_io: settings2.maxIo,
|
|
526
|
+
hard_factor: settings2.hardFactor,
|
|
527
|
+
settled: settled(settings2, reading)
|
|
528
|
+
},
|
|
529
|
+
lanes: LANES.map((lane) => ({
|
|
530
|
+
lane,
|
|
531
|
+
slots: settings2.slots[lane],
|
|
532
|
+
running: running.filter((record) => record.lane === lane).map((record) => job(settings2, record, now))
|
|
533
|
+
})),
|
|
534
|
+
waiters: waiters.map((record, at) => ({
|
|
535
|
+
id: record.id || null,
|
|
536
|
+
pid: record.pid,
|
|
537
|
+
label: record.label,
|
|
538
|
+
cwd: record.cwd,
|
|
539
|
+
lane: record.lane,
|
|
540
|
+
weight: record.weight,
|
|
541
|
+
session: record.session || null,
|
|
542
|
+
position: at + 1,
|
|
543
|
+
waited_s: now - record.since,
|
|
544
|
+
eta_s: eta[at] ?? null
|
|
545
|
+
})),
|
|
546
|
+
tickets: settings2.enabled ? recentTickets(settings2.dir).map((view) => ({
|
|
547
|
+
id: view.id,
|
|
548
|
+
state: view.state,
|
|
549
|
+
position: view.position,
|
|
550
|
+
label: view.label,
|
|
551
|
+
pid: view.pid,
|
|
552
|
+
exit: view.result?.exit ?? null
|
|
553
|
+
})) : [],
|
|
554
|
+
grace_24h: graceRuns(log),
|
|
555
|
+
outside: settings2.enabled ? outside(running.map((record) => record.pid)).map((item) => ({
|
|
556
|
+
pid: item.pid,
|
|
557
|
+
cpu_percent: Math.round(item.cpu),
|
|
558
|
+
io_mbps: Math.round(item.ioMbps * 10) / 10,
|
|
559
|
+
command: item.command
|
|
560
|
+
})) : [],
|
|
561
|
+
skip: skipReason(settings2, env)
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/lease/control.ts
|
|
566
|
+
import { readdirSync as readdirSync3 } from "fs";
|
|
567
|
+
import { join as join2 } from "path";
|
|
568
|
+
function find(paths, dir, id) {
|
|
569
|
+
let names = [];
|
|
570
|
+
try {
|
|
571
|
+
names = readdirSync3(dir).filter((name) => /^\d+$/u.test(name));
|
|
572
|
+
} catch {
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
for (const name of names) {
|
|
576
|
+
const record = readRecord(join2(dir, name));
|
|
577
|
+
if (record?.id === id) return { file: join2(dir, name), record };
|
|
578
|
+
}
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
function findRunning(paths, id) {
|
|
582
|
+
return liveRecords(paths.running, false).find((r) => r.id === id) ?? null;
|
|
583
|
+
}
|
|
584
|
+
function rewrite(file, record, change2) {
|
|
585
|
+
const next = { ...record, ...change2 };
|
|
586
|
+
writeRecord(file, next.pid, {
|
|
587
|
+
cwd: next.cwd,
|
|
588
|
+
label: next.label,
|
|
589
|
+
lane: next.lane,
|
|
590
|
+
weight: next.weight,
|
|
591
|
+
session: next.session,
|
|
592
|
+
queued: next.queued,
|
|
593
|
+
since: next.since,
|
|
594
|
+
started: next.started,
|
|
595
|
+
...next.id ? { id: next.id } : {},
|
|
596
|
+
...next.promote ? { promote: next.promote } : {},
|
|
597
|
+
...next.cancel ? { cancel: next.cancel } : {}
|
|
598
|
+
});
|
|
599
|
+
return next;
|
|
600
|
+
}
|
|
601
|
+
function logged(paths, record, who2, what, reason2) {
|
|
602
|
+
const by = `uid:${who2.uid ?? "?"}${who2.session ? ` session=${who2.session}` : ""}`;
|
|
603
|
+
logEvent(
|
|
604
|
+
paths,
|
|
605
|
+
record.pid,
|
|
606
|
+
"control",
|
|
607
|
+
`id=${record.id} ${what} by=${by} reason=${reason2}`
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
function change(paths, id, who2, reason2, what) {
|
|
611
|
+
const done = underAdmitLock(paths, () => {
|
|
612
|
+
const found = find(paths, paths.waiters, id);
|
|
613
|
+
if (found === null) return { ok: false, why: `no waiting job ${id}` };
|
|
614
|
+
const waiters = liveRecords(paths.waiters);
|
|
615
|
+
if (!waiters.some((r) => r.pid === found.record.pid))
|
|
616
|
+
return { ok: false, why: `${id} had died; its record was removed` };
|
|
617
|
+
const decided = what(found.record, waiters);
|
|
618
|
+
if (typeof decided === "string") return { ok: false, why: decided };
|
|
619
|
+
const [fields, description] = decided;
|
|
620
|
+
const record = rewrite(found.file, found.record, fields);
|
|
621
|
+
logged(paths, record, who2, description, reason2);
|
|
622
|
+
return { ok: true, record };
|
|
623
|
+
});
|
|
624
|
+
return done ?? { ok: false, why: "the admission lock stayed busy" };
|
|
625
|
+
}
|
|
626
|
+
function promote(paths, id, who2, reason2) {
|
|
627
|
+
return change(paths, id, who2, reason2, () => [
|
|
628
|
+
{ promote: reason2 },
|
|
629
|
+
"action=promote"
|
|
630
|
+
]);
|
|
631
|
+
}
|
|
632
|
+
function cancelWaiter(paths, id, who2, reason2) {
|
|
633
|
+
return change(paths, id, who2, reason2, () => [
|
|
634
|
+
{ cancel: reason2 },
|
|
635
|
+
"action=cancel"
|
|
636
|
+
]);
|
|
637
|
+
}
|
|
638
|
+
function reprioritize(paths, id, to, who2, reason2) {
|
|
639
|
+
return change(paths, id, who2, reason2, (record, waiters) => {
|
|
640
|
+
if (LANES.includes(to))
|
|
641
|
+
return [
|
|
642
|
+
{ lane: to, queued: Date.now() },
|
|
643
|
+
`action=priority from=${record.lane} to=${to}`
|
|
644
|
+
];
|
|
645
|
+
const step = /^[+-]\d+$/u.test(to) ? Number(to) : NaN;
|
|
646
|
+
if (!Number.isInteger(step) || step === 0)
|
|
647
|
+
return `priority is a lane (${LANES.join(", ")}) or +N/-N, not ${to}`;
|
|
648
|
+
const lane = waiters.filter((r) => r.lane === record.lane);
|
|
649
|
+
const at = lane.findIndex((r) => r.pid === record.pid);
|
|
650
|
+
const target = Math.max(0, Math.min(lane.length - 1, at - step));
|
|
651
|
+
if (target === at) return [{}, `action=priority ${to} (already there)`];
|
|
652
|
+
const other = lane[target];
|
|
653
|
+
const queued = target < at ? other.queued - 1 : other.queued + 1;
|
|
654
|
+
return [
|
|
655
|
+
{ queued },
|
|
656
|
+
`action=priority ${to} position=${at + 1}->${target + 1}`
|
|
657
|
+
];
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// src/commands/heavy-queue.ts
|
|
662
|
+
function who(context) {
|
|
663
|
+
return {
|
|
664
|
+
uid: process.getuid?.() ?? null,
|
|
665
|
+
session: sessionOf(context.env, context.cwd)
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
function reason(context, required) {
|
|
669
|
+
const text = one(context.args, "reason")?.trim() ?? "";
|
|
670
|
+
if (required && !text)
|
|
671
|
+
throw new ArgumentError("say why with --reason <text>");
|
|
672
|
+
return text || "none given";
|
|
673
|
+
}
|
|
674
|
+
function report(context, result) {
|
|
675
|
+
if (!result.ok) {
|
|
676
|
+
context.ui.error(result.why);
|
|
677
|
+
return EXIT.failure;
|
|
678
|
+
}
|
|
679
|
+
context.ui.data(
|
|
680
|
+
`${result.record.id} ${result.record.lane} ${result.record.label}
|
|
681
|
+
`
|
|
682
|
+
);
|
|
683
|
+
return EXIT.ok;
|
|
684
|
+
}
|
|
685
|
+
function queueList(context, settings2) {
|
|
686
|
+
const snapshot = statusJson(settings2, context.env, () => []);
|
|
687
|
+
if (context.args.flags.has("json")) {
|
|
688
|
+
const { version, schema, waiters } = snapshot;
|
|
689
|
+
context.ui.data(`${JSON.stringify({ version, schema, waiters })}
|
|
690
|
+
`);
|
|
691
|
+
return EXIT.ok;
|
|
692
|
+
}
|
|
693
|
+
if (snapshot.waiters.length === 0) context.ui.data("queue: empty\n");
|
|
694
|
+
context.ui.results.records(
|
|
695
|
+
snapshot.waiters.map(
|
|
696
|
+
(w) => `${w.id} ${w.lane} position ${w.position} waited ${w.waited_s}s eta ${w.eta_s ?? "?"}s weight ${w.weight} pid ${w.pid} ${w.session ?? "-"} ${w.label}`
|
|
697
|
+
)
|
|
698
|
+
);
|
|
699
|
+
return EXIT.ok;
|
|
700
|
+
}
|
|
701
|
+
function queuePromote(context, settings2, id) {
|
|
702
|
+
if (id === void 0) throw new ArgumentError("name the job id");
|
|
703
|
+
if (!context.args.flags.has("now"))
|
|
704
|
+
throw new ArgumentError(
|
|
705
|
+
"promote starts a job at once: pass --now --reason <text>"
|
|
706
|
+
);
|
|
707
|
+
requireOperatorFor(context, "pmcp heavy promote --now");
|
|
708
|
+
const paths = leasePaths(settings2.dir);
|
|
709
|
+
return report(
|
|
710
|
+
context,
|
|
711
|
+
promote(paths, id, who(context), reason(context, true))
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
function queuePriority(context, settings2, id, to) {
|
|
715
|
+
if (id === void 0 || to === void 0)
|
|
716
|
+
throw new ArgumentError(
|
|
717
|
+
"pmcp heavy priority <id> <lane|+N|-N> --reason <text>"
|
|
718
|
+
);
|
|
719
|
+
const paths = leasePaths(settings2.dir);
|
|
720
|
+
return report(
|
|
721
|
+
context,
|
|
722
|
+
reprioritize(paths, id, to, who(context), reason(context, true))
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
function queueCancel(context, settings2, id) {
|
|
726
|
+
if (id === void 0) throw new ArgumentError("name the job id");
|
|
727
|
+
const paths = leasePaths(settings2.dir);
|
|
728
|
+
const because = reason(context, false);
|
|
729
|
+
const ticket = /^\d{10}-[0-9a-f]{6}$/u.test(id) && ticketExists(settings2.dir, id);
|
|
730
|
+
const waiting = cancelWaiter(paths, id, who(context), because);
|
|
731
|
+
if (waiting.ok) {
|
|
732
|
+
if (ticket)
|
|
733
|
+
writeResult(settings2.dir, id, {
|
|
734
|
+
version: 1,
|
|
735
|
+
ticket: id,
|
|
736
|
+
exit: 130,
|
|
737
|
+
outcome: "cancelled"
|
|
738
|
+
});
|
|
739
|
+
context.ui.data(`${id} cancelled
|
|
740
|
+
`);
|
|
741
|
+
return EXIT.ok;
|
|
742
|
+
}
|
|
743
|
+
const running = findRunning(paths, id);
|
|
744
|
+
const force = context.args.flags.has("force");
|
|
745
|
+
const owner = (ticket ? ticketOwner(settings2.dir, id) : null) ?? running?.session;
|
|
746
|
+
if (force && owner !== void 0 && owner !== who(context).session)
|
|
747
|
+
requireOperatorFor(
|
|
748
|
+
context,
|
|
749
|
+
`pmcp heavy cancel --force on another session's job (${owner})`
|
|
750
|
+
);
|
|
751
|
+
if (running !== null && !ticket) {
|
|
752
|
+
if (!force) {
|
|
753
|
+
context.ui.error(`${id} is running; --force stops it`);
|
|
754
|
+
return EXIT.failure;
|
|
755
|
+
}
|
|
756
|
+
process.kill(running.pid, "SIGTERM");
|
|
757
|
+
context.ui.data(`${id} stopped
|
|
758
|
+
`);
|
|
759
|
+
return EXIT.ok;
|
|
760
|
+
}
|
|
761
|
+
if (!ticket) {
|
|
762
|
+
context.ui.error(waiting.why);
|
|
763
|
+
return EXIT.failure;
|
|
764
|
+
}
|
|
765
|
+
const done = cancel(settings2.dir, id, force);
|
|
766
|
+
if (done === "running") {
|
|
767
|
+
context.ui.error(`${id} is running; --force stops it`);
|
|
768
|
+
return EXIT.failure;
|
|
769
|
+
}
|
|
770
|
+
context.ui.data(`${id} ${done}
|
|
771
|
+
`);
|
|
772
|
+
return done === "cancelled" ? EXIT.ok : EXIT.failure;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// src/commands/heavy-tickets.ts
|
|
776
|
+
var PASSED = ["config", "label", "profile", "lane", "weight"];
|
|
777
|
+
function submitTicket(context, settings2, command) {
|
|
778
|
+
const flags2 = PASSED.flatMap((name) => {
|
|
779
|
+
const value = one(context.args, name);
|
|
780
|
+
return value === void 0 ? [] : [`--${name}`, value];
|
|
781
|
+
});
|
|
782
|
+
const self = [process.execPath, ...process.execArgv, process.argv[1] ?? ""];
|
|
783
|
+
const id = submit({
|
|
784
|
+
dir: settings2.dir,
|
|
785
|
+
label: one(context.args, "label") ?? command.join(" "),
|
|
786
|
+
runner: (ticket) => [
|
|
787
|
+
...self,
|
|
788
|
+
"heavy",
|
|
789
|
+
...flags2,
|
|
790
|
+
"--ticket",
|
|
791
|
+
ticket,
|
|
792
|
+
"--",
|
|
793
|
+
...command
|
|
794
|
+
],
|
|
795
|
+
cwd: context.cwd,
|
|
796
|
+
env: context.env,
|
|
797
|
+
session: sessionOf(context.env, context.cwd)
|
|
798
|
+
});
|
|
799
|
+
context.ui.data(`${id}
|
|
800
|
+
`);
|
|
801
|
+
return EXIT.ok;
|
|
802
|
+
}
|
|
803
|
+
function describe(view) {
|
|
804
|
+
const where = view.position === null ? "" : ` position ${view.position}`;
|
|
805
|
+
const result = view.result ? ` exit ${view.result.exit} (${view.result.outcome})` : "";
|
|
806
|
+
return `${view.id} ${view.state}${where}${result} ${view.label}`;
|
|
807
|
+
}
|
|
808
|
+
function viewOf(settings2, id) {
|
|
809
|
+
if (id === void 0) throw new ArgumentError("name the ticket");
|
|
810
|
+
let view;
|
|
811
|
+
try {
|
|
812
|
+
view = ticketView(settings2.dir, id);
|
|
813
|
+
} catch (error) {
|
|
814
|
+
throw new ArgumentError(error.message);
|
|
815
|
+
}
|
|
816
|
+
if (view === null) throw new ArgumentError(`no ticket ${id}`);
|
|
817
|
+
return view;
|
|
818
|
+
}
|
|
819
|
+
function ticketResult(context, settings2, id) {
|
|
820
|
+
const view = viewOf(settings2, id);
|
|
821
|
+
const tailText = one(context.args, "tail");
|
|
822
|
+
if (tailText !== void 0 && !context.args.flags.has("log"))
|
|
823
|
+
throw new ArgumentError("--tail goes with --log");
|
|
824
|
+
if (context.args.flags.has("log")) {
|
|
825
|
+
const tail2 = tailText === void 0 ? void 0 : Number(tailText);
|
|
826
|
+
if (tail2 !== void 0 && !(Number.isInteger(tail2) && tail2 >= 1))
|
|
827
|
+
throw new ArgumentError("--tail is a whole number of at least 1");
|
|
828
|
+
context.ui.data(readJobLog(jobLogPath(settings2.dir, view.id), tail2));
|
|
829
|
+
return EXIT.ok;
|
|
830
|
+
}
|
|
831
|
+
if (context.args.flags.has("json"))
|
|
832
|
+
context.ui.data(`${JSON.stringify({ version: 1, ...view })}
|
|
833
|
+
`);
|
|
834
|
+
else context.ui.data(`${describe(view)}
|
|
835
|
+
`);
|
|
836
|
+
return EXIT.ok;
|
|
837
|
+
}
|
|
838
|
+
async function ticketWait(context, settings2, id) {
|
|
839
|
+
const text = one(context.args, "timeout");
|
|
840
|
+
const timeout = text === void 0 ? Infinity : Number(text);
|
|
841
|
+
if (!(timeout > 0)) throw new ArgumentError("--timeout must be positive");
|
|
842
|
+
const deadline = Date.now() + timeout * 1e3;
|
|
843
|
+
let view = viewOf(settings2, id);
|
|
844
|
+
while (view.state === "queued" || view.state === "running") {
|
|
845
|
+
if (Date.now() >= deadline) {
|
|
846
|
+
context.ui.error(`${view.id} is still ${view.state}`);
|
|
847
|
+
return BUSY_EXIT;
|
|
848
|
+
}
|
|
849
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
850
|
+
view = viewOf(settings2, view.id);
|
|
851
|
+
}
|
|
852
|
+
const tail2 = logTail(settings2.dir, view.id);
|
|
853
|
+
if (tail2.trim()) process.stderr.write(`${tail2}
|
|
854
|
+
`);
|
|
855
|
+
context.ui.data(`${describe(view)}
|
|
856
|
+
`);
|
|
857
|
+
if (view.state === "lost") return EXIT.failure;
|
|
858
|
+
return view.result?.exit ?? EXIT.failure;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// src/commands/heavy.ts
|
|
862
|
+
function settings(context) {
|
|
863
|
+
try {
|
|
864
|
+
return leaseSettingsFor(context.cwd, one(context.args, "config"));
|
|
865
|
+
} catch (error) {
|
|
866
|
+
if (error instanceof ConfigError) throw new ArgumentError(error.message);
|
|
867
|
+
throw error;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
function statLine(stat) {
|
|
871
|
+
const pair = (key, scale = 1, unit = "") => `${Math.round(stat.p50[key] / scale)}/${Math.round(stat.p90[key] / scale)}${unit}`;
|
|
872
|
+
const cpu = {
|
|
873
|
+
p50: stat.p50.user_s + stat.p50.sys_s,
|
|
874
|
+
p90: stat.p90.user_s + stat.p90.sys_s
|
|
875
|
+
};
|
|
876
|
+
return [
|
|
877
|
+
stat.signature,
|
|
878
|
+
`samples ${stat.samples} (failed ${stat.failed}, timeouts ${stat.timeouts}, skipped the lease ${stat.skipped})`,
|
|
879
|
+
`wall ${pair("wall_s", 1, "s")}`,
|
|
880
|
+
`cpu ${Math.round(cpu.p50)}/${Math.round(cpu.p90)}s`,
|
|
881
|
+
`rss ${pair("max_rss_kb", 1024, "MB")}`,
|
|
882
|
+
`read ${pair("read_bytes", 1e6, "MB")}`,
|
|
883
|
+
`write ${pair("write_bytes", 1e6, "MB")}`
|
|
884
|
+
].join(" ");
|
|
885
|
+
}
|
|
886
|
+
function top(context) {
|
|
887
|
+
const text = one(context.args, "top");
|
|
888
|
+
if (text === void 0) return void 0;
|
|
889
|
+
const count = Number(text);
|
|
890
|
+
if (!Number.isInteger(count) || count < 1)
|
|
891
|
+
throw new ArgumentError("--top is a whole number of at least 1");
|
|
892
|
+
return count;
|
|
893
|
+
}
|
|
894
|
+
function stats(context) {
|
|
895
|
+
const records = readUsage(leasePaths(settings(context).dir).usage);
|
|
896
|
+
const limit = top(context);
|
|
897
|
+
const found = usageStats(records).slice(0, limit);
|
|
898
|
+
if (context.args.flags.has("json")) {
|
|
899
|
+
context.ui.data(`${JSON.stringify(found)}
|
|
900
|
+
`);
|
|
901
|
+
return EXIT.ok;
|
|
902
|
+
}
|
|
903
|
+
const lines = [];
|
|
904
|
+
if (found.length === 0) lines.push("no usage recorded yet");
|
|
905
|
+
const { skipped, runs } = skipShare(records);
|
|
906
|
+
if (runs > 0)
|
|
907
|
+
lines.push(
|
|
908
|
+
`skipped the lease: ${skipped} of ${runs} runs (${Math.round(skipped / runs * 100)}%)`
|
|
909
|
+
);
|
|
910
|
+
const reasons = skipReasons(records);
|
|
911
|
+
const counted = (counts) => Object.entries(counts).sort(([a, x], [b, y]) => y - x || a.localeCompare(b)).map(([name, count]) => `${name} ${count}`).join(", ");
|
|
912
|
+
if (Object.keys(reasons.honoured).length > 0)
|
|
913
|
+
lines.push(`skip reasons: ${counted(reasons.honoured)}`);
|
|
914
|
+
if (Object.keys(reasons.invalid).length > 0)
|
|
915
|
+
lines.push(`refused skip reasons: ${counted(reasons.invalid)}`);
|
|
916
|
+
lines.push(
|
|
917
|
+
"signature @ dir samples p50/p90 of wall, cpu, rss, read, write"
|
|
918
|
+
);
|
|
919
|
+
lines.push(...found.map(statLine));
|
|
920
|
+
context.ui.results.records(lines);
|
|
921
|
+
return EXIT.ok;
|
|
922
|
+
}
|
|
923
|
+
function verbs(context) {
|
|
924
|
+
const { positional, passthrough } = context.args;
|
|
925
|
+
return positional.slice(0, positional.length - (passthrough?.length ?? 0));
|
|
926
|
+
}
|
|
927
|
+
function subcommand(context) {
|
|
928
|
+
const [verb, ticket, to, ...rest] = verbs(context);
|
|
929
|
+
const lease = settings(context);
|
|
930
|
+
if (verb === "priority" && rest.length === 0)
|
|
931
|
+
return queuePriority(context, lease, ticket, to);
|
|
932
|
+
if (to !== void 0) throw new ArgumentError(`unexpected ${to}`);
|
|
933
|
+
if (verb === "result") return ticketResult(context, lease, ticket);
|
|
934
|
+
if (verb === "wait") return ticketWait(context, lease, ticket);
|
|
935
|
+
if (verb === "cancel") return queueCancel(context, lease, ticket);
|
|
936
|
+
if (verb === "promote") return queuePromote(context, lease, ticket);
|
|
937
|
+
if (ticket !== void 0 && verb !== "queue")
|
|
938
|
+
throw new ArgumentError(`unexpected ${ticket}`);
|
|
939
|
+
if (verb === "queue") return queueList(context, lease);
|
|
940
|
+
if (verb !== "status" && verb !== "stats")
|
|
941
|
+
throw new ArgumentError(
|
|
942
|
+
"put the command after --, or use pmcp heavy status | stats | queue | submit | wait | result | promote | priority | cancel"
|
|
943
|
+
);
|
|
944
|
+
if (verb === "stats") return stats(context);
|
|
945
|
+
if (context.args.flags.has("json")) {
|
|
946
|
+
const snapshot = statusJson(lease, context.env);
|
|
947
|
+
context.ui.data(`${JSON.stringify(snapshot)}
|
|
948
|
+
`);
|
|
949
|
+
return EXIT.ok;
|
|
950
|
+
}
|
|
951
|
+
context.ui.results.records(
|
|
952
|
+
leaseStatus({ settings: lease, env: context.env })
|
|
953
|
+
);
|
|
954
|
+
return EXIT.ok;
|
|
955
|
+
}
|
|
956
|
+
async function run(context) {
|
|
957
|
+
const { args } = context;
|
|
958
|
+
const say = (line) => process.stderr.write(`pmcp heavy: ${line}
|
|
959
|
+
`);
|
|
960
|
+
const [verb, ...extra] = verbs(context);
|
|
961
|
+
if (args.passthrough === void 0 || verb !== void 0) {
|
|
962
|
+
if (verb !== "submit") return subcommand(context);
|
|
963
|
+
if (extra.length > 0) throw new ArgumentError(`unexpected ${extra[0]}`);
|
|
964
|
+
}
|
|
965
|
+
if (args.passthrough === void 0 || args.passthrough.length === 0)
|
|
966
|
+
throw new ArgumentError("no command after --");
|
|
967
|
+
const lease = settings(context);
|
|
968
|
+
const profile = one(args, "profile") ?? "default";
|
|
969
|
+
if (lease.profiles[profile] === void 0)
|
|
970
|
+
throw new ArgumentError(
|
|
971
|
+
`unknown profile ${profile}; known: ${Object.keys(lease.profiles).join(", ")}`
|
|
972
|
+
);
|
|
973
|
+
const label = one(args, "label");
|
|
974
|
+
const reason2 = one(args, "reason") ?? context.env[NOW_ENV];
|
|
975
|
+
const now = args.flags.has("now") || Boolean(context.env[NOW_ENV]);
|
|
976
|
+
if (now && !reason2?.trim())
|
|
977
|
+
throw new ArgumentError(
|
|
978
|
+
"--now needs --reason <text>: say why it cannot wait"
|
|
979
|
+
);
|
|
980
|
+
if (!now && one(args, "reason") !== void 0)
|
|
981
|
+
throw new ArgumentError("--reason goes with --now");
|
|
982
|
+
const lane = one(args, "lane");
|
|
983
|
+
if (lane !== void 0 && !LANES.includes(lane))
|
|
984
|
+
throw new ArgumentError(`--lane is ${LANES.join(", ")}`);
|
|
985
|
+
const weightText = one(args, "weight");
|
|
986
|
+
const weight = weightText === void 0 ? void 0 : Number(weightText);
|
|
987
|
+
if (weight !== void 0 && !(Number.isInteger(weight) && weight >= 0))
|
|
988
|
+
throw new ArgumentError("--weight is a whole number (0 for a tiny job)");
|
|
989
|
+
if (verb === "submit") {
|
|
990
|
+
if (now) throw new ArgumentError("--now runs at once; it is not submitted");
|
|
991
|
+
return submitTicket(context, lease, args.passthrough);
|
|
992
|
+
}
|
|
993
|
+
if (now) requireOperatorFor(context, "pmcp heavy --now");
|
|
994
|
+
const submitted = one(args, "ticket");
|
|
995
|
+
const kept = keepOutput(context, lease, label, submitted);
|
|
996
|
+
const ticket = submitted ?? kept?.id;
|
|
997
|
+
let result = null;
|
|
998
|
+
const config = one(args, "config") ?? findConfig(context.cwd);
|
|
999
|
+
const code = await runHeavy({
|
|
1000
|
+
settings: lease,
|
|
1001
|
+
profile,
|
|
1002
|
+
...label ? { label } : {},
|
|
1003
|
+
...lane ? { lane } : {},
|
|
1004
|
+
...weight !== void 0 ? { weight } : {},
|
|
1005
|
+
...now ? { now: reason2 } : {},
|
|
1006
|
+
root: config ? dirname(config) : context.cwd,
|
|
1007
|
+
operator: () => isOperator(context),
|
|
1008
|
+
...ticket ? { id: ticket } : {},
|
|
1009
|
+
argv: args.passthrough,
|
|
1010
|
+
cwd: context.cwd,
|
|
1011
|
+
env: context.env,
|
|
1012
|
+
say: (line) => {
|
|
1013
|
+
say(line);
|
|
1014
|
+
kept?.log.write(`pmcp heavy: ${line}
|
|
1015
|
+
`);
|
|
1016
|
+
},
|
|
1017
|
+
...kept ? { output: kept.log, echo: submitted === void 0 } : {},
|
|
1018
|
+
...ticket ? {
|
|
1019
|
+
onUsage: (usage) => {
|
|
1020
|
+
result = { version: 1, ticket, ...usage };
|
|
1021
|
+
}
|
|
1022
|
+
} : {}
|
|
1023
|
+
});
|
|
1024
|
+
kept?.log.close();
|
|
1025
|
+
if (ticket)
|
|
1026
|
+
writeResult(
|
|
1027
|
+
lease.dir,
|
|
1028
|
+
ticket,
|
|
1029
|
+
result ?? { version: 1, ticket, exit: code, outcome: "ran" },
|
|
1030
|
+
true
|
|
1031
|
+
);
|
|
1032
|
+
if (kept && submitted === void 0 && code !== 0)
|
|
1033
|
+
say(`output kept: pmcp heavy result ${kept.id} --log`);
|
|
1034
|
+
return code;
|
|
1035
|
+
}
|
|
1036
|
+
function keepOutput(context, lease, label, submitted) {
|
|
1037
|
+
const cap = lease.jobLogBytes ?? DEFAULT_JOB_LOG_BYTES;
|
|
1038
|
+
try {
|
|
1039
|
+
if (submitted !== void 0)
|
|
1040
|
+
return {
|
|
1041
|
+
id: submitted,
|
|
1042
|
+
log: new JobLog(
|
|
1043
|
+
jobLogPath(lease.dir, submitted),
|
|
1044
|
+
cap || DEFAULT_JOB_LOG_BYTES
|
|
1045
|
+
)
|
|
1046
|
+
};
|
|
1047
|
+
if (cap === 0 || !lease.enabled) return null;
|
|
1048
|
+
const id = blockingTicket({
|
|
1049
|
+
dir: lease.dir,
|
|
1050
|
+
label: label ?? context.args.passthrough.join(" "),
|
|
1051
|
+
cwd: context.cwd,
|
|
1052
|
+
session: sessionOf(context.env, context.cwd)
|
|
1053
|
+
});
|
|
1054
|
+
return { id, log: new JobLog(jobLogPath(lease.dir, id), cap) };
|
|
1055
|
+
} catch {
|
|
1056
|
+
return null;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
var heavyCommand = { ...heavySpec, run };
|
|
1060
|
+
export {
|
|
1061
|
+
heavyCommand
|
|
1062
|
+
};
|
|
1063
|
+
//# sourceMappingURL=heavy-VBC4AGL6.js.map
|