@modootoday/pmcp 0.22.11 → 0.22.13
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/affected-ZWO2F5EM.js +836 -0
- package/dist/{apply-DXM56NV3.js → apply-OIKQ5SIY.js} +21 -17
- package/dist/{available-BFXLII4A.js → available-Z5MIDFPH.js} +3 -3
- package/dist/{broadcast-DZGMSZRG.js → broadcast-BF2QL3O7.js} +6 -5
- package/dist/{chunk-TU2SG5BM.js → chunk-24L4MO7H.js} +2 -2
- package/dist/{chunk-7I7O7ZNT.js → chunk-3V37OZQH.js} +5 -3
- package/dist/{chunk-PEMMV5YQ.js → chunk-4O4ZOSBQ.js} +3 -3
- package/dist/{chunk-7ZWSCSZM.js → chunk-5IZFEVPW.js} +2 -2
- package/dist/{chunk-TQODDSIG.js → chunk-5KWDQFVY.js} +4 -4
- package/dist/{chunk-R5MT42JS.js → chunk-5OFQVW2K.js} +2 -2
- package/dist/{chunk-REFEJ4VJ.js → chunk-5PNS6V5C.js} +2 -2
- package/dist/chunk-65BW7Y6O.js +40 -0
- package/dist/{chunk-IATFWEAX.js → chunk-733WVP2R.js} +19 -7
- package/dist/{chunk-URCQLONP.js → chunk-737BENAU.js} +8 -8
- package/dist/{chunk-GFOQDLPL.js → chunk-74O7KZUH.js} +13 -3
- package/dist/{chunk-5ZUEPPFZ.js → chunk-7ZOQELXR.js} +9 -7
- package/dist/{chunk-ENOMRAFS.js → chunk-A6DARDQ4.js} +3 -3
- package/dist/{chunk-PUIWESU3.js → chunk-A7XCUR2C.js} +4 -4
- package/dist/{chunk-P4N7DBD5.js → chunk-ANJMLKBR.js} +2 -2
- package/dist/chunk-BN4XAXMQ.js +65 -0
- package/dist/chunk-FOLA4EUJ.js +261 -0
- package/dist/chunk-FQLCD4HU.js +24 -0
- package/dist/chunk-G4ATQPQI.js +35 -0
- package/dist/{chunk-TZWLT6FD.js → chunk-GNVME5AD.js} +2 -2
- package/dist/{chunk-OEH47H6A.js → chunk-GUHOEIHP.js} +3 -20
- package/dist/{chunk-PIVKGKMR.js → chunk-ISGBOJT7.js} +3 -3
- package/dist/{chunk-RPCIROW7.js → chunk-J3QUG6O5.js} +2 -2
- package/dist/{chunk-B2WRYGHJ.js → chunk-JW5NMQTU.js} +3 -3
- package/dist/{chunk-2GOR4ZKO.js → chunk-K2VU3B4W.js} +99 -27
- package/dist/{chunk-5QKXEWDB.js → chunk-MHKY6TNF.js} +6 -4
- package/dist/chunk-N7ANFMA5.js +171 -0
- package/dist/{chunk-URP3QIWI.js → chunk-NNGGNXF2.js} +2 -2
- package/dist/{chunk-7CSUKCU7.js → chunk-P5D6RKMT.js} +13 -94
- package/dist/{chunk-EA2OSLFJ.js → chunk-QBSJL3SP.js} +42 -31
- package/dist/chunk-SGFTHKKS.js +86 -0
- package/dist/{chunk-Z5RNKEHF.js → chunk-T4GVN7XO.js} +5 -5
- package/dist/chunk-T6C3TSTO.js +99 -0
- package/dist/{chunk-NRBQK6UX.js → chunk-TYY5TYHF.js} +14 -1
- package/dist/{chunk-VEOYGFEQ.js → chunk-UED3RH53.js} +2 -2
- package/dist/{chunk-37RGCHTK.js → chunk-VNZTULFD.js} +3 -3
- package/dist/chunk-VSTUKACC.js +7 -0
- package/dist/{chunk-A4PKLESO.js → chunk-WFY7ATSZ.js} +8 -4
- package/dist/{chunk-KPKMEUUH.js → chunk-XSWF7APE.js} +3 -3
- package/dist/{chunk-GIDKCJVZ.js → chunk-ZKB7ZNKY.js} +4 -4
- package/dist/{chunk-ZEERJLE5.js → chunk-ZS5IDQFJ.js} +6 -6
- package/dist/{cli-JXPJFHUA.js → cli-FSSYFCV3.js} +15 -9
- package/dist/{cli-34EDFXJA.js → cli-IHVNE3WL.js} +9 -9
- package/dist/cli.js +19 -13
- package/dist/{completion-25CHR7SR.js → completion-ZTTJVV4C.js} +8 -6
- package/dist/{diagnostics-4PQSTI2X.js → diagnostics-XC5L6Z3E.js} +11 -7
- package/dist/{doctor-MHFALEWR.js → doctor-FGDSUBR3.js} +21 -17
- package/dist/{entry-25VWXTCW.js → entry-NNB2CQLZ.js} +20 -16
- package/dist/{exec-mcp-OQP5OU26.js → exec-mcp-ZU3Z3XT6.js} +16 -12
- package/dist/{harness-DTXPIWKL.js → harness-B7ANLH5M.js} +5 -5
- package/dist/harness-launch-gate.js +9 -5
- package/dist/harness-worker.js +11 -7
- package/dist/heavy-IIZBO4YM.js +1909 -0
- package/dist/hook-body.js +69 -21
- package/dist/{hooks-E33GKFRS.js → hooks-IEJTW7BV.js} +5 -5
- package/dist/{host-4YG67ZQ2.js → host-5P4D7GSE.js} +18 -14
- package/dist/host-XXWQ23LD.js +28 -0
- package/dist/{host-run-AJR2THEY.js → host-run-AEWHFVQL.js} +11 -7
- package/dist/http.js +1 -1
- package/dist/{import-mcp-MGDRREMJ.js → import-mcp-BQM5DKYH.js} +16 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +18 -14
- package/dist/{init-RMTFPDJQ.js → init-L6GUO3PW.js} +2 -2
- package/dist/{install-7V6NIA7W.js → install-OFVLR7TI.js} +3 -3
- package/dist/invoke-3BQ5NG3P.js +29 -0
- package/dist/{list-WJE6NS7Y.js → list-EU4MJOXA.js} +17 -13
- package/dist/{list-WK2IYUK7.js → list-WWYCGD3X.js} +4 -4
- package/dist/{logout-SZY5GBIH.js → logout-6DFDYJUI.js} +16 -12
- package/dist/{logs-UHJM7ZCL.js → logs-BRF6DBKG.js} +7 -6
- package/dist/{mailbox-YN6PG66A.js → mailbox-AQCTXO3H.js} +3 -3
- package/dist/{mailbox-NIMBOQPA.js → mailbox-QYA2LQSE.js} +9 -41
- package/dist/{mainline-74WCH4T4.js → mainline-O5AEHG3J.js} +69 -121
- package/dist/{marketplace-G4VQET5W.js → marketplace-TI5HFAHB.js} +2 -2
- package/dist/{mcp-AXHV547W.js → mcp-4R53HSGY.js} +2 -2
- package/dist/{mcp-BPTTJKWL.js → mcp-HR5JFUW7.js} +2 -2
- package/dist/{models-ISU4DCMA.js → models-63NQBSV3.js} +2 -2
- package/dist/{pause-CBZ3BTJL.js → pause-UF7FQDJX.js} +6 -5
- package/dist/{preview-DT7DWG5F.js → preview-WSESS2UN.js} +3 -3
- package/dist/{project-6GDQBLDC.js → project-JXXL24WN.js} +6 -6
- package/dist/{reindex-I7RYWCUR.js → reindex-3GPKAFBW.js} +5 -5
- package/dist/{report-ZHOHQ4PZ.js → report-JUSKQPAC.js} +7 -6
- package/dist/{resume-UJEWQ66P.js → resume-3WAROOQ3.js} +6 -5
- package/dist/{run-V4LBDW5O.js → run-VUUQS4WY.js} +5 -5
- package/dist/{run-5UM7WUTH.js → run-XRWU4L73.js} +14 -10
- package/dist/{serve-723OCAUT.js → serve-OEX5V5CD.js} +7 -7
- package/dist/{setup-SOUBEZ3H.js → setup-SWNAC2C7.js} +6 -4
- package/dist/{smoke-2FI5D74B.js → smoke-5BGJXAHX.js} +4 -4
- package/dist/{start-login-M425T4GS.js → start-login-MG26NDE3.js} +16 -12
- package/dist/{status-XAP66PLK.js → status-APADL7O2.js} +16 -12
- package/dist/{status-KN2ZE64O.js → status-GKCIVAAR.js} +6 -5
- package/dist/{status-QEFRBTOO.js → status-LH7Q255G.js} +18 -14
- package/dist/stdio.js +2 -2
- package/dist/{sweep-EPZCW63Q.js → sweep-VTVPDAPY.js} +6 -5
- package/dist/{test-TL7DRDMD.js → test-TK2J3IB7.js} +17 -13
- package/dist/{tui-DGGNATMI.js → tui-YTPHHKCW.js} +5 -5
- package/dist/{unbroadcast-N6LXHAMT.js → unbroadcast-CH7BAQ2G.js} +6 -5
- package/dist/{validate-KFBCUFI4.js → validate-5QVZBE6W.js} +4 -4
- package/dist/wait-4HYJJAWO.js +141 -0
- package/dist/{worker-N7EOJVI3.js → worker-RBXYAMR4.js} +13 -9
- package/dist/{worktree-create-KMXPQXIQ.js → worktree-create-FEZNQUQ6.js} +2 -2
- package/docs/commands.md +48 -12
- package/docs/git-workflow.md +51 -0
- package/docs/hooks.md +8 -1
- package/docs/project-configuration.md +134 -18
- package/docs/upgrading.md +11 -0
- 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-L7LQEVTE.js +0 -7
- package/dist/host-W6B2O44Q.js +0 -24
- package/dist/invoke-JRFOK374.js +0 -25
|
@@ -0,0 +1,1909 @@
|
|
|
1
|
+
import {
|
|
2
|
+
flock
|
|
3
|
+
} from "./chunk-SGFTHKKS.js";
|
|
4
|
+
import {
|
|
5
|
+
inferSession
|
|
6
|
+
} from "./chunk-65BW7Y6O.js";
|
|
7
|
+
import {
|
|
8
|
+
flockAvailable
|
|
9
|
+
} from "./chunk-DZ2MRTYT.js";
|
|
10
|
+
import {
|
|
11
|
+
heavySpec
|
|
12
|
+
} from "./chunk-BN4XAXMQ.js";
|
|
13
|
+
import "./chunk-XNML2KIC.js";
|
|
14
|
+
import {
|
|
15
|
+
ArgumentError,
|
|
16
|
+
EXIT,
|
|
17
|
+
one
|
|
18
|
+
} from "./chunk-WFY7ATSZ.js";
|
|
19
|
+
import {
|
|
20
|
+
BUSY_EXIT,
|
|
21
|
+
DISABLE_ENV,
|
|
22
|
+
NOW_ENV,
|
|
23
|
+
leaseSettingsFor
|
|
24
|
+
} from "./chunk-FOLA4EUJ.js";
|
|
25
|
+
import {
|
|
26
|
+
LANES,
|
|
27
|
+
describeRecord,
|
|
28
|
+
epoch,
|
|
29
|
+
leaseHeld,
|
|
30
|
+
leasePaths,
|
|
31
|
+
liveRecords,
|
|
32
|
+
liveWaiters,
|
|
33
|
+
logEvent,
|
|
34
|
+
newId,
|
|
35
|
+
nowUtc,
|
|
36
|
+
oneLine,
|
|
37
|
+
pidAlive,
|
|
38
|
+
readRecord,
|
|
39
|
+
removeRecord,
|
|
40
|
+
writeRecord
|
|
41
|
+
} from "./chunk-N7ANFMA5.js";
|
|
42
|
+
import "./chunk-T6C3TSTO.js";
|
|
43
|
+
import "./chunk-GUHOEIHP.js";
|
|
44
|
+
import "./chunk-IJBSOWHK.js";
|
|
45
|
+
import {
|
|
46
|
+
ConfigError,
|
|
47
|
+
findConfig
|
|
48
|
+
} from "./chunk-GJXOFDJT.js";
|
|
49
|
+
import "./chunk-PMJG2CXF.js";
|
|
50
|
+
|
|
51
|
+
// src/commands/heavy.ts
|
|
52
|
+
import { dirname } from "path";
|
|
53
|
+
|
|
54
|
+
// src/lease/run.ts
|
|
55
|
+
import { spawn } from "child_process";
|
|
56
|
+
import { constants } from "os";
|
|
57
|
+
|
|
58
|
+
// src/lease/scope.ts
|
|
59
|
+
import { spawnSync as spawnSync2 } from "child_process";
|
|
60
|
+
import { join as join2 } from "path";
|
|
61
|
+
|
|
62
|
+
// src/lease/iocontrol.ts
|
|
63
|
+
import { spawnSync } from "child_process";
|
|
64
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
65
|
+
import { join } from "path";
|
|
66
|
+
function cgroupHost() {
|
|
67
|
+
let self = "";
|
|
68
|
+
try {
|
|
69
|
+
const line = readFileSync("/proc/self/cgroup", "utf8").split("\n").find((entry) => entry.startsWith("0::"));
|
|
70
|
+
self = line?.slice(3) ?? "";
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
return { root: "/sys/fs/cgroup", self };
|
|
74
|
+
}
|
|
75
|
+
function controllers(dir) {
|
|
76
|
+
try {
|
|
77
|
+
return readFileSync(join(dir, "cgroup.controllers"), "utf8").trim().split(/\s+/u);
|
|
78
|
+
} catch {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function ioDelegated(host) {
|
|
83
|
+
const parts = host.self.split("/").filter(Boolean);
|
|
84
|
+
const at = parts.findIndex((part) => /^user@\d+\.service$/u.test(part));
|
|
85
|
+
if (at < 0) return false;
|
|
86
|
+
const manager = join(host.root, ...parts.slice(0, at + 1));
|
|
87
|
+
return controllers(manager).includes("io");
|
|
88
|
+
}
|
|
89
|
+
function enable(dir) {
|
|
90
|
+
const file = join(dir, "cgroup.subtree_control");
|
|
91
|
+
try {
|
|
92
|
+
if (readFileSync(file, "utf8").split(/\s+/u).includes("io")) return;
|
|
93
|
+
writeFileSync(file, "+io");
|
|
94
|
+
} catch {
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function applyIo(host, scope, weight, max) {
|
|
98
|
+
const parts = scope.split("/").filter(Boolean);
|
|
99
|
+
for (let at = 1; at < parts.length; at += 1)
|
|
100
|
+
enable(join(host.root, ...parts.slice(0, at)));
|
|
101
|
+
const dir = join(host.root, ...parts);
|
|
102
|
+
if (!controllers(dir).includes("io")) return false;
|
|
103
|
+
try {
|
|
104
|
+
writeFileSync(join(dir, "io.weight"), `default ${weight}`);
|
|
105
|
+
if (max) writeFileSync(join(dir, "io.max"), max);
|
|
106
|
+
return true;
|
|
107
|
+
} catch {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function scopeCgroup(unit) {
|
|
112
|
+
const run2 = spawnSync(
|
|
113
|
+
"systemctl",
|
|
114
|
+
["--user", "show", unit, "-p", "ControlGroup", "--value"],
|
|
115
|
+
{ encoding: "utf8" }
|
|
116
|
+
);
|
|
117
|
+
const path = run2.status === 0 ? run2.stdout.trim() : "";
|
|
118
|
+
return path || null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/lease/scope.ts
|
|
122
|
+
var scopeSeen;
|
|
123
|
+
function scopeAvailable() {
|
|
124
|
+
scopeSeen ??= process.platform === "linux" && spawnSync2("systemd-run", ["--user", "--version"], { stdio: "ignore" }).status === 0;
|
|
125
|
+
return scopeSeen;
|
|
126
|
+
}
|
|
127
|
+
function wantsIo(settings2, lane, host) {
|
|
128
|
+
if (lane === null || settings2.io.enabled === false) return false;
|
|
129
|
+
return ioDelegated(host);
|
|
130
|
+
}
|
|
131
|
+
async function weigh(unit, settings2, lane, host, say, onCgroup) {
|
|
132
|
+
for (let tries = 0; tries < 30; tries += 1) {
|
|
133
|
+
const scope = scopeCgroup(unit);
|
|
134
|
+
if (scope !== null) {
|
|
135
|
+
onCgroup(join2(host.root, scope));
|
|
136
|
+
const set = applyIo(
|
|
137
|
+
host,
|
|
138
|
+
scope,
|
|
139
|
+
settings2.io.weights[lane],
|
|
140
|
+
settings2.io.max[lane]
|
|
141
|
+
);
|
|
142
|
+
if (!set && settings2.io.enabled === true)
|
|
143
|
+
say(`io control did not reach ${scope}; running without io.weight`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function scoped(argv, settings2, lane, memoryMax, say, onCgroup = () => {
|
|
150
|
+
}, host = cgroupHost()) {
|
|
151
|
+
const io2 = wantsIo(settings2, lane, host);
|
|
152
|
+
if (memoryMax === void 0 && !io2) return { argv };
|
|
153
|
+
if (!scopeAvailable()) {
|
|
154
|
+
if (memoryMax !== void 0)
|
|
155
|
+
say(
|
|
156
|
+
`systemd-run --user is unavailable; running without MemoryMax=${memoryMax}`
|
|
157
|
+
);
|
|
158
|
+
return { argv };
|
|
159
|
+
}
|
|
160
|
+
const unit = `pmcp-heavy-${process.pid}-${Date.now()}`;
|
|
161
|
+
const limits = memoryMax === void 0 ? [] : ["-p", `MemoryMax=${memoryMax}`, "-p", "MemorySwapMax=0"];
|
|
162
|
+
return {
|
|
163
|
+
argv: [
|
|
164
|
+
"systemd-run",
|
|
165
|
+
"--user",
|
|
166
|
+
"--scope",
|
|
167
|
+
"-q",
|
|
168
|
+
`--unit=${unit}`,
|
|
169
|
+
...limits,
|
|
170
|
+
"--",
|
|
171
|
+
...argv
|
|
172
|
+
],
|
|
173
|
+
...io2 && lane !== null ? { started: () => weigh(unit, settings2, lane, host, say, onCgroup) } : {}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/lease/acquire.ts
|
|
178
|
+
import {
|
|
179
|
+
closeSync as closeSync2,
|
|
180
|
+
mkdirSync,
|
|
181
|
+
openSync as openSync2,
|
|
182
|
+
readFileSync as readFileSync4,
|
|
183
|
+
rmSync,
|
|
184
|
+
writeFileSync as writeFileSync3
|
|
185
|
+
} from "fs";
|
|
186
|
+
|
|
187
|
+
// src/lease/admit.ts
|
|
188
|
+
import { closeSync, openSync } from "fs";
|
|
189
|
+
import { join as join3 } from "path";
|
|
190
|
+
function admissible(settings2, running, waiters) {
|
|
191
|
+
const free = { ...settings2.slots };
|
|
192
|
+
let budget = settings2.budget;
|
|
193
|
+
for (const job2 of running) {
|
|
194
|
+
if (job2.weight === 0) continue;
|
|
195
|
+
free[job2.lane] -= 1;
|
|
196
|
+
budget -= job2.weight;
|
|
197
|
+
}
|
|
198
|
+
const found = new Set(
|
|
199
|
+
waiters.filter((waiter) => waiter.weight === 0).map((w) => w.pid)
|
|
200
|
+
);
|
|
201
|
+
for (const waiter of waiters) {
|
|
202
|
+
if (waiter.weight === 0 || free[waiter.lane] <= 0) continue;
|
|
203
|
+
if (waiter.weight > budget) break;
|
|
204
|
+
free[waiter.lane] -= 1;
|
|
205
|
+
budget -= waiter.weight;
|
|
206
|
+
found.add(waiter.pid);
|
|
207
|
+
}
|
|
208
|
+
return found;
|
|
209
|
+
}
|
|
210
|
+
function runningWeight(running) {
|
|
211
|
+
return running.reduce((sum, job2) => sum + job2.weight, 0);
|
|
212
|
+
}
|
|
213
|
+
function laneUse(settings2, running) {
|
|
214
|
+
const used = (lane) => running.filter((job2) => job2.lane === lane).length;
|
|
215
|
+
const lanes = LANES.map(
|
|
216
|
+
(lane) => `${lane} ${used(lane)}/${settings2.slots[lane]}`
|
|
217
|
+
).join(" ");
|
|
218
|
+
return `${lanes}, weight ${runningWeight(running)}/${settings2.budget}`;
|
|
219
|
+
}
|
|
220
|
+
function underAdmitLock(paths, act) {
|
|
221
|
+
const fd = openSync(paths.admit, "a");
|
|
222
|
+
try {
|
|
223
|
+
if (flock(fd, ["-w", "10"]) !== 0) return null;
|
|
224
|
+
return act();
|
|
225
|
+
} finally {
|
|
226
|
+
closeSync(fd);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function readQueue(paths, prune2 = true) {
|
|
230
|
+
return {
|
|
231
|
+
running: liveRecords(paths.running, prune2),
|
|
232
|
+
waiters: liveRecords(paths.waiters, prune2)
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function runningFile(paths, pid) {
|
|
236
|
+
return join3(paths.running, String(pid));
|
|
237
|
+
}
|
|
238
|
+
function waiterFile(paths, pid) {
|
|
239
|
+
return join3(paths.waiters, String(pid));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/lease/host.ts
|
|
243
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
244
|
+
function hostSources(env) {
|
|
245
|
+
return {
|
|
246
|
+
loadavg: env["PMCP_HEAVY_LOADAVG_FILE"] || "/proc/loadavg",
|
|
247
|
+
pressure: env["PMCP_HEAVY_PRESSURE_FILE"] || "/proc/pressure/io"
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function read(file) {
|
|
251
|
+
try {
|
|
252
|
+
return readFileSync2(file, "utf8");
|
|
253
|
+
} catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function readHost(sources) {
|
|
258
|
+
const load = read(sources.loadavg)?.split(" ")[0]?.trim() || null;
|
|
259
|
+
const some = read(sources.pressure)?.match(/^some (.*)$/mu)?.[1] ?? "";
|
|
260
|
+
const pressure = {};
|
|
261
|
+
for (const [, key, value] of some.matchAll(/(avg\d+)=([0-9.]+)/gu))
|
|
262
|
+
pressure[key] = value;
|
|
263
|
+
return { load, io: pressure["avg10"] ?? null, pressure };
|
|
264
|
+
}
|
|
265
|
+
function hostNow(reading) {
|
|
266
|
+
return `load ${reading.load ?? "n/a"} io ${reading.io ?? "n/a"}`;
|
|
267
|
+
}
|
|
268
|
+
function below(value, max) {
|
|
269
|
+
return value === null || Number(value) < max;
|
|
270
|
+
}
|
|
271
|
+
function hostSettled(settings2, paths, sources) {
|
|
272
|
+
const reading = readHost(sources);
|
|
273
|
+
if (!below(reading.load, settings2.maxLoad)) return false;
|
|
274
|
+
const io2 = reading.pressure[settings2.ioWindow] ?? reading.io;
|
|
275
|
+
if (!below(io2, settings2.maxIo)) return false;
|
|
276
|
+
try {
|
|
277
|
+
writeFileSync2(paths.settled, `${nowUtc()}
|
|
278
|
+
`);
|
|
279
|
+
} catch {
|
|
280
|
+
}
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
function sleepSeconds(seconds) {
|
|
284
|
+
if (seconds <= 0) return;
|
|
285
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, seconds * 1e3);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/lease/queue.ts
|
|
289
|
+
import { readFileSync as readFileSync3 } from "fs";
|
|
290
|
+
function sessionOf(env, cwd) {
|
|
291
|
+
const inferred = inferSession(env, [], []).session;
|
|
292
|
+
if (inferred) return inferred;
|
|
293
|
+
try {
|
|
294
|
+
const stat = readFileSync3("/proc/self/stat", "utf8");
|
|
295
|
+
const group = stat.slice(stat.lastIndexOf(")") + 2).split(" ")[2];
|
|
296
|
+
if (group) return `pgid:${group}`;
|
|
297
|
+
} catch {
|
|
298
|
+
}
|
|
299
|
+
return `cwd:${cwd}`;
|
|
300
|
+
}
|
|
301
|
+
var CAPPED = ["interactive", "bulk"];
|
|
302
|
+
function parkForSession(park) {
|
|
303
|
+
if (park.cap === 0 || !CAPPED.includes(park.lane)) return 0;
|
|
304
|
+
const started = epoch();
|
|
305
|
+
const queued = () => liveWaiters(park.paths).filter(
|
|
306
|
+
(record2) => record2.session === park.session && record2.lane === park.lane
|
|
307
|
+
).length;
|
|
308
|
+
let count = queued();
|
|
309
|
+
if (count < park.cap) return 0;
|
|
310
|
+
park.say(
|
|
311
|
+
`parked outside the queue: this session already has ${count} queued ${park.lane} job(s) (per_session ${park.cap})`
|
|
312
|
+
);
|
|
313
|
+
for (; ; ) {
|
|
314
|
+
if (epoch() - started >= park.maxWait) return null;
|
|
315
|
+
sleepSeconds(1);
|
|
316
|
+
count = queued();
|
|
317
|
+
if (count < park.cap) return epoch() - started;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/lease/settle.ts
|
|
322
|
+
function io(settings2, reading) {
|
|
323
|
+
return reading.pressure[settings2.ioWindow] ?? reading.io;
|
|
324
|
+
}
|
|
325
|
+
function over(value, max) {
|
|
326
|
+
return value !== null && Number(value) >= max;
|
|
327
|
+
}
|
|
328
|
+
function settled(settings2, reading) {
|
|
329
|
+
return !over(reading.load, settings2.maxLoad) && !over(io(settings2, reading), settings2.maxIo);
|
|
330
|
+
}
|
|
331
|
+
function severe(settings2, reading) {
|
|
332
|
+
const factor = settings2.hardFactor;
|
|
333
|
+
return over(reading.load, settings2.maxLoad * factor) || over(io(settings2, reading), settings2.maxIo * factor);
|
|
334
|
+
}
|
|
335
|
+
function hostVerdict(settings2, reading, waiting) {
|
|
336
|
+
if (settled(settings2, reading)) return "settled";
|
|
337
|
+
if (severe(settings2, reading)) return "severe";
|
|
338
|
+
if (waiting.lane === "hook") return "hook";
|
|
339
|
+
if (waiting.weight === 0) return "tiny";
|
|
340
|
+
const window = settings2.settleWindow;
|
|
341
|
+
if (window > 0 && waiting.sinceRelease !== null && waiting.sinceRelease >= window)
|
|
342
|
+
return "settle";
|
|
343
|
+
if (waiting.grace > 0 && waiting.waited >= waiting.grace && waiting.runningWeight === 0)
|
|
344
|
+
return "grace";
|
|
345
|
+
return "wait";
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/lease/acquire.ts
|
|
349
|
+
var STEP_SECONDS = 1;
|
|
350
|
+
function skipReason(settings2, env) {
|
|
351
|
+
const own = env[DISABLE_ENV];
|
|
352
|
+
if (own) return own;
|
|
353
|
+
const legacy = settings2.legacySkipEnv ? env[settings2.legacySkipEnv] : "";
|
|
354
|
+
return legacy || null;
|
|
355
|
+
}
|
|
356
|
+
function reentrant(settings2, env) {
|
|
357
|
+
const paths = leasePaths(settings2.dir);
|
|
358
|
+
const names = [settings2.markerEnv, settings2.legacyMarkerEnv];
|
|
359
|
+
const markers = names.flatMap(
|
|
360
|
+
(name) => name && env[name] ? [env[name]] : []
|
|
361
|
+
);
|
|
362
|
+
if (markers.length === 0) return false;
|
|
363
|
+
if (liveRecords(paths.running).some((job2) => markers.includes(String(job2.pid))))
|
|
364
|
+
return true;
|
|
365
|
+
const holder = readRecord(paths.holder);
|
|
366
|
+
if (holder === null || !markers.includes(String(holder.pid))) return false;
|
|
367
|
+
return pidAlive(holder.pid) && leaseHeld(paths);
|
|
368
|
+
}
|
|
369
|
+
function profileLane(profile) {
|
|
370
|
+
if (profile === "gate" || profile === "hook") return profile;
|
|
371
|
+
return "interactive";
|
|
372
|
+
}
|
|
373
|
+
function position(paths, pid) {
|
|
374
|
+
const index = liveRecords(paths.waiters).findIndex((r) => r.pid === pid);
|
|
375
|
+
return index < 0 ? 0 : index + 1;
|
|
376
|
+
}
|
|
377
|
+
function tryAdmit(options, paths, fd, start, registered) {
|
|
378
|
+
let fields = start;
|
|
379
|
+
const pid = process.pid;
|
|
380
|
+
const outcome = underAdmitLock(paths, () => {
|
|
381
|
+
const mine = readRecord(waiterFile(paths, pid));
|
|
382
|
+
if (mine === null || mine.cancel)
|
|
383
|
+
return {
|
|
384
|
+
admitted: false,
|
|
385
|
+
verdict: "cancelled",
|
|
386
|
+
reason: mine?.cancel || "removed"
|
|
387
|
+
};
|
|
388
|
+
fields = { ...fields, lane: mine.lane };
|
|
389
|
+
if (mine.promote) {
|
|
390
|
+
flock(fd, ["-s", "-n"]);
|
|
391
|
+
writeRecord(runningFile(paths, pid), pid, {
|
|
392
|
+
...fields,
|
|
393
|
+
bypass: mine.promote
|
|
394
|
+
});
|
|
395
|
+
writeRecord(paths.holder, pid, fields);
|
|
396
|
+
rmSync(waiterFile(paths, pid), { force: true });
|
|
397
|
+
logEvent(
|
|
398
|
+
paths,
|
|
399
|
+
pid,
|
|
400
|
+
"bypass",
|
|
401
|
+
`reason=${mine.promote} cwd=${fields.cwd} label=${fields.label} lane=${fields.lane} weight=${fields.weight} id=${fields.id}`
|
|
402
|
+
);
|
|
403
|
+
return { admitted: true, verdict: "promoted", lane: fields.lane };
|
|
404
|
+
}
|
|
405
|
+
const { running, waiters } = readQueue(paths);
|
|
406
|
+
if (!admissible(options.settings, running, waiters).has(pid))
|
|
407
|
+
return { admitted: false, verdict: "queue" };
|
|
408
|
+
const grace = options.settings.profiles[options.profile].loadGrace;
|
|
409
|
+
const reading = readHost(hostSources(options.env));
|
|
410
|
+
const released = lastRelease(paths);
|
|
411
|
+
const verdict = hostVerdict(options.settings, reading, {
|
|
412
|
+
lane: fields.lane,
|
|
413
|
+
weight: fields.weight,
|
|
414
|
+
grace,
|
|
415
|
+
waited: epoch() - registered,
|
|
416
|
+
// Only a release while this job waited counts: older ones explain nothing now.
|
|
417
|
+
sinceRelease: released !== null && released >= registered ? epoch() - released : null,
|
|
418
|
+
runningWeight: runningWeight(running)
|
|
419
|
+
});
|
|
420
|
+
if (verdict === "wait" || verdict === "severe")
|
|
421
|
+
return { admitted: false, verdict };
|
|
422
|
+
if (flock(fd, ["-s", "-n"]) !== 0)
|
|
423
|
+
return { admitted: false, verdict: "legacy" };
|
|
424
|
+
writeRecord(runningFile(paths, pid), pid, {
|
|
425
|
+
...fields,
|
|
426
|
+
queued: Date.now(),
|
|
427
|
+
grace: verdict === "grace"
|
|
428
|
+
});
|
|
429
|
+
writeRecord(paths.holder, pid, fields);
|
|
430
|
+
rmSync(waiterFile(paths, pid), { force: true });
|
|
431
|
+
if (verdict !== "settled")
|
|
432
|
+
logEvent(
|
|
433
|
+
paths,
|
|
434
|
+
pid,
|
|
435
|
+
verdict,
|
|
436
|
+
`waited=${epoch() - registered} ${hostNow(reading)} label=${fields.label}`
|
|
437
|
+
);
|
|
438
|
+
return { admitted: true, verdict, lane: fields.lane };
|
|
439
|
+
});
|
|
440
|
+
return outcome ?? { admitted: false, verdict: "queue" };
|
|
441
|
+
}
|
|
442
|
+
function lastRelease(paths) {
|
|
443
|
+
try {
|
|
444
|
+
return Number(readFileSync4(paths.released, "utf8").trim()) || null;
|
|
445
|
+
} catch {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function markRelease(paths) {
|
|
450
|
+
try {
|
|
451
|
+
writeFileSync3(paths.released, `${epoch()}
|
|
452
|
+
`);
|
|
453
|
+
} catch {
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function release(paths, pid, fd, rc, ran) {
|
|
457
|
+
underAdmitLock(paths, () => {
|
|
458
|
+
rmSync(runningFile(paths, pid), { force: true });
|
|
459
|
+
removeRecord(paths.holder, pid);
|
|
460
|
+
markRelease(paths);
|
|
461
|
+
const next = liveRecords(paths.running)[0];
|
|
462
|
+
if (next && readRecord(paths.holder) === null)
|
|
463
|
+
writeRecord(paths.holder, next.pid, { ...next, queued: next.queued });
|
|
464
|
+
});
|
|
465
|
+
logEvent(paths, pid, "released", `ran=${ran} rc=${rc}`);
|
|
466
|
+
closeSync2(fd);
|
|
467
|
+
}
|
|
468
|
+
function held(options, paths, fd, start) {
|
|
469
|
+
const { settings: settings2 } = options;
|
|
470
|
+
const pid = String(process.pid);
|
|
471
|
+
const env = { [settings2.markerEnv]: pid };
|
|
472
|
+
if (settings2.legacyMarkerEnv) env[settings2.legacyMarkerEnv] = pid;
|
|
473
|
+
if (settings2.legacySkipEnv)
|
|
474
|
+
env[settings2.legacySkipEnv] = `inside pmcp heavy ${pid}`;
|
|
475
|
+
let open = true;
|
|
476
|
+
return {
|
|
477
|
+
kind: "held",
|
|
478
|
+
env,
|
|
479
|
+
start,
|
|
480
|
+
release(rc, ran) {
|
|
481
|
+
if (!open) return;
|
|
482
|
+
open = false;
|
|
483
|
+
release(paths, process.pid, fd, rc, ran);
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function acquireLease(options) {
|
|
488
|
+
const { settings: settings2, env, label, say } = options;
|
|
489
|
+
if (!settings2.enabled) return { kind: "disabled" };
|
|
490
|
+
const paths = leasePaths(settings2.dir);
|
|
491
|
+
const skip = skipReason(settings2, env);
|
|
492
|
+
if (skip !== null) {
|
|
493
|
+
say(`running without the host lease: ${skip}`);
|
|
494
|
+
logEvent(
|
|
495
|
+
paths,
|
|
496
|
+
process.pid,
|
|
497
|
+
"skip",
|
|
498
|
+
`reason=${skip} cwd=${options.cwd} label=${label}`
|
|
499
|
+
);
|
|
500
|
+
return { kind: "skipped", reason: skip };
|
|
501
|
+
}
|
|
502
|
+
if (!flockAvailable()) {
|
|
503
|
+
say("flock is not installed; running without the host lease");
|
|
504
|
+
return { kind: "skipped", reason: "flock is not installed" };
|
|
505
|
+
}
|
|
506
|
+
if (reentrant(settings2, env)) return { kind: "reentrant" };
|
|
507
|
+
const profile = settings2.profiles[options.profile];
|
|
508
|
+
if (profile === void 0)
|
|
509
|
+
throw new Error(`unknown profile ${options.profile}`);
|
|
510
|
+
const lane = options.lane ?? profileLane(options.profile);
|
|
511
|
+
const weight = Math.min(
|
|
512
|
+
options.weight ?? settings2.weights[lane],
|
|
513
|
+
settings2.budget
|
|
514
|
+
);
|
|
515
|
+
const session = sessionOf(env, options.cwd);
|
|
516
|
+
const fields = {
|
|
517
|
+
cwd: options.cwd,
|
|
518
|
+
label,
|
|
519
|
+
lane,
|
|
520
|
+
weight,
|
|
521
|
+
session,
|
|
522
|
+
id: options.id ?? newId()
|
|
523
|
+
};
|
|
524
|
+
mkdirSync(paths.waiters, { recursive: true });
|
|
525
|
+
mkdirSync(paths.running, { recursive: true });
|
|
526
|
+
if (options.now) return bypass(options, paths, fields);
|
|
527
|
+
const started = epoch();
|
|
528
|
+
const deadline = started + profile.maxWait;
|
|
529
|
+
const parked = parkForSession({
|
|
530
|
+
paths,
|
|
531
|
+
lane,
|
|
532
|
+
session,
|
|
533
|
+
cap: settings2.perSession,
|
|
534
|
+
maxWait: profile.maxWait,
|
|
535
|
+
say
|
|
536
|
+
});
|
|
537
|
+
if (parked === null) return busy(options, paths, lane, 0, "parked", started);
|
|
538
|
+
const fd = openSync2(paths.lock, "a");
|
|
539
|
+
writeRecord(waiterFile(paths, process.pid), process.pid, fields);
|
|
540
|
+
const registered = epoch();
|
|
541
|
+
let beat = 0;
|
|
542
|
+
let last = "queue";
|
|
543
|
+
let admittedLane = lane;
|
|
544
|
+
try {
|
|
545
|
+
for (; ; ) {
|
|
546
|
+
const attempt = tryAdmit(options, paths, fd, fields, registered);
|
|
547
|
+
if (attempt.admitted) {
|
|
548
|
+
last = attempt.verdict;
|
|
549
|
+
admittedLane = attempt.lane ?? lane;
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
if (attempt.verdict === "cancelled") {
|
|
553
|
+
closeSync2(fd);
|
|
554
|
+
say(`cancelled in the queue: ${attempt.reason}`);
|
|
555
|
+
return { kind: "cancelled", reason: attempt.reason ?? "" };
|
|
556
|
+
}
|
|
557
|
+
if (last === "queue" && attempt.verdict !== "queue")
|
|
558
|
+
say(
|
|
559
|
+
`next in line; ${describeWait(attempt.verdict, hostNow(readHost(hostSources(env))))}`
|
|
560
|
+
);
|
|
561
|
+
last = attempt.verdict;
|
|
562
|
+
if (epoch() >= deadline) {
|
|
563
|
+
closeSync2(fd);
|
|
564
|
+
const place = position(paths, process.pid);
|
|
565
|
+
const reason2 = last === "severe" ? "host" : last === "legacy" ? "legacy" : "queue";
|
|
566
|
+
return busy(options, paths, lane, place, reason2, started);
|
|
567
|
+
}
|
|
568
|
+
if (epoch() - beat >= settings2.heartbeat) {
|
|
569
|
+
const { running } = readQueue(paths);
|
|
570
|
+
say(
|
|
571
|
+
`waiting ${epoch() - started}s; ${lane} position ${position(paths, process.pid)}, running weight ${runningWeight(running)}/${settings2.budget}, ${hostNow(readHost(hostSources(env)))}`
|
|
572
|
+
);
|
|
573
|
+
beat = epoch();
|
|
574
|
+
}
|
|
575
|
+
sleepSeconds(Math.min(STEP_SECONDS, deadline - epoch()));
|
|
576
|
+
}
|
|
577
|
+
} finally {
|
|
578
|
+
rmSync(waiterFile(paths, process.pid), { force: true });
|
|
579
|
+
}
|
|
580
|
+
hostSettled(settings2, paths, hostSources(env));
|
|
581
|
+
logEvent(
|
|
582
|
+
paths,
|
|
583
|
+
process.pid,
|
|
584
|
+
"acquired",
|
|
585
|
+
`waited=${epoch() - started} cwd=${options.cwd} label=${label} lane=${admittedLane} weight=${weight}`
|
|
586
|
+
);
|
|
587
|
+
const start = last === "promoted" ? "bypass" : last === "grace" ? "grace" : "queue";
|
|
588
|
+
return held(options, paths, fd, start);
|
|
589
|
+
}
|
|
590
|
+
function bypass(options, paths, fields) {
|
|
591
|
+
const fd = openSync2(paths.lock, "a");
|
|
592
|
+
const reason2 = options.now;
|
|
593
|
+
underAdmitLock(paths, () => {
|
|
594
|
+
flock(fd, ["-s", "-n"]);
|
|
595
|
+
writeRecord(runningFile(paths, process.pid), process.pid, {
|
|
596
|
+
...fields,
|
|
597
|
+
bypass: reason2
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
options.say(
|
|
601
|
+
`BYPASS: running now, outside the queue and the host check: ${reason2}`
|
|
602
|
+
);
|
|
603
|
+
logEvent(
|
|
604
|
+
paths,
|
|
605
|
+
process.pid,
|
|
606
|
+
"bypass",
|
|
607
|
+
`reason=${reason2} cwd=${fields.cwd} label=${fields.label} lane=${fields.lane} weight=${fields.weight}`
|
|
608
|
+
);
|
|
609
|
+
return held(options, paths, fd, "bypass");
|
|
610
|
+
}
|
|
611
|
+
function describeWait(verdict, host) {
|
|
612
|
+
if (verdict === "severe")
|
|
613
|
+
return `the host is at hard_factor x a limit (${host})`;
|
|
614
|
+
if (verdict === "wait") return `waiting for the host to settle (${host})`;
|
|
615
|
+
if (verdict === "legacy") return "a shell wrapper holds the whole lease";
|
|
616
|
+
return "waiting";
|
|
617
|
+
}
|
|
618
|
+
function busy(options, paths, lane, place, reason2, started) {
|
|
619
|
+
const profile = options.settings.profiles[options.profile];
|
|
620
|
+
const host = hostNow(readHost(hostSources(options.env)));
|
|
621
|
+
logEvent(
|
|
622
|
+
paths,
|
|
623
|
+
process.pid,
|
|
624
|
+
"timeout",
|
|
625
|
+
`waited=${profile.maxWait} label=${options.label} lane=${lane} position=${place} reason=${reason2}`
|
|
626
|
+
);
|
|
627
|
+
return {
|
|
628
|
+
kind: "busy",
|
|
629
|
+
waited: epoch() - started,
|
|
630
|
+
lane,
|
|
631
|
+
position: place,
|
|
632
|
+
reason: reason2,
|
|
633
|
+
host
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// src/lease/usage.ts
|
|
638
|
+
import {
|
|
639
|
+
appendFileSync,
|
|
640
|
+
readFileSync as readFileSync5,
|
|
641
|
+
readdirSync,
|
|
642
|
+
renameSync,
|
|
643
|
+
statSync
|
|
644
|
+
} from "fs";
|
|
645
|
+
import { join as join4, relative } from "path";
|
|
646
|
+
var WORD = /^[a-z][a-z0-9:_-]{0,39}$/u;
|
|
647
|
+
function signature(argv) {
|
|
648
|
+
const [program = "", ...rest] = argv;
|
|
649
|
+
const words = [program.slice(program.lastIndexOf("/") + 1)];
|
|
650
|
+
for (const word of rest) {
|
|
651
|
+
if (!WORD.test(word)) break;
|
|
652
|
+
words.push(word);
|
|
653
|
+
}
|
|
654
|
+
return words.join(" ");
|
|
655
|
+
}
|
|
656
|
+
function packageDir(cwd, root) {
|
|
657
|
+
const path = relative(root, cwd);
|
|
658
|
+
return path === "" ? "." : path.startsWith("..") ? "(outside)" : path;
|
|
659
|
+
}
|
|
660
|
+
function appendUsage(file, usage, cap) {
|
|
661
|
+
try {
|
|
662
|
+
if (statSync(file).size >= cap) renameSync(file, `${file}.1`);
|
|
663
|
+
} catch {
|
|
664
|
+
}
|
|
665
|
+
try {
|
|
666
|
+
appendFileSync(file, `${JSON.stringify(usage)}
|
|
667
|
+
`);
|
|
668
|
+
} catch {
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function readUsage(file) {
|
|
672
|
+
const found = [];
|
|
673
|
+
for (const path of [`${file}.1`, file]) {
|
|
674
|
+
let text = "";
|
|
675
|
+
try {
|
|
676
|
+
text = readFileSync5(path, "utf8");
|
|
677
|
+
} catch {
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
for (const line of text.split("\n")) {
|
|
681
|
+
if (!line.trim()) continue;
|
|
682
|
+
try {
|
|
683
|
+
found.push(JSON.parse(line));
|
|
684
|
+
} catch {
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return found;
|
|
689
|
+
}
|
|
690
|
+
var METRICS = [
|
|
691
|
+
"wall_s",
|
|
692
|
+
"user_s",
|
|
693
|
+
"sys_s",
|
|
694
|
+
"max_rss_kb",
|
|
695
|
+
"read_bytes",
|
|
696
|
+
"write_bytes"
|
|
697
|
+
];
|
|
698
|
+
function percentile(values, share) {
|
|
699
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
700
|
+
return sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * share))] ?? 0;
|
|
701
|
+
}
|
|
702
|
+
function usageStats(records) {
|
|
703
|
+
const groups = /* @__PURE__ */ new Map();
|
|
704
|
+
for (const record2 of records) {
|
|
705
|
+
const key = `${record2.signature} @ ${record2.dir}`;
|
|
706
|
+
groups.set(key, [...groups.get(key) ?? [], record2]);
|
|
707
|
+
}
|
|
708
|
+
return [...groups.entries()].map(([key, list]) => {
|
|
709
|
+
const ran = list.filter((record2) => record2.outcome === "ran");
|
|
710
|
+
const pick = (share) => Object.fromEntries(
|
|
711
|
+
METRICS.map((metric) => [
|
|
712
|
+
metric,
|
|
713
|
+
percentile(
|
|
714
|
+
ran.map((record2) => record2[metric]),
|
|
715
|
+
share
|
|
716
|
+
)
|
|
717
|
+
])
|
|
718
|
+
);
|
|
719
|
+
return {
|
|
720
|
+
signature: key,
|
|
721
|
+
samples: ran.length,
|
|
722
|
+
failed: ran.filter((record2) => record2.exit !== 0).length,
|
|
723
|
+
timeouts: list.length - ran.length,
|
|
724
|
+
p50: pick(0.5),
|
|
725
|
+
p90: pick(0.9)
|
|
726
|
+
};
|
|
727
|
+
}).sort(
|
|
728
|
+
(a, b) => b.samples - a.samples || a.signature.localeCompare(b.signature)
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
var Sampler = class {
|
|
732
|
+
constructor(proc = "/proc") {
|
|
733
|
+
this.proc = proc;
|
|
734
|
+
this.before = this.children();
|
|
735
|
+
}
|
|
736
|
+
proc;
|
|
737
|
+
ticks = 100;
|
|
738
|
+
before;
|
|
739
|
+
peakRss = 0;
|
|
740
|
+
io = /* @__PURE__ */ new Map();
|
|
741
|
+
cgroup = null;
|
|
742
|
+
cgroupIo = { read: 0, write: 0 };
|
|
743
|
+
cgroupPeak = 0;
|
|
744
|
+
children() {
|
|
745
|
+
try {
|
|
746
|
+
const stat = readFileSync5(`${this.proc}/self/stat`, "utf8");
|
|
747
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
748
|
+
return { user: Number(fields[13]), sys: Number(fields[14]) };
|
|
749
|
+
} catch {
|
|
750
|
+
return { user: 0, sys: 0 };
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
useCgroup(dir) {
|
|
754
|
+
this.cgroup = dir;
|
|
755
|
+
}
|
|
756
|
+
sample(root) {
|
|
757
|
+
if (this.cgroup) this.sampleCgroup(this.cgroup);
|
|
758
|
+
if (root === void 0) return;
|
|
759
|
+
const parents = /* @__PURE__ */ new Map();
|
|
760
|
+
let names = [];
|
|
761
|
+
try {
|
|
762
|
+
names = readdirSync(this.proc).filter((name) => /^\d+$/u.test(name));
|
|
763
|
+
} catch {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
for (const name of names) {
|
|
767
|
+
try {
|
|
768
|
+
const stat = readFileSync5(`${this.proc}/${name}/stat`, "utf8");
|
|
769
|
+
parents.set(
|
|
770
|
+
Number(name),
|
|
771
|
+
Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1])
|
|
772
|
+
);
|
|
773
|
+
} catch {
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
const inTree = (pid) => {
|
|
777
|
+
for (let at = pid, hops = 0; at > 1 && hops < 64; hops += 1) {
|
|
778
|
+
if (at === root) return true;
|
|
779
|
+
at = parents.get(at) ?? 0;
|
|
780
|
+
}
|
|
781
|
+
return false;
|
|
782
|
+
};
|
|
783
|
+
let rss = 0;
|
|
784
|
+
for (const pid of parents.keys()) {
|
|
785
|
+
if (!inTree(pid)) continue;
|
|
786
|
+
try {
|
|
787
|
+
const status = readFileSync5(`${this.proc}/${pid}/status`, "utf8");
|
|
788
|
+
rss += Number(status.match(/^VmRSS:\s+(\d+)/mu)?.[1] ?? 0);
|
|
789
|
+
const io2 = readFileSync5(`${this.proc}/${pid}/io`, "utf8");
|
|
790
|
+
const value = (key) => Number(io2.match(new RegExp(`^${key}: (\\d+)`, "mu"))?.[1] ?? 0);
|
|
791
|
+
this.io.set(pid, {
|
|
792
|
+
read: value("read_bytes"),
|
|
793
|
+
write: value("write_bytes")
|
|
794
|
+
});
|
|
795
|
+
} catch {
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
this.peakRss = Math.max(this.peakRss, rss);
|
|
799
|
+
}
|
|
800
|
+
sampleCgroup(dir) {
|
|
801
|
+
try {
|
|
802
|
+
const peak = Number(
|
|
803
|
+
readFileSync5(join4(dir, "memory.peak"), "utf8").trim()
|
|
804
|
+
);
|
|
805
|
+
if (peak > 0)
|
|
806
|
+
this.cgroupPeak = Math.max(this.cgroupPeak, Math.round(peak / 1024));
|
|
807
|
+
} catch {
|
|
808
|
+
}
|
|
809
|
+
try {
|
|
810
|
+
let read3 = 0;
|
|
811
|
+
let write = 0;
|
|
812
|
+
for (const line of readFileSync5(join4(dir, "io.stat"), "utf8").split(
|
|
813
|
+
"\n"
|
|
814
|
+
)) {
|
|
815
|
+
read3 += Number(line.match(/rbytes=(\d+)/u)?.[1] ?? 0);
|
|
816
|
+
write += Number(line.match(/wbytes=(\d+)/u)?.[1] ?? 0);
|
|
817
|
+
}
|
|
818
|
+
this.cgroupIo = { read: read3, write };
|
|
819
|
+
} catch {
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
finish() {
|
|
823
|
+
const after = this.children();
|
|
824
|
+
const cpu = {
|
|
825
|
+
user_s: Math.max(0, after.user - this.before.user) / this.ticks,
|
|
826
|
+
sys_s: Math.max(0, after.sys - this.before.sys) / this.ticks
|
|
827
|
+
};
|
|
828
|
+
if (this.cgroup && (this.cgroupPeak > 0 || this.cgroupIo.read + this.cgroupIo.write > 0))
|
|
829
|
+
return {
|
|
830
|
+
...cpu,
|
|
831
|
+
max_rss_kb: this.cgroupPeak || this.peakRss,
|
|
832
|
+
read_bytes: this.cgroupIo.read,
|
|
833
|
+
write_bytes: this.cgroupIo.write,
|
|
834
|
+
source: "cgroup"
|
|
835
|
+
};
|
|
836
|
+
let read3 = 0;
|
|
837
|
+
let write = 0;
|
|
838
|
+
for (const value of this.io.values()) {
|
|
839
|
+
read3 += value.read;
|
|
840
|
+
write += value.write;
|
|
841
|
+
}
|
|
842
|
+
return {
|
|
843
|
+
...cpu,
|
|
844
|
+
max_rss_kb: this.peakRss,
|
|
845
|
+
read_bytes: read3,
|
|
846
|
+
write_bytes: write,
|
|
847
|
+
source: "proc"
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
// src/lease/run.ts
|
|
853
|
+
function prefixed(argv, prefix) {
|
|
854
|
+
const name = (word = "") => word.slice(word.lastIndexOf("/") + 1);
|
|
855
|
+
return argv.length >= prefix.length && prefix.every((word, at) => (at === 0 ? name(argv[0]) : argv[at]) === word);
|
|
856
|
+
}
|
|
857
|
+
function laneFor(run2) {
|
|
858
|
+
const command = run2.settings.commands.find(
|
|
859
|
+
(entry) => prefixed(run2.argv, entry.argv)
|
|
860
|
+
);
|
|
861
|
+
const fromProfile = run2.profile === "gate" || run2.profile === "hook";
|
|
862
|
+
const lane = run2.lane ?? (fromProfile ? profileLane(run2.profile) : command?.lane ?? "interactive");
|
|
863
|
+
const tiny = run2.settings.tiny.some((prefix) => prefixed(run2.argv, prefix));
|
|
864
|
+
const weight = run2.weight ?? command?.weight ?? (tiny ? 0 : run2.settings.weights[lane]);
|
|
865
|
+
return { lane, weight };
|
|
866
|
+
}
|
|
867
|
+
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
868
|
+
function execute(argv, run2, extra, watch) {
|
|
869
|
+
const [bin, ...rest] = argv;
|
|
870
|
+
return new Promise((resolve) => {
|
|
871
|
+
const child = spawn(bin ?? "true", rest, {
|
|
872
|
+
cwd: run2.cwd,
|
|
873
|
+
env: { ...run2.env, ...extra },
|
|
874
|
+
stdio: "inherit"
|
|
875
|
+
});
|
|
876
|
+
void watch.started?.();
|
|
877
|
+
const forward = (signal) => child.kill(signal);
|
|
878
|
+
for (const signal of SIGNALS) process.on(signal, forward);
|
|
879
|
+
const timer = setInterval(() => watch.tick(child.pid), 1e3);
|
|
880
|
+
const done = (code) => {
|
|
881
|
+
clearInterval(timer);
|
|
882
|
+
for (const signal of SIGNALS) process.off(signal, forward);
|
|
883
|
+
resolve(code);
|
|
884
|
+
};
|
|
885
|
+
child.on("error", (error) => {
|
|
886
|
+
run2.say(`${bin}: ${error.message}`);
|
|
887
|
+
done(127);
|
|
888
|
+
});
|
|
889
|
+
child.on("exit", (code, signal) => {
|
|
890
|
+
if (code !== null) return done(code);
|
|
891
|
+
done(128 + (signal && constants.signals[signal] || 15));
|
|
892
|
+
});
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
function holdWarner(run2, since) {
|
|
896
|
+
let warned = false;
|
|
897
|
+
return () => {
|
|
898
|
+
const held2 = epoch() - since;
|
|
899
|
+
if (warned || held2 < run2.settings.holdWarn) return;
|
|
900
|
+
warned = true;
|
|
901
|
+
run2.say(
|
|
902
|
+
`holding the host lease for ${held2}s (hold_warn ${run2.settings.holdWarn}s); others may be waiting`
|
|
903
|
+
);
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
var BECAUSE = {
|
|
907
|
+
queue: "the jobs ahead kept the budget",
|
|
908
|
+
parked: "this session already had a job queued in the lane (per_session)",
|
|
909
|
+
host: "the host stayed at hard_factor x a limit or more",
|
|
910
|
+
legacy: "a shell wrapper kept the whole lease"
|
|
911
|
+
};
|
|
912
|
+
function busyReason(busy2) {
|
|
913
|
+
return `gave up after ${busy2.waited}s in the ${busy2.lane} lane (position ${busy2.position}): ${BECAUSE[busy2.reason]}; ${busy2.host}`;
|
|
914
|
+
}
|
|
915
|
+
function record(run2, fields) {
|
|
916
|
+
const usage = {
|
|
917
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
918
|
+
signature: signature(run2.argv),
|
|
919
|
+
dir: packageDir(run2.cwd, run2.root ?? run2.cwd),
|
|
920
|
+
...fields
|
|
921
|
+
};
|
|
922
|
+
appendUsage(leasePaths(run2.settings.dir).usage, usage, run2.settings.usageCap);
|
|
923
|
+
run2.onUsage?.(usage);
|
|
924
|
+
}
|
|
925
|
+
var CANCELLED_EXIT = 130;
|
|
926
|
+
var NOTHING = {
|
|
927
|
+
wall_s: 0,
|
|
928
|
+
user_s: 0,
|
|
929
|
+
sys_s: 0,
|
|
930
|
+
max_rss_kb: 0,
|
|
931
|
+
read_bytes: 0,
|
|
932
|
+
write_bytes: 0,
|
|
933
|
+
source: "none"
|
|
934
|
+
};
|
|
935
|
+
async function runHeavy(run2) {
|
|
936
|
+
const label = oneLine(run2.label || run2.argv.join(" "));
|
|
937
|
+
const { lane, weight } = laneFor(run2);
|
|
938
|
+
let outcome;
|
|
939
|
+
if (!run2.settings.enabled) {
|
|
940
|
+
run2.say("lease disabled ([host.lease] enabled = false); running now");
|
|
941
|
+
outcome = { kind: "disabled" };
|
|
942
|
+
} else {
|
|
943
|
+
outcome = acquireLease({ ...run2, label, lane, weight });
|
|
944
|
+
}
|
|
945
|
+
if (outcome.kind === "cancelled") {
|
|
946
|
+
record(run2, {
|
|
947
|
+
...NOTHING,
|
|
948
|
+
lane,
|
|
949
|
+
weight,
|
|
950
|
+
start: "none",
|
|
951
|
+
outcome: "cancelled",
|
|
952
|
+
exit: CANCELLED_EXIT
|
|
953
|
+
});
|
|
954
|
+
return CANCELLED_EXIT;
|
|
955
|
+
}
|
|
956
|
+
if (outcome.kind === "busy") {
|
|
957
|
+
run2.say(`not run: host busy; ${busyReason(outcome)}`);
|
|
958
|
+
if (outcome.lane === "bulk" && outcome.reason === "queue")
|
|
959
|
+
run2.say(
|
|
960
|
+
"a bulk job waits behind every gate, hook and interactive job; this one reached its max_wait"
|
|
961
|
+
);
|
|
962
|
+
run2.say(
|
|
963
|
+
`run it again later, or set ${DISABLE_ENV}=<reason> to run beside it`
|
|
964
|
+
);
|
|
965
|
+
record(run2, {
|
|
966
|
+
...NOTHING,
|
|
967
|
+
lane,
|
|
968
|
+
weight,
|
|
969
|
+
start: "none",
|
|
970
|
+
outcome: "timeout",
|
|
971
|
+
exit: BUSY_EXIT
|
|
972
|
+
});
|
|
973
|
+
return BUSY_EXIT;
|
|
974
|
+
}
|
|
975
|
+
const memoryMax = run2.settings.profiles[run2.profile]?.memoryMax;
|
|
976
|
+
const held2 = outcome.kind === "held" ? outcome : null;
|
|
977
|
+
const sampler = new Sampler();
|
|
978
|
+
const { argv, started } = scoped(
|
|
979
|
+
run2.argv,
|
|
980
|
+
run2.settings,
|
|
981
|
+
held2 ? lane : null,
|
|
982
|
+
memoryMax,
|
|
983
|
+
run2.say,
|
|
984
|
+
(dir) => sampler.useCgroup(dir)
|
|
985
|
+
);
|
|
986
|
+
const warn = held2 ? holdWarner(run2, epoch()) : () => {
|
|
987
|
+
};
|
|
988
|
+
const begun = Date.now();
|
|
989
|
+
let code = 1;
|
|
990
|
+
try {
|
|
991
|
+
code = await execute(argv, run2, held2?.env ?? {}, {
|
|
992
|
+
tick: (pid) => {
|
|
993
|
+
warn();
|
|
994
|
+
sampler.sample(pid);
|
|
995
|
+
},
|
|
996
|
+
...started ? { started } : {}
|
|
997
|
+
});
|
|
998
|
+
} finally {
|
|
999
|
+
const wall = Math.round((Date.now() - begun) / 100) / 10;
|
|
1000
|
+
held2?.release(code, Math.round(wall));
|
|
1001
|
+
if (outcome.kind !== "disabled")
|
|
1002
|
+
record(run2, {
|
|
1003
|
+
...sampler.finish(),
|
|
1004
|
+
wall_s: wall,
|
|
1005
|
+
lane,
|
|
1006
|
+
weight,
|
|
1007
|
+
start: held2 ? held2.start : "none",
|
|
1008
|
+
outcome: "ran",
|
|
1009
|
+
exit: code
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
return code;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// src/lease/status.ts
|
|
1016
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync7 } from "fs";
|
|
1017
|
+
|
|
1018
|
+
// src/lease/estimate.ts
|
|
1019
|
+
import { closeSync as closeSync3, fstatSync, openSync as openSync3, readSync } from "fs";
|
|
1020
|
+
var TAIL_BYTES = 512 * 1024;
|
|
1021
|
+
function tail(file) {
|
|
1022
|
+
let fd;
|
|
1023
|
+
try {
|
|
1024
|
+
fd = openSync3(file, "r");
|
|
1025
|
+
} catch {
|
|
1026
|
+
return "";
|
|
1027
|
+
}
|
|
1028
|
+
try {
|
|
1029
|
+
const size = fstatSync(fd).size;
|
|
1030
|
+
const length = Math.min(size, TAIL_BYTES);
|
|
1031
|
+
const buffer = Buffer.alloc(length);
|
|
1032
|
+
readSync(fd, buffer, 0, length, size - length);
|
|
1033
|
+
return buffer.toString("utf8");
|
|
1034
|
+
} finally {
|
|
1035
|
+
closeSync3(fd);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
function median(values) {
|
|
1039
|
+
if (values.length === 0) return void 0;
|
|
1040
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
1041
|
+
return sorted[Math.floor(sorted.length / 2)];
|
|
1042
|
+
}
|
|
1043
|
+
function logLines(log) {
|
|
1044
|
+
return tail(log).split("\n").map((line) => line.split(" ")).filter((fields) => fields.length >= 4);
|
|
1045
|
+
}
|
|
1046
|
+
function runMedians(lines) {
|
|
1047
|
+
const open = /* @__PURE__ */ new Map();
|
|
1048
|
+
const runs = { all: [] };
|
|
1049
|
+
for (const [, pid = "", event, detail = ""] of lines) {
|
|
1050
|
+
if (event === "acquired") {
|
|
1051
|
+
const named = detail.match(/ lane=(\w+)/u)?.[1];
|
|
1052
|
+
open.set(pid, LANES.includes(named) ? named : "interactive");
|
|
1053
|
+
} else if (event === "released") {
|
|
1054
|
+
const lane = open.get(pid);
|
|
1055
|
+
const ran = Number(detail.match(/^ran=(\d+)/u)?.[1]);
|
|
1056
|
+
if (lane === void 0 || !Number.isFinite(ran)) continue;
|
|
1057
|
+
open.delete(pid);
|
|
1058
|
+
(runs[lane] ??= []).push(ran);
|
|
1059
|
+
runs["all"].push(ran);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
const found = {};
|
|
1063
|
+
for (const [key, values] of Object.entries(runs)) {
|
|
1064
|
+
const value = median(values);
|
|
1065
|
+
if (value !== void 0) found[key] = value;
|
|
1066
|
+
}
|
|
1067
|
+
return found;
|
|
1068
|
+
}
|
|
1069
|
+
function graceRuns(lines, now = Date.now()) {
|
|
1070
|
+
const recent = lines.filter(
|
|
1071
|
+
([stamp = "", , event]) => event === "grace" && now - Date.parse(stamp) <= 864e5
|
|
1072
|
+
);
|
|
1073
|
+
const last = recent.at(-1);
|
|
1074
|
+
return {
|
|
1075
|
+
count: recent.length,
|
|
1076
|
+
last: last ? `${last[0]} (${last[3]})` : null
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
function etas(medians, budget, running, waiters, now) {
|
|
1080
|
+
if (medians["all"] === void 0) return waiters.map(() => null);
|
|
1081
|
+
const typical = (lane) => medians[lane] ?? medians["all"] ?? 0;
|
|
1082
|
+
let work = running.reduce(
|
|
1083
|
+
(sum, job2) => sum + Math.max(0, typical(job2.lane) - (now - job2.since)) * job2.weight,
|
|
1084
|
+
0
|
|
1085
|
+
);
|
|
1086
|
+
return waiters.map((waiter) => {
|
|
1087
|
+
const mine = Math.round(work / Math.max(1, budget));
|
|
1088
|
+
work += typical(waiter.lane) * waiter.weight;
|
|
1089
|
+
return mine;
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
// src/lease/procs.ts
|
|
1094
|
+
import { readFileSync as readFileSync6, readdirSync as readdirSync2 } from "fs";
|
|
1095
|
+
var TICKS_PER_SECOND = 100;
|
|
1096
|
+
function read2(file) {
|
|
1097
|
+
try {
|
|
1098
|
+
return readFileSync6(file, "utf8");
|
|
1099
|
+
} catch {
|
|
1100
|
+
return null;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
function sample(proc) {
|
|
1104
|
+
const found = /* @__PURE__ */ new Map();
|
|
1105
|
+
let names;
|
|
1106
|
+
try {
|
|
1107
|
+
names = readdirSync2(proc);
|
|
1108
|
+
} catch {
|
|
1109
|
+
return found;
|
|
1110
|
+
}
|
|
1111
|
+
for (const name of names) {
|
|
1112
|
+
if (!/^\d+$/u.test(name)) continue;
|
|
1113
|
+
const stat = read2(`${proc}/${name}/stat`);
|
|
1114
|
+
if (stat === null) continue;
|
|
1115
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
1116
|
+
const io2 = read2(`${proc}/${name}/io`) ?? "";
|
|
1117
|
+
const bytes = (key) => Number(io2.match(new RegExp(`^${key}: (\\d+)`, "mu"))?.[1] ?? 0);
|
|
1118
|
+
found.set(Number(name), {
|
|
1119
|
+
ppid: Number(fields[1]),
|
|
1120
|
+
ticks: Number(fields[11]) + Number(fields[12]),
|
|
1121
|
+
bytes: bytes("read_bytes") + bytes("write_bytes")
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
return found;
|
|
1125
|
+
}
|
|
1126
|
+
function under(pid, roots, all) {
|
|
1127
|
+
for (let at = pid, hops = 0; at > 1 && hops < 64; hops += 1) {
|
|
1128
|
+
if (roots.has(at)) return true;
|
|
1129
|
+
at = all.get(at)?.ppid ?? 0;
|
|
1130
|
+
}
|
|
1131
|
+
return false;
|
|
1132
|
+
}
|
|
1133
|
+
function outsideLease(options) {
|
|
1134
|
+
const proc = options.proc ?? "/proc";
|
|
1135
|
+
const interval = options.interval ?? 1;
|
|
1136
|
+
const before = sample(proc);
|
|
1137
|
+
if (before.size === 0) return [];
|
|
1138
|
+
sleepSeconds(interval);
|
|
1139
|
+
const after = sample(proc);
|
|
1140
|
+
const roots = /* @__PURE__ */ new Set([process.pid]);
|
|
1141
|
+
for (const pid of options.holders) roots.add(pid);
|
|
1142
|
+
const found = [];
|
|
1143
|
+
for (const [pid, now] of after) {
|
|
1144
|
+
const then = before.get(pid);
|
|
1145
|
+
if (then === void 0 || now.ppid === 2 || pid === 2) continue;
|
|
1146
|
+
if (under(pid, roots, after)) continue;
|
|
1147
|
+
const cpu = (now.ticks - then.ticks) / TICKS_PER_SECOND / interval * 100;
|
|
1148
|
+
const ioMbps = (now.bytes - then.bytes) / interval / 1e6;
|
|
1149
|
+
if (cpu < 20 && ioMbps < 5) continue;
|
|
1150
|
+
const command = (read2(`${proc}/${pid}/cmdline`) ?? "").split("\0").join(" ").trim().slice(0, 100);
|
|
1151
|
+
found.push({ pid, cpu, ioMbps, command });
|
|
1152
|
+
}
|
|
1153
|
+
return found.sort((a, b) => b.cpu + b.ioMbps * 10 - (a.cpu + a.ioMbps * 10)).slice(0, options.limit ?? 5);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// src/lease/status.ts
|
|
1157
|
+
function hostLine(settings2, load, io2) {
|
|
1158
|
+
return `host: load1 ${load ?? "n/a"} (max ${settings2.maxLoad}), io some avg10 ${io2 ?? "n/a"} (max ${settings2.maxIo})`;
|
|
1159
|
+
}
|
|
1160
|
+
function holdLine(settings2, job2) {
|
|
1161
|
+
const held2 = epoch() - job2.since;
|
|
1162
|
+
const limits = `warn ${settings2.holdWarn}s${settings2.holdMax ? `, max ${settings2.holdMax}s` : ""}`;
|
|
1163
|
+
let verdict = "ok";
|
|
1164
|
+
if (held2 >= settings2.holdWarn) verdict = "warning: over hold_warn";
|
|
1165
|
+
if (settings2.holdMax && held2 >= settings2.holdMax)
|
|
1166
|
+
verdict = "over-limit: over hold_max";
|
|
1167
|
+
return `hold: pid ${job2.pid} ${held2}s (${limits}) ${verdict} lane ${job2.lane} weight ${job2.weight}`;
|
|
1168
|
+
}
|
|
1169
|
+
function outsideLines(found) {
|
|
1170
|
+
if (found.length === 0) return ["outside: none"];
|
|
1171
|
+
return found.map(
|
|
1172
|
+
(item) => `outside: pid ${item.pid} cpu ${item.cpu.toFixed(0)}% io ${item.ioMbps.toFixed(1)}MB/s ${item.command}`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
function settledLine(settings2, env) {
|
|
1176
|
+
const paths = leasePaths(settings2.dir);
|
|
1177
|
+
if (hostSettled(settings2, paths, hostSources(env))) return "settled: now";
|
|
1178
|
+
let last = "never";
|
|
1179
|
+
try {
|
|
1180
|
+
last = readFileSync7(paths.settled, "utf8").trim() || "never";
|
|
1181
|
+
} catch {
|
|
1182
|
+
}
|
|
1183
|
+
return `settled: last seen ${last}`;
|
|
1184
|
+
}
|
|
1185
|
+
function leaseStatus(options) {
|
|
1186
|
+
const { settings: settings2, env } = options;
|
|
1187
|
+
const paths = leasePaths(settings2.dir);
|
|
1188
|
+
const reading = readHost(hostSources(env));
|
|
1189
|
+
const host = hostLine(settings2, reading.load, reading.io);
|
|
1190
|
+
if (!settings2.enabled)
|
|
1191
|
+
return ["lease: disabled ([host.lease] enabled = false)", host];
|
|
1192
|
+
try {
|
|
1193
|
+
mkdirSync2(paths.dir, { recursive: true });
|
|
1194
|
+
} catch {
|
|
1195
|
+
}
|
|
1196
|
+
const lines = [`lease: ${paths.lock}`, host, settledLine(settings2, env)];
|
|
1197
|
+
const held2 = leaseHeld(paths);
|
|
1198
|
+
lines.push(
|
|
1199
|
+
held2 ? `holder: ${describeRecord(readRecord(paths.holder))}` : "holder: none"
|
|
1200
|
+
);
|
|
1201
|
+
const { running, waiters } = readQueue(paths);
|
|
1202
|
+
const log = logLines(paths.log);
|
|
1203
|
+
const eta = etas(runMedians(log), settings2.budget, running, waiters, epoch());
|
|
1204
|
+
waiters.forEach((waiter, at) => {
|
|
1205
|
+
const guess = eta[at];
|
|
1206
|
+
const tail2 = guess === null || guess === void 0 ? "" : ` eta ~${guess}s`;
|
|
1207
|
+
lines.push(
|
|
1208
|
+
`waiter: ${describeRecord(waiter)} lane ${waiter.lane} weight ${waiter.weight} position ${at + 1}${tail2}${waiter.id ? ` id ${waiter.id}` : ""}`
|
|
1209
|
+
);
|
|
1210
|
+
});
|
|
1211
|
+
if (waiters.length === 0) lines.push("waiters: none");
|
|
1212
|
+
lines.push(`lanes: ${laneUse(settings2, running)}`);
|
|
1213
|
+
for (const job2 of running) lines.push(`running: ${describeRecord(job2)}`);
|
|
1214
|
+
for (const job2 of running.filter((entry) => entry.bypass))
|
|
1215
|
+
lines.push(
|
|
1216
|
+
`bypass running: pid ${job2.pid}, reason ${job2.bypass}, since ${epoch() - job2.since}s`
|
|
1217
|
+
);
|
|
1218
|
+
for (const job2 of running) lines.push(holdLine(settings2, job2));
|
|
1219
|
+
const window = reading.pressure[settings2.ioWindow] ?? "n/a";
|
|
1220
|
+
lines.push(
|
|
1221
|
+
`io: the host check reads some ${settings2.ioWindow} ${window} (max ${settings2.maxIo}, hard ${settings2.maxIo * settings2.hardFactor})`
|
|
1222
|
+
);
|
|
1223
|
+
const grace = graceRuns(log);
|
|
1224
|
+
lines.push(
|
|
1225
|
+
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}`
|
|
1226
|
+
);
|
|
1227
|
+
const outside = options.outside ?? ((pids) => outsideLease({ holders: pids }));
|
|
1228
|
+
lines.push(...outsideLines(outside(running.map((job2) => job2.pid))));
|
|
1229
|
+
const skip = skipReason(settings2, env);
|
|
1230
|
+
if (skip !== null)
|
|
1231
|
+
lines.push(
|
|
1232
|
+
`skip: this shell runs without the lease (${DISABLE_ENV}): ${skip}`
|
|
1233
|
+
);
|
|
1234
|
+
return lines;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
// src/lease/tickets.ts
|
|
1238
|
+
import { randomBytes } from "crypto";
|
|
1239
|
+
import {
|
|
1240
|
+
mkdirSync as mkdirSync3,
|
|
1241
|
+
openSync as openSync4,
|
|
1242
|
+
closeSync as closeSync4,
|
|
1243
|
+
readFileSync as readFileSync8,
|
|
1244
|
+
readdirSync as readdirSync3,
|
|
1245
|
+
renameSync as renameSync2,
|
|
1246
|
+
rmSync as rmSync2,
|
|
1247
|
+
statSync as statSync2,
|
|
1248
|
+
writeFileSync as writeFileSync4
|
|
1249
|
+
} from "fs";
|
|
1250
|
+
import { join as join5 } from "path";
|
|
1251
|
+
import { spawn as spawn2 } from "child_process";
|
|
1252
|
+
function ticketExists(dir, id) {
|
|
1253
|
+
try {
|
|
1254
|
+
return readJson(join5(ticketDir(dir, id), "request.json")) !== null;
|
|
1255
|
+
} catch {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
var ID = /^\d{10}-[0-9a-f]{6}$/u;
|
|
1260
|
+
var KEEP_MS = 7 * 864e5;
|
|
1261
|
+
function ticketsDir(dir) {
|
|
1262
|
+
return join5(dir, "tickets");
|
|
1263
|
+
}
|
|
1264
|
+
function ticketDir(dir, id) {
|
|
1265
|
+
if (!ID.test(id)) throw new Error(`not a ticket id: ${id}`);
|
|
1266
|
+
return join5(ticketsDir(dir), id);
|
|
1267
|
+
}
|
|
1268
|
+
function readJson(file) {
|
|
1269
|
+
try {
|
|
1270
|
+
return JSON.parse(readFileSync8(file, "utf8"));
|
|
1271
|
+
} catch {
|
|
1272
|
+
return null;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
function writeJson(file, value) {
|
|
1276
|
+
const temporary = `${file}.tmp.${process.pid}`;
|
|
1277
|
+
writeFileSync4(temporary, `${JSON.stringify(value)}
|
|
1278
|
+
`);
|
|
1279
|
+
renameSync2(temporary, file);
|
|
1280
|
+
}
|
|
1281
|
+
function writeResult(dir, id, result, keepExisting = false) {
|
|
1282
|
+
const file = join5(ticketDir(dir, id), "result.json");
|
|
1283
|
+
if (keepExisting && readJson(file) !== null) return;
|
|
1284
|
+
writeJson(file, result);
|
|
1285
|
+
}
|
|
1286
|
+
function prune(dir) {
|
|
1287
|
+
let names = [];
|
|
1288
|
+
try {
|
|
1289
|
+
names = readdirSync3(ticketsDir(dir));
|
|
1290
|
+
} catch {
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
for (const name of names) {
|
|
1294
|
+
const path = join5(ticketsDir(dir), name);
|
|
1295
|
+
try {
|
|
1296
|
+
const done = statSync2(join5(path, "result.json"));
|
|
1297
|
+
if (Date.now() - done.mtimeMs > KEEP_MS)
|
|
1298
|
+
rmSync2(path, { recursive: true, force: true });
|
|
1299
|
+
} catch {
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
function submit(request) {
|
|
1304
|
+
prune(request.dir);
|
|
1305
|
+
const id = `${Math.floor(Date.now() / 1e3)}-${randomBytes(3).toString("hex")}`;
|
|
1306
|
+
const path = ticketDir(request.dir, id);
|
|
1307
|
+
mkdirSync3(path, { recursive: true });
|
|
1308
|
+
writeJson(join5(path, "request.json"), {
|
|
1309
|
+
version: 1,
|
|
1310
|
+
ticket: id,
|
|
1311
|
+
label: request.label,
|
|
1312
|
+
cwd: request.cwd,
|
|
1313
|
+
created: (/* @__PURE__ */ new Date()).toISOString()
|
|
1314
|
+
});
|
|
1315
|
+
const log = openSync4(join5(path, "log"), "a");
|
|
1316
|
+
const [bin, ...args] = request.runner(id);
|
|
1317
|
+
const child = spawn2(bin, args, {
|
|
1318
|
+
cwd: request.cwd,
|
|
1319
|
+
env: request.env,
|
|
1320
|
+
detached: true,
|
|
1321
|
+
stdio: ["ignore", log, log]
|
|
1322
|
+
});
|
|
1323
|
+
closeSync4(log);
|
|
1324
|
+
writeFileSync4(join5(path, "pid"), `${child.pid}
|
|
1325
|
+
`);
|
|
1326
|
+
child.unref();
|
|
1327
|
+
return id;
|
|
1328
|
+
}
|
|
1329
|
+
function ticketView(dir, id) {
|
|
1330
|
+
const path = ticketDir(dir, id);
|
|
1331
|
+
const request = readJson(join5(path, "request.json"));
|
|
1332
|
+
if (request === null) return null;
|
|
1333
|
+
const result = readJson(join5(path, "result.json"));
|
|
1334
|
+
const pidText = readJson(join5(path, "pid"));
|
|
1335
|
+
const pid = typeof pidText === "number" && pidText > 0 ? pidText : null;
|
|
1336
|
+
const base = { id, label: request.label, pid, result };
|
|
1337
|
+
if (result) return { ...base, state: "done", position: null };
|
|
1338
|
+
if (pid === null) return { ...base, state: "queued", position: null };
|
|
1339
|
+
if (!pidAlive(pid)) return { ...base, state: "lost", position: null };
|
|
1340
|
+
const paths = leasePaths(dir);
|
|
1341
|
+
const waiting = liveRecords(paths.waiters, false).findIndex(
|
|
1342
|
+
(r) => r.pid === pid
|
|
1343
|
+
);
|
|
1344
|
+
if (waiting >= 0) return { ...base, state: "queued", position: waiting + 1 };
|
|
1345
|
+
const running = liveRecords(paths.running, false).some((r) => r.pid === pid);
|
|
1346
|
+
return { ...base, state: running ? "running" : "queued", position: null };
|
|
1347
|
+
}
|
|
1348
|
+
function recentTickets(dir) {
|
|
1349
|
+
let names = [];
|
|
1350
|
+
try {
|
|
1351
|
+
names = readdirSync3(ticketsDir(dir)).filter((name) => ID.test(name));
|
|
1352
|
+
} catch {
|
|
1353
|
+
return [];
|
|
1354
|
+
}
|
|
1355
|
+
return names.sort().map((name) => ticketView(dir, name)).filter((view) => {
|
|
1356
|
+
if (view === null) return false;
|
|
1357
|
+
if (view.state !== "done") return true;
|
|
1358
|
+
try {
|
|
1359
|
+
const done = statSync2(join5(ticketDir(dir, view.id), "result.json"));
|
|
1360
|
+
return Date.now() - done.mtimeMs < 36e5;
|
|
1361
|
+
} catch {
|
|
1362
|
+
return false;
|
|
1363
|
+
}
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
function logTail(dir, id, lines = 20) {
|
|
1367
|
+
try {
|
|
1368
|
+
const text = readFileSync8(join5(ticketDir(dir, id), "log"), "utf8");
|
|
1369
|
+
return text.split("\n").slice(-lines - 1).join("\n");
|
|
1370
|
+
} catch {
|
|
1371
|
+
return "";
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
function cancel(dir, id, force) {
|
|
1375
|
+
const view = ticketView(dir, id);
|
|
1376
|
+
if (view === null) return "unknown";
|
|
1377
|
+
if (view.state === "done") return "done";
|
|
1378
|
+
if (view.state === "running" && !force) return "running";
|
|
1379
|
+
if (view.pid !== null && pidAlive(view.pid)) {
|
|
1380
|
+
try {
|
|
1381
|
+
process.kill(-view.pid, "SIGTERM");
|
|
1382
|
+
} catch {
|
|
1383
|
+
process.kill(view.pid, "SIGTERM");
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
writeResult(dir, id, {
|
|
1387
|
+
version: 1,
|
|
1388
|
+
ticket: id,
|
|
1389
|
+
exit: 130,
|
|
1390
|
+
outcome: "cancelled"
|
|
1391
|
+
});
|
|
1392
|
+
return "cancelled";
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// src/lease/status-json.ts
|
|
1396
|
+
var STATUS_SCHEMA = "pmcp.heavy.status.v1";
|
|
1397
|
+
function flags(settings2, job2, now) {
|
|
1398
|
+
const held2 = now - job2.since;
|
|
1399
|
+
return [
|
|
1400
|
+
...job2.grace ? ["grace"] : [],
|
|
1401
|
+
...job2.bypass ? ["bypass"] : [],
|
|
1402
|
+
...held2 >= settings2.holdWarn ? ["over_hold_warn"] : [],
|
|
1403
|
+
...settings2.holdMax && held2 >= settings2.holdMax ? ["over_hold_max"] : []
|
|
1404
|
+
];
|
|
1405
|
+
}
|
|
1406
|
+
function job(settings2, record2, now) {
|
|
1407
|
+
return {
|
|
1408
|
+
id: record2.id || null,
|
|
1409
|
+
pid: record2.pid,
|
|
1410
|
+
label: record2.label,
|
|
1411
|
+
cwd: record2.cwd,
|
|
1412
|
+
lane: record2.lane,
|
|
1413
|
+
weight: record2.weight,
|
|
1414
|
+
session: record2.session || null,
|
|
1415
|
+
started: record2.started,
|
|
1416
|
+
running_s: now - record2.since,
|
|
1417
|
+
flags: flags(settings2, record2, now),
|
|
1418
|
+
bypass_reason: record2.bypass || null
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
function number(value) {
|
|
1422
|
+
return value === null || value === void 0 ? null : Number(value);
|
|
1423
|
+
}
|
|
1424
|
+
function statusJson(settings2, env, outside = (pids) => outsideLease({ holders: pids, interval: 0.25 })) {
|
|
1425
|
+
const paths = leasePaths(settings2.dir);
|
|
1426
|
+
const reading = readHost(hostSources(env));
|
|
1427
|
+
const now = epoch();
|
|
1428
|
+
const { running, waiters } = settings2.enabled ? readQueue(paths, false) : { running: [], waiters: [] };
|
|
1429
|
+
const log = settings2.enabled ? logLines(paths.log) : [];
|
|
1430
|
+
const eta = etas(runMedians(log), settings2.budget, running, waiters, now);
|
|
1431
|
+
return {
|
|
1432
|
+
version: 1,
|
|
1433
|
+
schema: STATUS_SCHEMA,
|
|
1434
|
+
enabled: settings2.enabled,
|
|
1435
|
+
dir: settings2.dir,
|
|
1436
|
+
budget: settings2.budget,
|
|
1437
|
+
running_weight: runningWeight(running),
|
|
1438
|
+
// A shell wrapper holds the lock file exclusively and counts as the whole budget.
|
|
1439
|
+
legacy_holder: settings2.enabled && running.length === 0 && leaseHeld(paths),
|
|
1440
|
+
host: {
|
|
1441
|
+
load1: number(reading.load),
|
|
1442
|
+
io_some_avg10: number(reading.pressure["avg10"]),
|
|
1443
|
+
io_some_avg60: number(reading.pressure["avg60"]),
|
|
1444
|
+
io_some_avg300: number(reading.pressure["avg300"]),
|
|
1445
|
+
io_window: settings2.ioWindow,
|
|
1446
|
+
max_load: settings2.maxLoad,
|
|
1447
|
+
max_io: settings2.maxIo,
|
|
1448
|
+
hard_factor: settings2.hardFactor,
|
|
1449
|
+
settled: settled(settings2, reading)
|
|
1450
|
+
},
|
|
1451
|
+
lanes: LANES.map((lane) => ({
|
|
1452
|
+
lane,
|
|
1453
|
+
slots: settings2.slots[lane],
|
|
1454
|
+
running: running.filter((record2) => record2.lane === lane).map((record2) => job(settings2, record2, now))
|
|
1455
|
+
})),
|
|
1456
|
+
waiters: waiters.map((record2, at) => ({
|
|
1457
|
+
id: record2.id || null,
|
|
1458
|
+
pid: record2.pid,
|
|
1459
|
+
label: record2.label,
|
|
1460
|
+
cwd: record2.cwd,
|
|
1461
|
+
lane: record2.lane,
|
|
1462
|
+
weight: record2.weight,
|
|
1463
|
+
session: record2.session || null,
|
|
1464
|
+
position: at + 1,
|
|
1465
|
+
waited_s: now - record2.since,
|
|
1466
|
+
eta_s: eta[at] ?? null
|
|
1467
|
+
})),
|
|
1468
|
+
tickets: settings2.enabled ? recentTickets(settings2.dir).map((view) => ({
|
|
1469
|
+
id: view.id,
|
|
1470
|
+
state: view.state,
|
|
1471
|
+
position: view.position,
|
|
1472
|
+
label: view.label,
|
|
1473
|
+
pid: view.pid,
|
|
1474
|
+
exit: view.result?.exit ?? null
|
|
1475
|
+
})) : [],
|
|
1476
|
+
grace_24h: graceRuns(log),
|
|
1477
|
+
outside: settings2.enabled ? outside(running.map((record2) => record2.pid)).map((item) => ({
|
|
1478
|
+
pid: item.pid,
|
|
1479
|
+
cpu_percent: Math.round(item.cpu),
|
|
1480
|
+
io_mbps: Math.round(item.ioMbps * 10) / 10,
|
|
1481
|
+
command: item.command
|
|
1482
|
+
})) : [],
|
|
1483
|
+
skip: skipReason(settings2, env)
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
// src/lease/control.ts
|
|
1488
|
+
import { readdirSync as readdirSync4 } from "fs";
|
|
1489
|
+
import { join as join6 } from "path";
|
|
1490
|
+
function find(paths, dir, id) {
|
|
1491
|
+
let names = [];
|
|
1492
|
+
try {
|
|
1493
|
+
names = readdirSync4(dir).filter((name) => /^\d+$/u.test(name));
|
|
1494
|
+
} catch {
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
for (const name of names) {
|
|
1498
|
+
const record2 = readRecord(join6(dir, name));
|
|
1499
|
+
if (record2?.id === id) return { file: join6(dir, name), record: record2 };
|
|
1500
|
+
}
|
|
1501
|
+
return null;
|
|
1502
|
+
}
|
|
1503
|
+
function findRunning(paths, id) {
|
|
1504
|
+
return liveRecords(paths.running, false).find((r) => r.id === id) ?? null;
|
|
1505
|
+
}
|
|
1506
|
+
function rewrite(file, record2, change2) {
|
|
1507
|
+
const next = { ...record2, ...change2 };
|
|
1508
|
+
writeRecord(file, next.pid, {
|
|
1509
|
+
cwd: next.cwd,
|
|
1510
|
+
label: next.label,
|
|
1511
|
+
lane: next.lane,
|
|
1512
|
+
weight: next.weight,
|
|
1513
|
+
session: next.session,
|
|
1514
|
+
queued: next.queued,
|
|
1515
|
+
since: next.since,
|
|
1516
|
+
started: next.started,
|
|
1517
|
+
...next.id ? { id: next.id } : {},
|
|
1518
|
+
...next.promote ? { promote: next.promote } : {},
|
|
1519
|
+
...next.cancel ? { cancel: next.cancel } : {}
|
|
1520
|
+
});
|
|
1521
|
+
return next;
|
|
1522
|
+
}
|
|
1523
|
+
function logged(paths, record2, who2, what, reason2) {
|
|
1524
|
+
const by = `uid:${who2.uid ?? "?"}${who2.session ? ` session=${who2.session}` : ""}`;
|
|
1525
|
+
logEvent(
|
|
1526
|
+
paths,
|
|
1527
|
+
record2.pid,
|
|
1528
|
+
"control",
|
|
1529
|
+
`id=${record2.id} ${what} by=${by} reason=${reason2}`
|
|
1530
|
+
);
|
|
1531
|
+
}
|
|
1532
|
+
function change(paths, id, who2, reason2, what) {
|
|
1533
|
+
const done = underAdmitLock(paths, () => {
|
|
1534
|
+
const found = find(paths, paths.waiters, id);
|
|
1535
|
+
if (found === null) return { ok: false, why: `no waiting job ${id}` };
|
|
1536
|
+
const waiters = liveRecords(paths.waiters);
|
|
1537
|
+
if (!waiters.some((r) => r.pid === found.record.pid))
|
|
1538
|
+
return { ok: false, why: `${id} had died; its record was removed` };
|
|
1539
|
+
const decided = what(found.record, waiters);
|
|
1540
|
+
if (typeof decided === "string") return { ok: false, why: decided };
|
|
1541
|
+
const [fields, description] = decided;
|
|
1542
|
+
const record2 = rewrite(found.file, found.record, fields);
|
|
1543
|
+
logged(paths, record2, who2, description, reason2);
|
|
1544
|
+
return { ok: true, record: record2 };
|
|
1545
|
+
});
|
|
1546
|
+
return done ?? { ok: false, why: "the admission lock stayed busy" };
|
|
1547
|
+
}
|
|
1548
|
+
function promote(paths, id, who2, reason2) {
|
|
1549
|
+
return change(paths, id, who2, reason2, () => [
|
|
1550
|
+
{ promote: reason2 },
|
|
1551
|
+
"action=promote"
|
|
1552
|
+
]);
|
|
1553
|
+
}
|
|
1554
|
+
function cancelWaiter(paths, id, who2, reason2) {
|
|
1555
|
+
return change(paths, id, who2, reason2, () => [
|
|
1556
|
+
{ cancel: reason2 },
|
|
1557
|
+
"action=cancel"
|
|
1558
|
+
]);
|
|
1559
|
+
}
|
|
1560
|
+
function reprioritize(paths, id, to, who2, reason2) {
|
|
1561
|
+
return change(paths, id, who2, reason2, (record2, waiters) => {
|
|
1562
|
+
if (LANES.includes(to))
|
|
1563
|
+
return [
|
|
1564
|
+
{ lane: to, queued: Date.now() },
|
|
1565
|
+
`action=priority from=${record2.lane} to=${to}`
|
|
1566
|
+
];
|
|
1567
|
+
const step = /^[+-]\d+$/u.test(to) ? Number(to) : NaN;
|
|
1568
|
+
if (!Number.isInteger(step) || step === 0)
|
|
1569
|
+
return `priority is a lane (${LANES.join(", ")}) or +N/-N, not ${to}`;
|
|
1570
|
+
const lane = waiters.filter((r) => r.lane === record2.lane);
|
|
1571
|
+
const at = lane.findIndex((r) => r.pid === record2.pid);
|
|
1572
|
+
const target = Math.max(0, Math.min(lane.length - 1, at - step));
|
|
1573
|
+
if (target === at) return [{}, `action=priority ${to} (already there)`];
|
|
1574
|
+
const other = lane[target];
|
|
1575
|
+
const queued = target < at ? other.queued - 1 : other.queued + 1;
|
|
1576
|
+
return [
|
|
1577
|
+
{ queued },
|
|
1578
|
+
`action=priority ${to} position=${at + 1}->${target + 1}`
|
|
1579
|
+
];
|
|
1580
|
+
});
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
// src/commands/heavy-queue.ts
|
|
1584
|
+
function who(context) {
|
|
1585
|
+
return {
|
|
1586
|
+
uid: process.getuid?.() ?? null,
|
|
1587
|
+
session: sessionOf(context.env, context.cwd)
|
|
1588
|
+
};
|
|
1589
|
+
}
|
|
1590
|
+
function reason(context, required) {
|
|
1591
|
+
const text = one(context.args, "reason")?.trim() ?? "";
|
|
1592
|
+
if (required && !text)
|
|
1593
|
+
throw new ArgumentError("say why with --reason <text>");
|
|
1594
|
+
return text || "none given";
|
|
1595
|
+
}
|
|
1596
|
+
function report(context, result) {
|
|
1597
|
+
if (!result.ok) {
|
|
1598
|
+
context.ui.error(result.why);
|
|
1599
|
+
return EXIT.failure;
|
|
1600
|
+
}
|
|
1601
|
+
context.ui.data(
|
|
1602
|
+
`${result.record.id} ${result.record.lane} ${result.record.label}
|
|
1603
|
+
`
|
|
1604
|
+
);
|
|
1605
|
+
return EXIT.ok;
|
|
1606
|
+
}
|
|
1607
|
+
function queueList(context, settings2) {
|
|
1608
|
+
const snapshot = statusJson(settings2, context.env, () => []);
|
|
1609
|
+
if (context.args.flags.has("json")) {
|
|
1610
|
+
const { version, schema, waiters } = snapshot;
|
|
1611
|
+
context.ui.data(`${JSON.stringify({ version, schema, waiters })}
|
|
1612
|
+
`);
|
|
1613
|
+
return EXIT.ok;
|
|
1614
|
+
}
|
|
1615
|
+
if (snapshot.waiters.length === 0) context.ui.data("queue: empty\n");
|
|
1616
|
+
for (const w of snapshot.waiters)
|
|
1617
|
+
context.ui.data(
|
|
1618
|
+
`${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}
|
|
1619
|
+
`
|
|
1620
|
+
);
|
|
1621
|
+
return EXIT.ok;
|
|
1622
|
+
}
|
|
1623
|
+
function queuePromote(context, settings2, id) {
|
|
1624
|
+
if (id === void 0) throw new ArgumentError("name the job id");
|
|
1625
|
+
if (!context.args.flags.has("now"))
|
|
1626
|
+
throw new ArgumentError(
|
|
1627
|
+
"promote starts a job at once: pass --now --reason <text>"
|
|
1628
|
+
);
|
|
1629
|
+
const paths = leasePaths(settings2.dir);
|
|
1630
|
+
return report(
|
|
1631
|
+
context,
|
|
1632
|
+
promote(paths, id, who(context), reason(context, true))
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
function queuePriority(context, settings2, id, to) {
|
|
1636
|
+
if (id === void 0 || to === void 0)
|
|
1637
|
+
throw new ArgumentError(
|
|
1638
|
+
"pmcp heavy priority <id> <lane|+N|-N> --reason <text>"
|
|
1639
|
+
);
|
|
1640
|
+
const paths = leasePaths(settings2.dir);
|
|
1641
|
+
return report(
|
|
1642
|
+
context,
|
|
1643
|
+
reprioritize(paths, id, to, who(context), reason(context, true))
|
|
1644
|
+
);
|
|
1645
|
+
}
|
|
1646
|
+
function queueCancel(context, settings2, id) {
|
|
1647
|
+
if (id === void 0) throw new ArgumentError("name the job id");
|
|
1648
|
+
const paths = leasePaths(settings2.dir);
|
|
1649
|
+
const because = reason(context, false);
|
|
1650
|
+
const ticket = /^\d{10}-[0-9a-f]{6}$/u.test(id) && ticketExists(settings2.dir, id);
|
|
1651
|
+
const waiting = cancelWaiter(paths, id, who(context), because);
|
|
1652
|
+
if (waiting.ok) {
|
|
1653
|
+
if (ticket)
|
|
1654
|
+
writeResult(settings2.dir, id, {
|
|
1655
|
+
version: 1,
|
|
1656
|
+
ticket: id,
|
|
1657
|
+
exit: 130,
|
|
1658
|
+
outcome: "cancelled"
|
|
1659
|
+
});
|
|
1660
|
+
context.ui.data(`${id} cancelled
|
|
1661
|
+
`);
|
|
1662
|
+
return EXIT.ok;
|
|
1663
|
+
}
|
|
1664
|
+
const running = findRunning(paths, id);
|
|
1665
|
+
if (running !== null && !ticket) {
|
|
1666
|
+
if (!context.args.flags.has("force")) {
|
|
1667
|
+
context.ui.error(`${id} is running; --force stops it`);
|
|
1668
|
+
return EXIT.failure;
|
|
1669
|
+
}
|
|
1670
|
+
process.kill(running.pid, "SIGTERM");
|
|
1671
|
+
context.ui.data(`${id} stopped
|
|
1672
|
+
`);
|
|
1673
|
+
return EXIT.ok;
|
|
1674
|
+
}
|
|
1675
|
+
if (!ticket) {
|
|
1676
|
+
context.ui.error(waiting.why);
|
|
1677
|
+
return EXIT.failure;
|
|
1678
|
+
}
|
|
1679
|
+
const done = cancel(settings2.dir, id, context.args.flags.has("force"));
|
|
1680
|
+
if (done === "running") {
|
|
1681
|
+
context.ui.error(`${id} is running; --force stops it`);
|
|
1682
|
+
return EXIT.failure;
|
|
1683
|
+
}
|
|
1684
|
+
context.ui.data(`${id} ${done}
|
|
1685
|
+
`);
|
|
1686
|
+
return done === "cancelled" ? EXIT.ok : EXIT.failure;
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
// src/commands/heavy-tickets.ts
|
|
1690
|
+
var PASSED = ["config", "label", "profile", "lane", "weight"];
|
|
1691
|
+
function submitTicket(context, settings2, command) {
|
|
1692
|
+
const flags2 = PASSED.flatMap((name) => {
|
|
1693
|
+
const value = one(context.args, name);
|
|
1694
|
+
return value === void 0 ? [] : [`--${name}`, value];
|
|
1695
|
+
});
|
|
1696
|
+
const self = [process.execPath, ...process.execArgv, process.argv[1] ?? ""];
|
|
1697
|
+
const id = submit({
|
|
1698
|
+
dir: settings2.dir,
|
|
1699
|
+
label: one(context.args, "label") ?? command.join(" "),
|
|
1700
|
+
runner: (ticket) => [
|
|
1701
|
+
...self,
|
|
1702
|
+
"heavy",
|
|
1703
|
+
...flags2,
|
|
1704
|
+
"--ticket",
|
|
1705
|
+
ticket,
|
|
1706
|
+
"--",
|
|
1707
|
+
...command
|
|
1708
|
+
],
|
|
1709
|
+
cwd: context.cwd,
|
|
1710
|
+
env: context.env
|
|
1711
|
+
});
|
|
1712
|
+
context.ui.data(`${id}
|
|
1713
|
+
`);
|
|
1714
|
+
return EXIT.ok;
|
|
1715
|
+
}
|
|
1716
|
+
function describe(view) {
|
|
1717
|
+
const where = view.position === null ? "" : ` position ${view.position}`;
|
|
1718
|
+
const result = view.result ? ` exit ${view.result.exit} (${view.result.outcome})` : "";
|
|
1719
|
+
return `${view.id} ${view.state}${where}${result} ${view.label}`;
|
|
1720
|
+
}
|
|
1721
|
+
function viewOf(settings2, id) {
|
|
1722
|
+
if (id === void 0) throw new ArgumentError("name the ticket");
|
|
1723
|
+
let view;
|
|
1724
|
+
try {
|
|
1725
|
+
view = ticketView(settings2.dir, id);
|
|
1726
|
+
} catch (error) {
|
|
1727
|
+
throw new ArgumentError(error.message);
|
|
1728
|
+
}
|
|
1729
|
+
if (view === null) throw new ArgumentError(`no ticket ${id}`);
|
|
1730
|
+
return view;
|
|
1731
|
+
}
|
|
1732
|
+
function ticketResult(context, settings2, id) {
|
|
1733
|
+
const view = viewOf(settings2, id);
|
|
1734
|
+
if (context.args.flags.has("json"))
|
|
1735
|
+
context.ui.data(`${JSON.stringify({ version: 1, ...view })}
|
|
1736
|
+
`);
|
|
1737
|
+
else context.ui.data(`${describe(view)}
|
|
1738
|
+
`);
|
|
1739
|
+
return EXIT.ok;
|
|
1740
|
+
}
|
|
1741
|
+
async function ticketWait(context, settings2, id) {
|
|
1742
|
+
const text = one(context.args, "timeout");
|
|
1743
|
+
const timeout = text === void 0 ? Infinity : Number(text);
|
|
1744
|
+
if (!(timeout > 0)) throw new ArgumentError("--timeout must be positive");
|
|
1745
|
+
const deadline = Date.now() + timeout * 1e3;
|
|
1746
|
+
let view = viewOf(settings2, id);
|
|
1747
|
+
while (view.state === "queued" || view.state === "running") {
|
|
1748
|
+
if (Date.now() >= deadline) {
|
|
1749
|
+
context.ui.error(`${view.id} is still ${view.state}`);
|
|
1750
|
+
return BUSY_EXIT;
|
|
1751
|
+
}
|
|
1752
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
1753
|
+
view = viewOf(settings2, view.id);
|
|
1754
|
+
}
|
|
1755
|
+
const tail2 = logTail(settings2.dir, view.id);
|
|
1756
|
+
if (tail2.trim()) process.stderr.write(`${tail2}
|
|
1757
|
+
`);
|
|
1758
|
+
context.ui.data(`${describe(view)}
|
|
1759
|
+
`);
|
|
1760
|
+
if (view.state === "lost") return EXIT.failure;
|
|
1761
|
+
return view.result?.exit ?? EXIT.failure;
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
// src/commands/heavy.ts
|
|
1765
|
+
function settings(context) {
|
|
1766
|
+
try {
|
|
1767
|
+
return leaseSettingsFor(context.cwd, one(context.args, "config"));
|
|
1768
|
+
} catch (error) {
|
|
1769
|
+
if (error instanceof ConfigError) throw new ArgumentError(error.message);
|
|
1770
|
+
throw error;
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
function statLine(stat) {
|
|
1774
|
+
const pair = (key, scale = 1, unit = "") => `${Math.round(stat.p50[key] / scale)}/${Math.round(stat.p90[key] / scale)}${unit}`;
|
|
1775
|
+
const cpu = {
|
|
1776
|
+
p50: stat.p50.user_s + stat.p50.sys_s,
|
|
1777
|
+
p90: stat.p90.user_s + stat.p90.sys_s
|
|
1778
|
+
};
|
|
1779
|
+
return [
|
|
1780
|
+
stat.signature,
|
|
1781
|
+
`samples ${stat.samples} (failed ${stat.failed}, timeouts ${stat.timeouts})`,
|
|
1782
|
+
`wall ${pair("wall_s", 1, "s")}`,
|
|
1783
|
+
`cpu ${Math.round(cpu.p50)}/${Math.round(cpu.p90)}s`,
|
|
1784
|
+
`rss ${pair("max_rss_kb", 1024, "MB")}`,
|
|
1785
|
+
`read ${pair("read_bytes", 1e6, "MB")}`,
|
|
1786
|
+
`write ${pair("write_bytes", 1e6, "MB")}`
|
|
1787
|
+
].join(" ");
|
|
1788
|
+
}
|
|
1789
|
+
function stats(context) {
|
|
1790
|
+
const found = usageStats(readUsage(leasePaths(settings(context).dir).usage));
|
|
1791
|
+
if (context.args.flags.has("json")) {
|
|
1792
|
+
context.ui.data(`${JSON.stringify(found)}
|
|
1793
|
+
`);
|
|
1794
|
+
return EXIT.ok;
|
|
1795
|
+
}
|
|
1796
|
+
if (found.length === 0) context.ui.data("no usage recorded yet\n");
|
|
1797
|
+
context.ui.data(
|
|
1798
|
+
"signature @ dir samples p50/p90 of wall, cpu, rss, read, write\n"
|
|
1799
|
+
);
|
|
1800
|
+
for (const stat of found) context.ui.data(`${statLine(stat)}
|
|
1801
|
+
`);
|
|
1802
|
+
return EXIT.ok;
|
|
1803
|
+
}
|
|
1804
|
+
function verbs(context) {
|
|
1805
|
+
const { positional, passthrough } = context.args;
|
|
1806
|
+
return positional.slice(0, positional.length - (passthrough?.length ?? 0));
|
|
1807
|
+
}
|
|
1808
|
+
function subcommand(context) {
|
|
1809
|
+
const [verb, ticket, to, ...rest] = verbs(context);
|
|
1810
|
+
const lease = settings(context);
|
|
1811
|
+
if (verb === "priority" && rest.length === 0)
|
|
1812
|
+
return queuePriority(context, lease, ticket, to);
|
|
1813
|
+
if (to !== void 0) throw new ArgumentError(`unexpected ${to}`);
|
|
1814
|
+
if (verb === "result") return ticketResult(context, lease, ticket);
|
|
1815
|
+
if (verb === "wait") return ticketWait(context, lease, ticket);
|
|
1816
|
+
if (verb === "cancel") return queueCancel(context, lease, ticket);
|
|
1817
|
+
if (verb === "promote") return queuePromote(context, lease, ticket);
|
|
1818
|
+
if (ticket !== void 0 && verb !== "queue")
|
|
1819
|
+
throw new ArgumentError(`unexpected ${ticket}`);
|
|
1820
|
+
if (verb === "queue") return queueList(context, lease);
|
|
1821
|
+
if (verb !== "status" && verb !== "stats")
|
|
1822
|
+
throw new ArgumentError(
|
|
1823
|
+
"put the command after --, or use pmcp heavy status | stats | queue | submit | wait | result | promote | priority | cancel"
|
|
1824
|
+
);
|
|
1825
|
+
if (verb === "stats") return stats(context);
|
|
1826
|
+
if (context.args.flags.has("json")) {
|
|
1827
|
+
const snapshot = statusJson(lease, context.env);
|
|
1828
|
+
context.ui.data(`${JSON.stringify(snapshot)}
|
|
1829
|
+
`);
|
|
1830
|
+
return EXIT.ok;
|
|
1831
|
+
}
|
|
1832
|
+
for (const line of leaseStatus({ settings: lease, env: context.env }))
|
|
1833
|
+
context.ui.data(`${line}
|
|
1834
|
+
`);
|
|
1835
|
+
return EXIT.ok;
|
|
1836
|
+
}
|
|
1837
|
+
async function run(context) {
|
|
1838
|
+
const { args } = context;
|
|
1839
|
+
const say = (line) => process.stderr.write(`pmcp heavy: ${line}
|
|
1840
|
+
`);
|
|
1841
|
+
const [verb, ...extra] = verbs(context);
|
|
1842
|
+
if (args.passthrough === void 0 || verb !== void 0) {
|
|
1843
|
+
if (verb !== "submit") return subcommand(context);
|
|
1844
|
+
if (extra.length > 0) throw new ArgumentError(`unexpected ${extra[0]}`);
|
|
1845
|
+
}
|
|
1846
|
+
if (args.passthrough === void 0 || args.passthrough.length === 0)
|
|
1847
|
+
throw new ArgumentError("no command after --");
|
|
1848
|
+
const lease = settings(context);
|
|
1849
|
+
const profile = one(args, "profile") ?? "default";
|
|
1850
|
+
if (lease.profiles[profile] === void 0)
|
|
1851
|
+
throw new ArgumentError(
|
|
1852
|
+
`unknown profile ${profile}; known: ${Object.keys(lease.profiles).join(", ")}`
|
|
1853
|
+
);
|
|
1854
|
+
const label = one(args, "label");
|
|
1855
|
+
const reason2 = one(args, "reason") ?? context.env[NOW_ENV];
|
|
1856
|
+
const now = args.flags.has("now") || Boolean(context.env[NOW_ENV]);
|
|
1857
|
+
if (now && !reason2?.trim())
|
|
1858
|
+
throw new ArgumentError(
|
|
1859
|
+
"--now needs --reason <text>: say why it cannot wait"
|
|
1860
|
+
);
|
|
1861
|
+
if (!now && one(args, "reason") !== void 0)
|
|
1862
|
+
throw new ArgumentError("--reason goes with --now");
|
|
1863
|
+
const lane = one(args, "lane");
|
|
1864
|
+
if (lane !== void 0 && !LANES.includes(lane))
|
|
1865
|
+
throw new ArgumentError(`--lane is ${LANES.join(", ")}`);
|
|
1866
|
+
const weightText = one(args, "weight");
|
|
1867
|
+
const weight = weightText === void 0 ? void 0 : Number(weightText);
|
|
1868
|
+
if (weight !== void 0 && !(Number.isInteger(weight) && weight >= 0))
|
|
1869
|
+
throw new ArgumentError("--weight is a whole number (0 for a tiny job)");
|
|
1870
|
+
if (verb === "submit") {
|
|
1871
|
+
if (now) throw new ArgumentError("--now runs at once; it is not submitted");
|
|
1872
|
+
return submitTicket(context, lease, args.passthrough);
|
|
1873
|
+
}
|
|
1874
|
+
const ticket = one(args, "ticket");
|
|
1875
|
+
let result = null;
|
|
1876
|
+
const config = one(args, "config") ?? findConfig(context.cwd);
|
|
1877
|
+
const code = await runHeavy({
|
|
1878
|
+
settings: lease,
|
|
1879
|
+
profile,
|
|
1880
|
+
...label ? { label } : {},
|
|
1881
|
+
...lane ? { lane } : {},
|
|
1882
|
+
...weight !== void 0 ? { weight } : {},
|
|
1883
|
+
...now ? { now: reason2 } : {},
|
|
1884
|
+
root: config ? dirname(config) : context.cwd,
|
|
1885
|
+
...ticket ? { id: ticket } : {},
|
|
1886
|
+
argv: args.passthrough,
|
|
1887
|
+
cwd: context.cwd,
|
|
1888
|
+
env: context.env,
|
|
1889
|
+
say,
|
|
1890
|
+
...ticket ? {
|
|
1891
|
+
onUsage: (usage) => {
|
|
1892
|
+
result = { version: 1, ticket, ...usage };
|
|
1893
|
+
}
|
|
1894
|
+
} : {}
|
|
1895
|
+
});
|
|
1896
|
+
if (ticket)
|
|
1897
|
+
writeResult(
|
|
1898
|
+
lease.dir,
|
|
1899
|
+
ticket,
|
|
1900
|
+
result ?? { version: 1, ticket, exit: code, outcome: "ran" },
|
|
1901
|
+
true
|
|
1902
|
+
);
|
|
1903
|
+
return code;
|
|
1904
|
+
}
|
|
1905
|
+
var heavyCommand = { ...heavySpec, run };
|
|
1906
|
+
export {
|
|
1907
|
+
heavyCommand
|
|
1908
|
+
};
|
|
1909
|
+
//# sourceMappingURL=heavy-IIZBO4YM.js.map
|