pi-straw-boss 0.106.0 → 0.107.1
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/dist/server/{chunk-WTEFOEUM.mjs → chunk-F2RNIFD6.mjs} +716 -57
- package/dist/server/cli.mjs +290 -43
- package/dist/server/main.mjs +2975 -3337
- package/package.json +1 -1
- package/src/bosswatch.ts +9 -4
- package/src/deadpane.ts +6 -4
- package/src/index.ts +4 -2
- package/src/procs/kill.ts +3 -1
- package/src/procs/killset.ts +4 -1
- package/src/server/carbook.ts +8 -0
- package/src/server/cli.ts +17 -5
- package/src/server/main.ts +2 -1
- package/src/server/panecli.ts +216 -0
- package/src/server/procs.ts +28 -12
- package/src/server/procscli.ts +15 -14
- package/src/server/procsops.ts +7 -1
- package/src/server/towers.ts +1 -1
- package/src/server/traincmds.ts +76 -4
- package/src/server/ward.ts +223 -0
- package/src/server/wardcli.ts +8 -0
- package/src/server/watches.ts +68 -27
- package/src/server/watchesops.ts +59 -8
- package/src/tools.ts +1 -1
- package/src/traincar.ts +22 -6
- package/src/wardtool.ts +72 -0
|
@@ -175,10 +175,10 @@ var HerdrError = class extends Error {
|
|
|
175
175
|
var TTL_MS = 10 * 6e4;
|
|
176
176
|
var START_MS = 3e4;
|
|
177
177
|
var START_ATTEMPTS = 20;
|
|
178
|
-
var herdrRun = (args, timeoutMs = 5e3) => new Promise((
|
|
178
|
+
var herdrRun = (args, timeoutMs = 5e3) => new Promise((resolve5, reject) => {
|
|
179
179
|
execFileExe(process.env.HERDR_BIN_PATH || "herdr", args, { deadline: timeoutMs }, (error, stdout, stderr) => {
|
|
180
180
|
if (error) reject(Object.assign(new Error(`herdr ${args.join(" ")} failed: ${stderr.trim() || error.message}`), { stderr: stderr.trim(), code: error.code, killed: error.killed === true, signal: error.signal ?? void 0 }));
|
|
181
|
-
else
|
|
181
|
+
else resolve5(stdout);
|
|
182
182
|
});
|
|
183
183
|
});
|
|
184
184
|
async function result(run4, args, timeoutMs) {
|
|
@@ -233,12 +233,12 @@ var herdrCall = (socketPath = herdrSocket()) => {
|
|
|
233
233
|
return await once(method, params, timeoutMs);
|
|
234
234
|
} catch (error) {
|
|
235
235
|
if (!READS.has(method) || !(error instanceof HerdrError) || !retried(error)) throw error;
|
|
236
|
-
await new Promise((
|
|
236
|
+
await new Promise((resolve5) => setTimeout(resolve5, RETRY_MS));
|
|
237
237
|
return once(method, params, timeoutMs);
|
|
238
238
|
}
|
|
239
239
|
};
|
|
240
240
|
};
|
|
241
|
-
var callOnce = (socketPath) => (method, params, timeoutMs = 5e3) => new Promise((
|
|
241
|
+
var callOnce = (socketPath) => (method, params, timeoutMs = 5e3) => new Promise((resolve5, reject) => {
|
|
242
242
|
const socket = createConnection(localEndpoint(socketPath)), text4 = new StringDecoder("utf8");
|
|
243
243
|
let reply = "";
|
|
244
244
|
const fail = (code, message) => (clearTimeout(timer), socket.destroy(), reject(new HerdrError(code, `herdr ${method} failed: ${code} ${message}`.trim())));
|
|
@@ -253,7 +253,7 @@ var callOnce = (socketPath) => (method, params, timeoutMs = 5e3) => new Promise(
|
|
|
253
253
|
try {
|
|
254
254
|
const { result: result2, error } = JSON.parse(reply.split("\n")[0]);
|
|
255
255
|
if (error) fail(error.code ?? "herdr_error", error.message ?? "");
|
|
256
|
-
else
|
|
256
|
+
else resolve5(result2);
|
|
257
257
|
} catch {
|
|
258
258
|
fail("bad_frame", reply.slice(0, 200));
|
|
259
259
|
}
|
|
@@ -613,11 +613,11 @@ function verified(result2, dir) {
|
|
|
613
613
|
}
|
|
614
614
|
function applyAcl(dir, exec = run, env = process.env, pid = process.pid) {
|
|
615
615
|
const { file, args, options } = aclCommand(dir, env, ACL_MS, pid);
|
|
616
|
-
return new Promise((
|
|
616
|
+
return new Promise((resolve5, reject) => {
|
|
617
617
|
exec(file, args, options, (error, stdout, stderr) => {
|
|
618
618
|
if (error) return reject(new AclError(`could not set the ACL of ${dir}: ${execFailure(error, stderr, stdout)}`));
|
|
619
619
|
try {
|
|
620
|
-
|
|
620
|
+
resolve5(verified(parseAclOutput(stdout), dir));
|
|
621
621
|
} catch (failure) {
|
|
622
622
|
reject(failure instanceof AclError ? failure : new AclError(`could not read the ACL of ${dir}: ${failure.message}`));
|
|
623
623
|
}
|
|
@@ -644,7 +644,7 @@ function readMarker(text4, dir, pid) {
|
|
|
644
644
|
}
|
|
645
645
|
async function awaitAclMarker(dir, token, pid, deps = {}) {
|
|
646
646
|
if (!ACL_TOKEN.test(token)) throw new AclError("the token of the ACL check is not 32 hex digits");
|
|
647
|
-
const now = deps.now ?? Date.now, sleep = deps.sleep ?? ((ms) => new Promise((
|
|
647
|
+
const now = deps.now ?? Date.now, sleep = deps.sleep ?? ((ms) => new Promise((resolve5) => setTimeout(resolve5, ms))), waitMs = deps.waitMs ?? MARKER_MS;
|
|
648
648
|
const file = aclMarkerPath(dir, token), end = now() + waitMs;
|
|
649
649
|
for (; ; ) {
|
|
650
650
|
let text4;
|
|
@@ -744,17 +744,17 @@ function createStartOf({ platform = process.platform, ps = (pid) => String(execF
|
|
|
744
744
|
const remember2 = (pid, value) => {
|
|
745
745
|
if (!valid(pid) || !value || !ISO_START.test(value)) return;
|
|
746
746
|
known2.set(pid, { value, at: now(), fails: 0 });
|
|
747
|
-
for (const
|
|
747
|
+
for (const resolve5 of waiting.get(pid) ?? []) resolve5(value);
|
|
748
748
|
waiting.delete(pid);
|
|
749
749
|
};
|
|
750
750
|
const startedOrWait2 = (pid, graceMs) => {
|
|
751
751
|
if (platform !== "win32") return Promise.resolve(startOf2(pid));
|
|
752
752
|
const seen = fresh(pid);
|
|
753
753
|
if (seen) return Promise.resolve(seen.value);
|
|
754
|
-
return new Promise((
|
|
755
|
-
const timer = setTimeout(() => void ask2(pid).then(
|
|
754
|
+
return new Promise((resolve5) => {
|
|
755
|
+
const timer = setTimeout(() => void ask2(pid).then(resolve5), graceMs);
|
|
756
756
|
timer.unref();
|
|
757
|
-
const told = (value) => (clearTimeout(timer),
|
|
757
|
+
const told = (value) => (clearTimeout(timer), resolve5(value));
|
|
758
758
|
waiting.set(pid, [...waiting.get(pid) ?? [], told]);
|
|
759
759
|
});
|
|
760
760
|
};
|
|
@@ -1699,11 +1699,11 @@ var runNative = (name, args, timeoutMs = NATIVE_MS, extraEnv) => {
|
|
|
1699
1699
|
return execFileSync2(file, argv, options);
|
|
1700
1700
|
};
|
|
1701
1701
|
function execAsync(file, args, { timeout, ...options }) {
|
|
1702
|
-
return new Promise((
|
|
1702
|
+
return new Promise((resolve5, reject) => {
|
|
1703
1703
|
let timedOut = false;
|
|
1704
1704
|
const child = execFile4(file, args, { encoding: "utf8", ...options }, (error, stdout, stderr) => {
|
|
1705
1705
|
clearTimeout(timer);
|
|
1706
|
-
if (!error) return
|
|
1706
|
+
if (!error) return resolve5(stdout);
|
|
1707
1707
|
const code = error.code;
|
|
1708
1708
|
reject(Object.assign(error, { status: typeof code === "number" ? code : null, stdout, stderr, ...timedOut ? { code: "ETIMEDOUT", syscall: `spawn ${file}` } : {} }));
|
|
1709
1709
|
});
|
|
@@ -1907,9 +1907,9 @@ async function ownerGate(dir, run4 = runNativeAsync, timeoutMs, now = Date.now,
|
|
|
1907
1907
|
if (before && before.id === id && (before.retryAt === void 0 || now() < before.retryAt)) throw before.error;
|
|
1908
1908
|
const held2 = open.get(key2);
|
|
1909
1909
|
if (held2 && held2.id === id && now() < held2.retryAt && held2.seen === seenOf(also)) return false;
|
|
1910
|
-
const
|
|
1911
|
-
if (
|
|
1912
|
-
await
|
|
1910
|
+
const under2 = reading.get(key2);
|
|
1911
|
+
if (under2 && under2.id === id) {
|
|
1912
|
+
await under2.done;
|
|
1913
1913
|
return false;
|
|
1914
1914
|
}
|
|
1915
1915
|
const read = readOwner(dir, also, key2, id, run4, timeoutMs, now);
|
|
@@ -2318,7 +2318,7 @@ function holderAlive(info, deps = {}) {
|
|
|
2318
2318
|
return now === void 0 || now === info.started;
|
|
2319
2319
|
}
|
|
2320
2320
|
var SELF_START_GRACE_MS = 2e4;
|
|
2321
|
-
var pause = (ms) => new Promise((
|
|
2321
|
+
var pause = (ms) => new Promise((resolve5) => setTimeout(resolve5, ms));
|
|
2322
2322
|
async function takeLock(slot, by, deps = {}, waitMs = 0) {
|
|
2323
2323
|
const now = deps.now ?? Date.now, sleep = deps.sleep ?? pause, lock = deps.files?.lock ?? fileOf(slot, "lock"), marker = deps.files?.marker ?? fileOf(slot, "stale"), maxMs = deps.maxMs ?? LOCK_MAX_MS;
|
|
2324
2324
|
const self = deps.self ?? { pid: process.pid, started: () => startedOrWait(process.pid, SELF_START_GRACE_MS) };
|
|
@@ -2640,14 +2640,14 @@ function listenControl({ endpoint, secret, hello, handle, refused: refused3, idl
|
|
|
2640
2640
|
}
|
|
2641
2641
|
});
|
|
2642
2642
|
});
|
|
2643
|
-
return acceptsConnections(endpoint, platform).then((live) => new Promise((
|
|
2643
|
+
return acceptsConnections(endpoint, platform).then((live) => new Promise((resolve5, reject) => {
|
|
2644
2644
|
if (live) return reject(new Error(`a live server already listens at ${endpoint}`));
|
|
2645
2645
|
if (platform !== "win32") rmSync7(endpoint, { force: true });
|
|
2646
2646
|
server.once("error", reject);
|
|
2647
2647
|
server.listen(endpoint, () => {
|
|
2648
2648
|
server.off("error", reject);
|
|
2649
2649
|
if (platform !== "win32") chmodSync3(endpoint, 384);
|
|
2650
|
-
|
|
2650
|
+
resolve5({
|
|
2651
2651
|
endpoint,
|
|
2652
2652
|
close: (graceful = false) => new Promise((done) => {
|
|
2653
2653
|
for (const conn of sockets) {
|
|
@@ -2663,11 +2663,11 @@ function listenControl({ endpoint, secret, hello, handle, refused: refused3, idl
|
|
|
2663
2663
|
}
|
|
2664
2664
|
function acceptsConnections(endpoint, platform) {
|
|
2665
2665
|
if (platform === "win32") return Promise.resolve(false);
|
|
2666
|
-
return new Promise((
|
|
2666
|
+
return new Promise((resolve5) => {
|
|
2667
2667
|
const conn = createConnection2(endpoint).setTimeout(PROBE_MS);
|
|
2668
|
-
conn.on("connect", () => (conn.destroy(),
|
|
2669
|
-
conn.on("timeout", () => (conn.destroy(),
|
|
2670
|
-
conn.on("error", () =>
|
|
2668
|
+
conn.on("connect", () => (conn.destroy(), resolve5(true)));
|
|
2669
|
+
conn.on("timeout", () => (conn.destroy(), resolve5(true)));
|
|
2670
|
+
conn.on("error", () => resolve5(false));
|
|
2671
2671
|
});
|
|
2672
2672
|
}
|
|
2673
2673
|
var ControlError = class extends Error {
|
|
@@ -2681,7 +2681,7 @@ var ControlError = class extends Error {
|
|
|
2681
2681
|
}
|
|
2682
2682
|
};
|
|
2683
2683
|
function callControl(endpoint, secret, request2, timeoutMs = 1e4) {
|
|
2684
|
-
return new Promise((
|
|
2684
|
+
return new Promise((resolve5, reject) => {
|
|
2685
2685
|
const want = request2 ? 2 : 1, got = [];
|
|
2686
2686
|
let buffer = "", done = false, sent = false, proven = false, lines = 0;
|
|
2687
2687
|
const clientNonce = randomBytes7(16).toString("hex");
|
|
@@ -2692,7 +2692,7 @@ function callControl(endpoint, secret, request2, timeoutMs = 1e4) {
|
|
|
2692
2692
|
clearTimeout(early);
|
|
2693
2693
|
conn.destroy();
|
|
2694
2694
|
if (error) reject(error);
|
|
2695
|
-
else
|
|
2695
|
+
else resolve5({ hello: got[0], ...got[1] ? { reply: got[1] } : {} });
|
|
2696
2696
|
};
|
|
2697
2697
|
const early = setTimeout(() => finish(new ControlError("timeout", `nothing answered the hello within ${Math.min(timeoutMs, HELLO_MS) / 1e3} s`)), Math.min(timeoutMs, HELLO_MS));
|
|
2698
2698
|
early.unref();
|
|
@@ -2894,8 +2894,8 @@ var run2 = (file, args, options, done) => execFile5(file, args, options, (error,
|
|
|
2894
2894
|
function spawnBreakaway(spec, deps) {
|
|
2895
2895
|
const exec = deps.exec ?? run2, alive2 = deps.alive ?? alive, now = deps.now ?? Date.now, t0 = now();
|
|
2896
2896
|
let timer, released = false, resolveExit, resolveStarted;
|
|
2897
|
-
const exited = new Promise((
|
|
2898
|
-
const started = new Promise((
|
|
2897
|
+
const exited = new Promise((resolve5) => resolveExit = resolve5);
|
|
2898
|
+
const started = new Promise((resolve5) => resolveStarted = resolve5);
|
|
2899
2899
|
const marker = spec.acl ? aclMarkerPath(spec.acl.dir, spec.acl.token) : void 0;
|
|
2900
2900
|
const failMarker = (text4) => {
|
|
2901
2901
|
if (!marker || existsSync4(marker)) return;
|
|
@@ -3055,7 +3055,7 @@ var spawnDetached = (spec) => {
|
|
|
3055
3055
|
child.unref();
|
|
3056
3056
|
noteStarted(child.pid);
|
|
3057
3057
|
child.once("exit", () => forgetStarted(child.pid));
|
|
3058
|
-
return { pid: child.pid, exited: new Promise((
|
|
3058
|
+
return { pid: child.pid, exited: new Promise((resolve5) => (child.once("exit", (code) => resolve5(code)), child.once("error", () => resolve5(-1)))) };
|
|
3059
3059
|
};
|
|
3060
3060
|
var isIdentity = (key2) => IDENTITY.test(key2);
|
|
3061
3061
|
var IDENTITY = /^(PI_INTERCOM_SESSION_ID|PI_INTERCOM_STABLE_ID|PI_SUBAGENT_.*|STRAW_BOSS_ROLE|STRAW_BOSS_OWNER|HERDR_PANE_ID|HERDR_TAB_ID|HERDR_WORKSPACE_ID)$/;
|
|
@@ -3068,9 +3068,9 @@ function intercomOf(env, platform = process.platform) {
|
|
|
3068
3068
|
var sameBroker = (a, b) => (a.scope ?? "") === (b.scope ?? "") && (a.transport ?? "default") === (b.transport ?? "default");
|
|
3069
3069
|
function makeCtx(over) {
|
|
3070
3070
|
const now = over.now ?? Date.now, alive2 = over.alive ?? alive, start = over.startOf ?? startOf;
|
|
3071
|
-
const sleep = over.sleep ?? ((ms, signal) => new Promise((
|
|
3072
|
-
const timer = setTimeout(
|
|
3073
|
-
signal?.addEventListener("abort", () => (clearTimeout(timer),
|
|
3071
|
+
const sleep = over.sleep ?? ((ms, signal) => new Promise((resolve5) => {
|
|
3072
|
+
const timer = setTimeout(resolve5, ms);
|
|
3073
|
+
signal?.addEventListener("abort", () => (clearTimeout(timer), resolve5()), { once: true });
|
|
3074
3074
|
}));
|
|
3075
3075
|
const log = over.log ?? createLog(fileOf(over.slot, "log"), { now });
|
|
3076
3076
|
if (!over.spawn) guardHome({ cwd: over.slot.dir, env: over.env ?? process.env });
|
|
@@ -3165,11 +3165,11 @@ async function passGate(ctx, deadline) {
|
|
|
3165
3165
|
}
|
|
3166
3166
|
}
|
|
3167
3167
|
var aclRefusal = (ctx, error) => error instanceof GateTimeout ? new ServerError("start-failed", error.message, void 0, "start") : new ServerError("start-failed", `acl: ${error.message}`, { at: new Date(ctx.now()).toISOString(), code: "acl", text: error.message });
|
|
3168
|
-
var herdrListens = (ctx, ms = 2e3) => new Promise((
|
|
3168
|
+
var herdrListens = (ctx, ms = 2e3) => new Promise((resolve5) => {
|
|
3169
3169
|
const conn = createConnection3(localEndpoint(ctx.slot.herdr, ctx.platform)).setTimeout(ms);
|
|
3170
|
-
conn.on("connect", () => (conn.destroy(),
|
|
3171
|
-
conn.on("timeout", () => (conn.destroy(),
|
|
3172
|
-
conn.on("error", () =>
|
|
3170
|
+
conn.on("connect", () => (conn.destroy(), resolve5(true)));
|
|
3171
|
+
conn.on("timeout", () => (conn.destroy(), resolve5(false)));
|
|
3172
|
+
conn.on("error", () => resolve5(false));
|
|
3173
3173
|
});
|
|
3174
3174
|
function decide(ctx, seen) {
|
|
3175
3175
|
const { record } = seen, live = seen.live;
|
|
@@ -3227,9 +3227,9 @@ async function launch(ctx, { began, old, deadline } = {}) {
|
|
|
3227
3227
|
clearStartError(ctx.slot);
|
|
3228
3228
|
let keeps = old, mine;
|
|
3229
3229
|
if (!began) {
|
|
3230
|
-
const
|
|
3231
|
-
mine = { began:
|
|
3232
|
-
keeps =
|
|
3230
|
+
const marked2 = markStarting(ctx.slot, ctx.now(), { pid: process.pid, version: ctx.version, desk: ctx.env.STRAW_BOSS_DESK === "1", intercom: intercomOf(ctx.env, ctx.platform), mode: "detached", by: ctx.by, prepare: (slot) => secretDir(slot.dir, dirname3(slot.stateDir)) });
|
|
3231
|
+
mine = { began: marked2.began, expect: marked2.expect };
|
|
3232
|
+
keeps = marked2.expect;
|
|
3233
3233
|
}
|
|
3234
3234
|
const child = ctx.spawn({ ...specOf(ctx), timeoutMs: waitMs });
|
|
3235
3235
|
if (mine && child.pid !== void 0) {
|
|
@@ -3237,7 +3237,7 @@ async function launch(ctx, { began, old, deadline } = {}) {
|
|
|
3237
3237
|
keeps = mine.expect;
|
|
3238
3238
|
}
|
|
3239
3239
|
ctx.log.line("start-requested", { by: ctx.by, version: ctx.version, pid: child.pid, spawnMs: ctx.now() - t0 });
|
|
3240
|
-
let note2,
|
|
3240
|
+
let note2, refusal2, pidUnknown = child.pid === void 0 && child.started !== void 0;
|
|
3241
3241
|
void child.started?.then((s) => {
|
|
3242
3242
|
ctx.log.line("phase", { name: "breakaway", ms: s.ms, via: s.via, pid: s.pid, ...s.breakaway === void 0 ? {} : { outsideJob: s.breakaway }, createMs: s.createMs, aclMs: s.aclMs });
|
|
3243
3243
|
if (s.serverStarted && s.pid > 0) rememberStart(s.pid, s.serverStarted);
|
|
@@ -3245,7 +3245,7 @@ async function launch(ctx, { began, old, deadline } = {}) {
|
|
|
3245
3245
|
if (s.markerError) ctx.log.line("error", { code: "acl-marker", text: s.markerError });
|
|
3246
3246
|
if (s.failure) ctx.log.line("breakaway-failed", { via: s.via, text: s.failure });
|
|
3247
3247
|
if (s.via === "failed") note2 = s.failure;
|
|
3248
|
-
if (s.via === "refused")
|
|
3248
|
+
if (s.via === "refused") refusal2 = s.failure;
|
|
3249
3249
|
pidUnknown = false;
|
|
3250
3250
|
}, () => void (pidUnknown = false));
|
|
3251
3251
|
try {
|
|
@@ -3257,10 +3257,10 @@ async function launch(ctx, { began, old, deadline } = {}) {
|
|
|
3257
3257
|
}
|
|
3258
3258
|
if (mine) endStarting(ctx.slot, mine.began, mine.expect);
|
|
3259
3259
|
if (!outcome && !note2 && child.started) await Promise.race([child.started, ctx.sleep(100)]);
|
|
3260
|
-
if (
|
|
3261
|
-
ctx.log.line("start-failed", { code: "acl", text:
|
|
3262
|
-
const startError = { at: new Date(ctx.now()).toISOString(), code: "acl", text:
|
|
3263
|
-
throw new ServerError("start-failed", `acl: ${
|
|
3260
|
+
if (refusal2) {
|
|
3261
|
+
ctx.log.line("start-failed", { code: "acl", text: refusal2 });
|
|
3262
|
+
const startError = { at: new Date(ctx.now()).toISOString(), code: "acl", text: refusal2 };
|
|
3263
|
+
throw new ServerError("start-failed", `acl: ${refusal2}`, startError);
|
|
3264
3264
|
}
|
|
3265
3265
|
ctx.log.line("start-unanswered", { ms: ctx.now() - t0, exit: outcome ? String(outcome.exit) : "none" });
|
|
3266
3266
|
throw failed(ctx, outcome, waitMs, note2);
|
|
@@ -4283,8 +4283,8 @@ function parsePs(text4) {
|
|
|
4283
4283
|
}
|
|
4284
4284
|
return table;
|
|
4285
4285
|
}
|
|
4286
|
-
var run3 = (file, args) => new Promise((
|
|
4287
|
-
execFileExe(file, args, { encoding: "utf8", maxBuffer: 1 << 26, deadline: 15e3, env: { ...process.env, LC_ALL: "C", TZ: "UTC" } }, (error, stdout) => error && !stdout ? reject(error) :
|
|
4286
|
+
var run3 = (file, args) => new Promise((resolve5, reject) => {
|
|
4287
|
+
execFileExe(file, args, { encoding: "utf8", maxBuffer: 1 << 26, deadline: 15e3, env: { ...process.env, LC_ALL: "C", TZ: "UTC" } }, (error, stdout) => error && !stdout ? reject(error) : resolve5(stdout));
|
|
4288
4288
|
});
|
|
4289
4289
|
function exeName(link) {
|
|
4290
4290
|
const name = basename3(link.endsWith(" (deleted)") ? link.slice(0, -" (deleted)".length) : link);
|
|
@@ -4451,7 +4451,7 @@ async function killSequence(refs, deps) {
|
|
|
4451
4451
|
}
|
|
4452
4452
|
};
|
|
4453
4453
|
const covered = /* @__PURE__ */ new Set();
|
|
4454
|
-
for (const group of wholeGroups(targets, all, self)) {
|
|
4454
|
+
for (const group of deps.groups === false ? [] : wholeGroups(targets, all, self)) {
|
|
4455
4455
|
const members = all.filter((p) => p.pgid === group);
|
|
4456
4456
|
if (send1(-group, members)) report2.groups += signal === "SIGTERM" ? 1 : 0, members.forEach((m) => covered.add(m.pid));
|
|
4457
4457
|
}
|
|
@@ -5976,8 +5976,8 @@ function checkCar(slip, evidence, o) {
|
|
|
5976
5976
|
}
|
|
5977
5977
|
function parseAppAccept(value) {
|
|
5978
5978
|
if (value === void 0) return { ok: true, accept: [] };
|
|
5979
|
-
const
|
|
5980
|
-
if (!Array.isArray(value) || !value.every((k) => typeof k === "string" && /^[a-z0-9-]{1,32}$/.test(k)) || new Set(value).size !== value.length) return { ok: false, refusal };
|
|
5979
|
+
const refusal2 = "sb: integration.accept must be a list of kind names (a-z, 0-9, -; 1 to 32 characters; no repeats)";
|
|
5980
|
+
if (!Array.isArray(value) || !value.every((k) => typeof k === "string" && /^[a-z0-9-]{1,32}$/.test(k)) || new Set(value).size !== value.length) return { ok: false, refusal: refusal2 };
|
|
5981
5981
|
return { ok: true, accept: [...value] };
|
|
5982
5982
|
}
|
|
5983
5983
|
function checkStages(stages) {
|
|
@@ -6813,8 +6813,8 @@ function addState(file, set) {
|
|
|
6813
6813
|
const refusals = validateStateFields(set.state, set.fields);
|
|
6814
6814
|
if (refusals.length) throw new Error(refusals.join("; "));
|
|
6815
6815
|
if (!isoTime(set.at)) throw new Error("sb: at must be an ISO time with its zone");
|
|
6816
|
-
const
|
|
6817
|
-
if (
|
|
6816
|
+
const refusal2 = sequence(file.states, set.state, label(file));
|
|
6817
|
+
if (refusal2) throw new Error(refusal2);
|
|
6818
6818
|
return { ...file, states: [...file.states, { state: set.state, at: set.at, by: { ...set.by }, ...set.fields }] };
|
|
6819
6819
|
}
|
|
6820
6820
|
function validateCarFile(value) {
|
|
@@ -7960,6 +7960,656 @@ function sanitizeReply(kind, status, body, rw) {
|
|
|
7960
7960
|
return out === void 0 ? { ok: false, why: "shape" } : { ok: true, status, body: out };
|
|
7961
7961
|
}
|
|
7962
7962
|
|
|
7963
|
+
// src/procs/worktree.ts
|
|
7964
|
+
import { readFileSync as readFileSync25, realpathSync as realpathSync5, statSync as statSync14 } from "node:fs";
|
|
7965
|
+
import { basename as basename9, dirname as dirname14, join as join28, relative as relative3, isAbsolute as isAbsolute2, resolve as resolve4 } from "node:path";
|
|
7966
|
+
var realDisk = {
|
|
7967
|
+
kind: (path) => {
|
|
7968
|
+
try {
|
|
7969
|
+
return statSync14(path).isDirectory() ? "dir" : "file";
|
|
7970
|
+
} catch {
|
|
7971
|
+
return void 0;
|
|
7972
|
+
}
|
|
7973
|
+
},
|
|
7974
|
+
read: (path) => {
|
|
7975
|
+
try {
|
|
7976
|
+
return readFileSync25(path, "utf8");
|
|
7977
|
+
} catch {
|
|
7978
|
+
return void 0;
|
|
7979
|
+
}
|
|
7980
|
+
}
|
|
7981
|
+
};
|
|
7982
|
+
function physical(path) {
|
|
7983
|
+
const abs = resolve4(path), tail = [];
|
|
7984
|
+
for (let dir = abs; ; dir = dirname14(dir)) {
|
|
7985
|
+
try {
|
|
7986
|
+
return join28(realpathSync5.native(dir), ...tail.reverse());
|
|
7987
|
+
} catch {
|
|
7988
|
+
if (dirname14(dir) === dir) return abs;
|
|
7989
|
+
tail.push(basename9(dir));
|
|
7990
|
+
}
|
|
7991
|
+
}
|
|
7992
|
+
}
|
|
7993
|
+
function checkoutOf(cwd, disk = realDisk, real3 = physical) {
|
|
7994
|
+
for (let dir = real3(cwd); ; dir = dirname14(dir)) {
|
|
7995
|
+
const git = join28(dir, ".git"), kind = disk.kind(git);
|
|
7996
|
+
if (kind === "dir") return { kind: "main", root: dir };
|
|
7997
|
+
if (kind === "file") {
|
|
7998
|
+
const gitdir = /^gitdir:\s*(.+?)\s*$/m.exec(disk.read(git) ?? "")?.[1];
|
|
7999
|
+
if (gitdir && /[\\/]worktrees[\\/][^\\/]+[\\/]?$/.test(gitdir)) {
|
|
8000
|
+
const admin = resolve4(dir, gitdir), back = disk.read(join28(admin, "gitdir"))?.trim();
|
|
8001
|
+
if (disk.kind(admin) === "dir" && back && real3(resolve4(admin, back)) === join28(dir, ".git")) return { kind: "linked", root: dir, gitdir };
|
|
8002
|
+
return { kind: "none" };
|
|
8003
|
+
}
|
|
8004
|
+
if (gitdir && /[\\/]modules[\\/]/.test(gitdir)) return { kind: "submodule", root: dir };
|
|
8005
|
+
return { kind: "none" };
|
|
8006
|
+
}
|
|
8007
|
+
if (dirname14(dir) === dir) return { kind: "none" };
|
|
8008
|
+
}
|
|
8009
|
+
}
|
|
8010
|
+
function under(path, root) {
|
|
8011
|
+
const rel = relative3(resolve4(root), resolve4(path));
|
|
8012
|
+
return rel === "" || !rel.startsWith("..") && !isAbsolute2(rel);
|
|
8013
|
+
}
|
|
8014
|
+
function rootOfRemoved(path, roots) {
|
|
8015
|
+
return roots.filter((root) => under(path, root)).sort((a, b) => b.length - a.length)[0];
|
|
8016
|
+
}
|
|
8017
|
+
|
|
8018
|
+
// src/procs/killset.ts
|
|
8019
|
+
var overlaps = (a, b) => under(a, b) || under(b, a);
|
|
8020
|
+
function killSet({ owner, live, table, places, markers, protect, reapers, self, held: held2 = [], ownUid, children = "all" }) {
|
|
8021
|
+
const byPid = new Map(table.map((p) => [p.pid, p]));
|
|
8022
|
+
const spare = /* @__PURE__ */ new Map();
|
|
8023
|
+
const hold = (p, why2) => p && !spare.has(p.pid) && spare.set(p.pid, why2);
|
|
8024
|
+
hold(byPid.get(1), "init");
|
|
8025
|
+
for (const pid of self) hold(byPid.get(pid), "self");
|
|
8026
|
+
for (const r of reapers) hold(byPid.get(r), "reaper");
|
|
8027
|
+
for (const p of protect) {
|
|
8028
|
+
const proc = byPid.get(p.pid);
|
|
8029
|
+
if (!proc || p.started !== void 0 && p.started !== proc.started) continue;
|
|
8030
|
+
hold(proc, p.why);
|
|
8031
|
+
if (p.tree) for (const kid of descendants(table, [p.pid])) hold(kid, p.why);
|
|
8032
|
+
}
|
|
8033
|
+
for (const other of live) {
|
|
8034
|
+
const proc = byPid.get(other.pid);
|
|
8035
|
+
if (proc && proc.started === other.started) {
|
|
8036
|
+
hold(proc, "live-owner");
|
|
8037
|
+
for (const kid of descendants(table, [other.pid])) hold(kid, "live-owner");
|
|
8038
|
+
}
|
|
8039
|
+
for (const ref of other.snapshot) {
|
|
8040
|
+
const alive2 = byPid.get(ref.pid);
|
|
8041
|
+
if (alive2 && sameProc(refOf(alive2), ref) && !alive2.zombie) hold(alive2, "live-owner");
|
|
8042
|
+
}
|
|
8043
|
+
}
|
|
8044
|
+
const liveIds = new Set(live.map((o) => o.id));
|
|
8045
|
+
for (const p of table) {
|
|
8046
|
+
const marker = markers.get(p.pid);
|
|
8047
|
+
if (marker !== void 0 && liveIds.has(marker)) hold(p, "live-owner");
|
|
8048
|
+
}
|
|
8049
|
+
const others = [...live.flatMap((o) => o.roots), ...held2];
|
|
8050
|
+
const roots = owner.roots.filter((root) => !others.some((other) => overlaps(root, other)));
|
|
8051
|
+
const inRoot = (place) => place.removed ? rootOfRemoved(place.path, roots) !== void 0 : roots.some((root) => under(place.path, root));
|
|
8052
|
+
const why = /* @__PURE__ */ new Map();
|
|
8053
|
+
const add = (p, reason) => {
|
|
8054
|
+
if (!p || p.zombie || why.has(p.pid) || ownUid !== void 0 && p.uid !== ownUid) return;
|
|
8055
|
+
if (spare.has(p.pid)) return;
|
|
8056
|
+
why.set(p.pid, reason);
|
|
8057
|
+
};
|
|
8058
|
+
for (const ref of owner.snapshot) {
|
|
8059
|
+
const p = byPid.get(ref.pid);
|
|
8060
|
+
if (p && sameProc(refOf(p), ref)) add(p, "snapshot");
|
|
8061
|
+
}
|
|
8062
|
+
for (const p of table) if (markers.get(p.pid) === owner.id) add(p, "marker");
|
|
8063
|
+
for (const p of table) {
|
|
8064
|
+
const place = places.get(p.pid);
|
|
8065
|
+
if (!place || !isOrphan(p, reapers)) continue;
|
|
8066
|
+
if (inRoot(place)) add(p, "cwd");
|
|
8067
|
+
}
|
|
8068
|
+
const kids = /* @__PURE__ */ new Map();
|
|
8069
|
+
for (const p of table) if (p.pid !== p.ppid) kids.set(p.ppid, [...kids.get(p.ppid) ?? [], p]);
|
|
8070
|
+
const walked = /* @__PURE__ */ new Set();
|
|
8071
|
+
const walk = (pid) => {
|
|
8072
|
+
for (const kid of kids.get(pid) ?? []) {
|
|
8073
|
+
if (walked.has(kid.pid) || spare.has(kid.pid)) continue;
|
|
8074
|
+
if (children === "placed" && !((place) => place && inRoot(place))(places.get(kid.pid))) continue;
|
|
8075
|
+
walked.add(kid.pid);
|
|
8076
|
+
add(kid, "child");
|
|
8077
|
+
walk(kid.pid);
|
|
8078
|
+
}
|
|
8079
|
+
};
|
|
8080
|
+
for (const pid of [...why.keys()]) walk(pid);
|
|
8081
|
+
const targets = [...why].sort(([a], [b]) => a - b).map(([pid, reason]) => ({ proc: byPid.get(pid), why: reason }));
|
|
8082
|
+
const reached = /* @__PURE__ */ new Set();
|
|
8083
|
+
for (const ref of owner.snapshot) reached.add(ref.pid);
|
|
8084
|
+
for (const p of table) {
|
|
8085
|
+
if (markers.get(p.pid) === owner.id) reached.add(p.pid);
|
|
8086
|
+
const place = places.get(p.pid);
|
|
8087
|
+
if (place && isOrphan(p, reapers) && inRoot(place)) reached.add(p.pid);
|
|
8088
|
+
if (why.has(p.ppid)) reached.add(p.pid);
|
|
8089
|
+
}
|
|
8090
|
+
const spared = [...spare].filter(([pid]) => reached.has(pid)).sort(([a], [b]) => a - b).map(([pid, reason]) => ({ proc: byPid.get(pid), why: reason }));
|
|
8091
|
+
return { targets, spared };
|
|
8092
|
+
}
|
|
8093
|
+
|
|
8094
|
+
// src/server/procs.ts
|
|
8095
|
+
import { existsSync as existsSync12, mkdirSync as mkdirSync15, readdirSync as readdirSync16, readFileSync as readFileSync27, renameSync as renameSync16 } from "node:fs";
|
|
8096
|
+
import { join as join29 } from "node:path";
|
|
8097
|
+
|
|
8098
|
+
// src/procs/marker.ts
|
|
8099
|
+
import { readFileSync as readFileSync26 } from "node:fs";
|
|
8100
|
+
var OWNER_KEY = "STRAW_BOSS_OWNER";
|
|
8101
|
+
var ID = /^[A-Za-z0-9._-]{1,64}$/;
|
|
8102
|
+
var WORD = new RegExp(`(?:^|\\s)${OWNER_KEY}=([^\\s]*)(?=\\s|$)`, "g");
|
|
8103
|
+
function ownerOf(text4) {
|
|
8104
|
+
const id = [...text4.matchAll(WORD)].at(-1)?.[1];
|
|
8105
|
+
return id && ID.test(id) ? id : void 0;
|
|
8106
|
+
}
|
|
8107
|
+
function parseOwnerRows(text4, wanted) {
|
|
8108
|
+
const out = /* @__PURE__ */ new Map();
|
|
8109
|
+
for (const line2 of text4.split("\n")) {
|
|
8110
|
+
const m = /^\s*(\d+)\s(.*)$/.exec(line2);
|
|
8111
|
+
const pid = m ? Number(m[1]) : NaN;
|
|
8112
|
+
const id = m && wanted.has(pid) ? ownerOf(m[2]) : void 0;
|
|
8113
|
+
if (id) out.set(pid, id);
|
|
8114
|
+
}
|
|
8115
|
+
return out;
|
|
8116
|
+
}
|
|
8117
|
+
var CHUNK2 = 100;
|
|
8118
|
+
async function readOwners(pids, { run: ask2 = run3, platform = process.platform, environ = (pid) => readFileSync26(`/proc/${pid}/environ`) } = {}) {
|
|
8119
|
+
const out = /* @__PURE__ */ new Map();
|
|
8120
|
+
if (platform === "win32") return out;
|
|
8121
|
+
if (platform === "linux") {
|
|
8122
|
+
for (const pid of pids) {
|
|
8123
|
+
try {
|
|
8124
|
+
const id = environ(pid).toString("utf8").split("\0").map((word) => word.startsWith(`${OWNER_KEY}=`) ? ownerOf(` ${word}`) : void 0).find(Boolean);
|
|
8125
|
+
if (id) out.set(pid, id);
|
|
8126
|
+
} catch {
|
|
8127
|
+
}
|
|
8128
|
+
}
|
|
8129
|
+
return out;
|
|
8130
|
+
}
|
|
8131
|
+
for (let i = 0; i < pids.length; i += CHUNK2) {
|
|
8132
|
+
const chunk = pids.slice(i, i + CHUNK2);
|
|
8133
|
+
let text4 = "";
|
|
8134
|
+
try {
|
|
8135
|
+
text4 = await ask2("ps", ["-E", "-ww", "-o", "pid=,command=", "-p", chunk.join(",")]);
|
|
8136
|
+
} catch (error) {
|
|
8137
|
+
if (error.code !== 1) throw error;
|
|
8138
|
+
}
|
|
8139
|
+
for (const [pid, id] of parseOwnerRows(text4, new Set(chunk))) out.set(pid, id);
|
|
8140
|
+
}
|
|
8141
|
+
return out;
|
|
8142
|
+
}
|
|
8143
|
+
|
|
8144
|
+
// src/server/procs.ts
|
|
8145
|
+
var OWNERS_FILE = "owners.json";
|
|
8146
|
+
var TICK_MS = 1e4;
|
|
8147
|
+
var SLOW_TICK_MS = 3e4;
|
|
8148
|
+
var SLOW_AFTER_MS = 1e3;
|
|
8149
|
+
var ID2 = /^[A-Za-z0-9._-]{1,64}$/;
|
|
8150
|
+
var DEFER_MAX = 3;
|
|
8151
|
+
var SHELLS = /* @__PURE__ */ new Set(["sh", "bash", "zsh", "fish", "dash", "ksh", "csh", "tcsh"]);
|
|
8152
|
+
var SNAPSHOT_MAX = 2e3;
|
|
8153
|
+
var ProcsRefusal = class extends Error {
|
|
8154
|
+
};
|
|
8155
|
+
function createProcs(d) {
|
|
8156
|
+
const folder = join29(d.dir, "procs"), file = join29(folder, OWNERS_FILE);
|
|
8157
|
+
const read = d.read ?? (() => readTable()), cwdsOf = d.cwds ?? ((pids) => readCwds(pids)), markersOf = d.markers ?? ((pids) => readOwners(pids));
|
|
8158
|
+
const inode = d.inode ?? inodeOnDisk, checkout = d.checkout ?? checkoutOf, probe2 = d.startOf ?? startOf, reap2 = d.reap ?? ((refs, options) => reap(refs, options));
|
|
8159
|
+
const platform = d.platform ?? process.platform, self = d.self ?? [process.pid], real3 = d.real ?? physical, exists = d.exists ?? existsSync12;
|
|
8160
|
+
const owners = /* @__PURE__ */ new Map();
|
|
8161
|
+
const pending2 = /* @__PURE__ */ new Set();
|
|
8162
|
+
const linked = /* @__PURE__ */ new Map();
|
|
8163
|
+
const busy = /* @__PURE__ */ new Set();
|
|
8164
|
+
const running = /* @__PURE__ */ new Set();
|
|
8165
|
+
let tickEvery = TICK_MS, ticking;
|
|
8166
|
+
const save = () => {
|
|
8167
|
+
try {
|
|
8168
|
+
mkdirSync15(folder, { recursive: true, mode: 448 });
|
|
8169
|
+
writeJson(file, { v: 1, owners: [...owners.values()] });
|
|
8170
|
+
} catch (error) {
|
|
8171
|
+
d.log("procs-error", { text: `owners.json was not written (${error.code ?? error.name})` });
|
|
8172
|
+
}
|
|
8173
|
+
};
|
|
8174
|
+
const load = () => {
|
|
8175
|
+
try {
|
|
8176
|
+
const parsed = JSON.parse(readFileSync27(file, "utf8"));
|
|
8177
|
+
for (const r of Array.isArray(parsed.owners) ? parsed.owners : []) {
|
|
8178
|
+
const o = r;
|
|
8179
|
+
if (typeof o?.id === "string" && ID2.test(o.id) && Number.isSafeInteger(o.pid) && typeof o.started === "string" && Array.isArray(o.roots) && Array.isArray(o.snapshot)) {
|
|
8180
|
+
owners.set(o.id, { ...o, snapshot: o.snapshot.filter((s) => Number.isSafeInteger(s?.pid) && typeof s?.started === "string") });
|
|
8181
|
+
}
|
|
8182
|
+
}
|
|
8183
|
+
} catch (error) {
|
|
8184
|
+
if (error.code === "ENOENT") return;
|
|
8185
|
+
d.log("procs-error", { text: `owners.json is not readable (${error.name}); starting empty` });
|
|
8186
|
+
try {
|
|
8187
|
+
renameSync16(file, `${file}.bad`);
|
|
8188
|
+
} catch {
|
|
8189
|
+
}
|
|
8190
|
+
}
|
|
8191
|
+
};
|
|
8192
|
+
const owned = (r) => ({ id: r.id, pid: r.pid, started: r.started, roots: r.roots, snapshot: r.snapshot });
|
|
8193
|
+
const alive2 = (table, r) => table.some((p) => !p.zombie && sameProc(refOf(p), r));
|
|
8194
|
+
const rootsOf = (cwd) => {
|
|
8195
|
+
const c = cwd ? checkout(cwd) : void 0;
|
|
8196
|
+
return c?.kind === "linked" ? [c.root] : [];
|
|
8197
|
+
};
|
|
8198
|
+
const overlap = (a, b) => under(a, b) || under(b, a);
|
|
8199
|
+
async function heldRoots(subject) {
|
|
8200
|
+
if (!d.panes) return { roots: [], unknown: false };
|
|
8201
|
+
let panes3;
|
|
8202
|
+
try {
|
|
8203
|
+
panes3 = await d.panes();
|
|
8204
|
+
} catch {
|
|
8205
|
+
}
|
|
8206
|
+
if (!Array.isArray(panes3)) return { roots: [], unknown: true };
|
|
8207
|
+
const roots = [];
|
|
8208
|
+
let unknown2 = false, looked;
|
|
8209
|
+
for (const p of panes3) {
|
|
8210
|
+
if (p.pane_id === subject.pane) continue;
|
|
8211
|
+
if (p.cwd) {
|
|
8212
|
+
roots.push(...rootsOf(p.cwd));
|
|
8213
|
+
continue;
|
|
8214
|
+
}
|
|
8215
|
+
if (!p.workspace_id) {
|
|
8216
|
+
unknown2 = true;
|
|
8217
|
+
continue;
|
|
8218
|
+
}
|
|
8219
|
+
looked ??= await lookAtWorkspaces();
|
|
8220
|
+
const place = linked.get(p.workspace_id);
|
|
8221
|
+
if (place) roots.push(...rootsOf(place));
|
|
8222
|
+
else if (!looked) unknown2 = true;
|
|
8223
|
+
}
|
|
8224
|
+
return { roots, unknown: unknown2 };
|
|
8225
|
+
}
|
|
8226
|
+
async function refuseProtected(pid, table, pane) {
|
|
8227
|
+
const proc = table.find((p) => p.pid === pid && !p.zombie);
|
|
8228
|
+
if (!proc) return;
|
|
8229
|
+
const no3 = (what) => new ProcsRefusal(`procs-hello: pid ${pid} is ${what}, not a pi`);
|
|
8230
|
+
if (SHELLS.has(baseName(proc.comm).replace(/^-/, ""))) throw no3("a shell");
|
|
8231
|
+
if (self.includes(pid)) throw no3("this server");
|
|
8232
|
+
const all = await d.protect?.(table) ?? [];
|
|
8233
|
+
const named2 = all.filter((p) => p.why !== "pane-foreground");
|
|
8234
|
+
const hit = named2.find((p) => p.pid === pid && (p.started === void 0 || p.started === proc.started));
|
|
8235
|
+
if (hit) throw no3(`protected (${hit.why})`);
|
|
8236
|
+
if (pane && all.some((p) => p.why === "pane-foreground" && p.pid === pid && p.pane !== void 0 && p.pane !== pane)) throw no3("the foreground of another pane");
|
|
8237
|
+
const below = new Set(descendants(table, [pid]).map((p) => p.pid));
|
|
8238
|
+
const shells = new Set(named2.filter((p) => p.why === "pane").map((p) => p.pid));
|
|
8239
|
+
const server = (p) => p.why === "herdr" && (shells.size === 0 || descendants(table, [p.pid]).some((c) => shells.has(c.pid)));
|
|
8240
|
+
if (named2.some((p) => (server(p) || p.why === "pane") && below.has(p.pid))) throw no3("above Herdr or a pane's shell");
|
|
8241
|
+
}
|
|
8242
|
+
async function reapSet(subject, reason, explicit = false, bounded) {
|
|
8243
|
+
const table = await read();
|
|
8244
|
+
if (!table) throw new Error("the process table could not be read; nothing was signalled");
|
|
8245
|
+
const live = [...owners.values()].filter((o) => !o.ended && o !== subject && alive2(table, o)).map(owned);
|
|
8246
|
+
const { roots: held2, unknown: unknown2 } = explicit || !subject.roots.length ? { roots: [], unknown: false } : await heldRoots(subject);
|
|
8247
|
+
if (unknown2) d.log("procs-held-unknown", { owner: subject.id, reason });
|
|
8248
|
+
const owner = unknown2 ? { ...owned(subject), roots: [] } : owned(subject);
|
|
8249
|
+
const orphans = owner.roots.length ? table.filter((p) => !p.zombie && isOrphan(p, reapersOf(table))) : [];
|
|
8250
|
+
const found = await cwdsOf(orphans.map((p) => p.pid));
|
|
8251
|
+
const places = /* @__PURE__ */ new Map();
|
|
8252
|
+
for (const [pid, cwd] of found) places.set(pid, { path: cwd.path, removed: classifyCwd(cwd, inode(cwd.path)) === "removed" });
|
|
8253
|
+
const placed = bounded ? descendants(table, [...places].filter(([, at2]) => !at2.removed && owner.roots.some((root) => under(at2.path, root))).map(([pid]) => pid)) : [];
|
|
8254
|
+
for (const [pid, cwd] of placed.length ? await cwdsOf(placed.filter((p) => !p.zombie).map((p) => p.pid)) : []) places.set(pid, { path: cwd.path, removed: classifyCwd(cwd, inode(cwd.path)) === "removed" });
|
|
8255
|
+
const markers = await markersOf(table.filter((p) => !p.zombie).map((p) => p.pid));
|
|
8256
|
+
const protect = await d.protect?.(table) ?? [];
|
|
8257
|
+
const { targets, spared } = killSet({ owner, live, table, places, markers, protect, reapers: reapersOf(table), self: [...self, ...bounded?.spare ?? []], held: held2, ...d.ownUid !== void 0 ? { ownUid: d.ownUid } : {}, ...bounded ? { children: "placed" } : {} });
|
|
8258
|
+
const report2 = targets.length ? await reap2(targets.map((t) => refOf(t.proc)), ...bounded ? [{ groups: false }] : []) : { termed: 0, killed: 0, skipped: 0, groups: 0, left: [] };
|
|
8259
|
+
const result2 = { owner: subject.id, reason, targets: targets.length, termed: report2.termed, killed: report2.killed, skipped: report2.skipped, left: report2.left, spared: spared.length, ...unknown2 ? { deferred: true } : {} };
|
|
8260
|
+
d.log("procs-reaped", { owner: subject.id, reason, targets: targets.length, termed: report2.termed, killed: report2.killed, left: report2.left.length, protected: spared.length, ...unknown2 ? { deferred: true } : {} });
|
|
8261
|
+
return result2;
|
|
8262
|
+
}
|
|
8263
|
+
const reapersOf = (table) => platform === "linux" ? table.filter((p) => p.ppid === 1 && baseName(p.comm) === "systemd").map((p) => p.pid) : [];
|
|
8264
|
+
function end(record, reason) {
|
|
8265
|
+
if (record.ended) return Promise.resolve();
|
|
8266
|
+
record.ended = { at: d.now(), reason };
|
|
8267
|
+
save();
|
|
8268
|
+
d.log("procs-owner-ended", { owner: record.id, reason, pid: record.pid });
|
|
8269
|
+
return reapEnded(record, reason);
|
|
8270
|
+
}
|
|
8271
|
+
function reapEnded(record, reason) {
|
|
8272
|
+
running.add(record);
|
|
8273
|
+
const work = reapSet(record, reason).then(
|
|
8274
|
+
(result2) => {
|
|
8275
|
+
const current2 = owners.get(record.id) === record;
|
|
8276
|
+
if (result2.left.length) {
|
|
8277
|
+
d.log("procs-left", { owner: record.id, count: result2.left.length, programs: result2.left.map((l) => l.program).slice(0, 5).join(",") });
|
|
8278
|
+
record.snapshot = [];
|
|
8279
|
+
if (record.ended) record.ended.left = true;
|
|
8280
|
+
} else if (result2.deferred) {
|
|
8281
|
+
if (record.ended) {
|
|
8282
|
+
record.ended.retry = (record.ended.retry ?? 0) + 1;
|
|
8283
|
+
if (record.ended.retry >= DEFER_MAX) record.ended.left = true, d.log("procs-error", { owner: record.id, text: "the live panes of Herdr stayed unknown; orphans in its worktree are left for procs reap" });
|
|
8284
|
+
}
|
|
8285
|
+
} else if (current2) owners.delete(record.id);
|
|
8286
|
+
if (current2) save();
|
|
8287
|
+
},
|
|
8288
|
+
(error) => {
|
|
8289
|
+
d.log("procs-error", { owner: record.id, text: error.message.slice(0, 200) });
|
|
8290
|
+
record.ended = { at: d.now(), reason: `${reason}; reap failed` };
|
|
8291
|
+
if (owners.get(record.id) === record) save();
|
|
8292
|
+
}
|
|
8293
|
+
).finally(() => void running.delete(record));
|
|
8294
|
+
pending2.add(work);
|
|
8295
|
+
work.finally(() => pending2.delete(work)).catch(() => void 0);
|
|
8296
|
+
return work;
|
|
8297
|
+
}
|
|
8298
|
+
async function reapWorkspace(ws2, pane, reason) {
|
|
8299
|
+
const before = ws2 ? linked.get(ws2) : void 0;
|
|
8300
|
+
if (ws2) await lookAtWorkspaces();
|
|
8301
|
+
const root = ws2 ? rootsOf((ws2 ? linked.get(ws2) : void 0) ?? before)[0] : void 0;
|
|
8302
|
+
if (!root || busy.has(root)) return;
|
|
8303
|
+
busy.add(root);
|
|
8304
|
+
const subject = { id: `(pane ${pane})`.slice(0, 64), pid: -1, started: "", roots: [root], snapshot: [], pane, helloAt: d.now(), seenAt: d.now() };
|
|
8305
|
+
const work = reapSet(subject, reason).then((result2) => void (result2.deferred && d.log("procs-error", { owner: subject.id, text: "the live panes of Herdr are unknown; orphans in the worktree are left for procs reap" })), (error) => d.log("procs-error", { owner: subject.id, text: error.message.slice(0, 200) })).finally(() => void busy.delete(root));
|
|
8306
|
+
pending2.add(work);
|
|
8307
|
+
work.finally(() => pending2.delete(work)).catch(() => void 0);
|
|
8308
|
+
await work;
|
|
8309
|
+
}
|
|
8310
|
+
const lookAtWorkspaces = async () => {
|
|
8311
|
+
const found = await d.workspaces?.().catch(() => void 0);
|
|
8312
|
+
if (!found) return false;
|
|
8313
|
+
linked.clear();
|
|
8314
|
+
for (const w of found) linked.set(w.workspace_id, w.checkout_path);
|
|
8315
|
+
return true;
|
|
8316
|
+
};
|
|
8317
|
+
async function takeTick() {
|
|
8318
|
+
const table = await read();
|
|
8319
|
+
if (!table) return;
|
|
8320
|
+
void d.watch?.(table).catch((error) => d.log("procs-error", { text: `the tick's watch failed: ${String(error?.message).slice(0, 160)}` }));
|
|
8321
|
+
await lookAtWorkspaces();
|
|
8322
|
+
let changed = false;
|
|
8323
|
+
for (const r of [...owners.values()]) {
|
|
8324
|
+
if (r.ended) continue;
|
|
8325
|
+
if (!alive2(table, r)) {
|
|
8326
|
+
void end(r, "pid gone");
|
|
8327
|
+
continue;
|
|
8328
|
+
}
|
|
8329
|
+
const below = descendants(table, [r.pid]).filter((p) => !p.zombie).map(refOf);
|
|
8330
|
+
const kept = r.snapshot.filter((s) => table.some((p) => !p.zombie && sameProc(refOf(p), s)) && !below.some((b) => sameProc(b, s)));
|
|
8331
|
+
const next = [...kept, ...below].slice(0, SNAPSHOT_MAX);
|
|
8332
|
+
if (next.length !== r.snapshot.length || next.some((s, i) => !sameProc(s, r.snapshot[i] ?? { pid: -1, started: "" }))) r.snapshot = next, changed = true;
|
|
8333
|
+
r.seenAt = d.now();
|
|
8334
|
+
}
|
|
8335
|
+
for (const r of owners.values()) {
|
|
8336
|
+
if (r.ended?.reason.endsWith("; reap failed")) {
|
|
8337
|
+
const reason = r.ended.reason.slice(0, -"; reap failed".length);
|
|
8338
|
+
r.ended = void 0;
|
|
8339
|
+
void end(r, reason);
|
|
8340
|
+
} else if (r.ended && r.ended.retry !== void 0 && !r.ended.left && !running.has(r)) void reapEnded(r, r.ended.reason);
|
|
8341
|
+
}
|
|
8342
|
+
if (changed) save();
|
|
8343
|
+
}
|
|
8344
|
+
return {
|
|
8345
|
+
async start() {
|
|
8346
|
+
load();
|
|
8347
|
+
const table = await read();
|
|
8348
|
+
for (const r of [...owners.values()]) {
|
|
8349
|
+
if (r.ended) {
|
|
8350
|
+
if (!r.ended.left && r.ended.retry === void 0 && !r.ended.reason.endsWith("; reap failed")) void reapEnded(r, r.ended.reason);
|
|
8351
|
+
continue;
|
|
8352
|
+
}
|
|
8353
|
+
if (table && !alive2(table, r)) void end(r, "gone while the server was down");
|
|
8354
|
+
}
|
|
8355
|
+
d.log("procs-start", { owners: owners.size });
|
|
8356
|
+
const began = d.now();
|
|
8357
|
+
await takeTick();
|
|
8358
|
+
const cost = d.now() - began;
|
|
8359
|
+
if (cost > SLOW_AFTER_MS) tickEvery = SLOW_TICK_MS;
|
|
8360
|
+
d.log("procs-tick-cost", { ms: cost, owners: owners.size, procs: table?.length ?? 0, everyMs: tickEvery });
|
|
8361
|
+
},
|
|
8362
|
+
async hello(h) {
|
|
8363
|
+
if (!Number.isSafeInteger(h.pid) || h.pid <= 1) throw new ProcsRefusal("procs-hello needs the pid of the pi");
|
|
8364
|
+
const id = h.id ?? `p${h.pid}`;
|
|
8365
|
+
if (!ID2.test(id)) throw new ProcsRefusal("procs-hello: the owner id is 1 to 64 letters, digits, and . _ -");
|
|
8366
|
+
const table = await read();
|
|
8367
|
+
const started = table ? table.find((p) => p.pid === h.pid && !p.zombie)?.started : probe2(h.pid);
|
|
8368
|
+
if (!started || h.started !== void 0 && h.started !== started) throw new ProcsRefusal("procs-hello: that pid is not a process of this account with that start time");
|
|
8369
|
+
if (table) await refuseProtected(h.pid, table, h.pane);
|
|
8370
|
+
const known2 = owners.get(id);
|
|
8371
|
+
if (known2 && !known2.ended && (known2.pid !== h.pid || known2.started !== started)) {
|
|
8372
|
+
if (table ? alive2(table, known2) : probe2(known2.pid) === known2.started) throw new ProcsRefusal(`procs-hello: the owner id ${id} is in use by pid ${known2.pid}; an id needs the pid or the session in it`);
|
|
8373
|
+
void end(known2, "replaced by a new hello");
|
|
8374
|
+
}
|
|
8375
|
+
const cwd = typeof h.cwd === "string" && h.cwd.length <= 4096 ? h.cwd : void 0;
|
|
8376
|
+
const text4 = (v, max = 200) => typeof v === "string" && v.length > 0 && v.length <= max ? v : void 0;
|
|
8377
|
+
const keep = known2 && !known2.ended && known2.pid === h.pid && known2.started === started ? known2 : void 0;
|
|
8378
|
+
const record = {
|
|
8379
|
+
id,
|
|
8380
|
+
pid: h.pid,
|
|
8381
|
+
started,
|
|
8382
|
+
roots: rootsOf(cwd),
|
|
8383
|
+
snapshot: keep?.snapshot ?? [],
|
|
8384
|
+
helloAt: keep?.helloAt ?? d.now(),
|
|
8385
|
+
seenAt: d.now(),
|
|
8386
|
+
...text4(h.subagentId) ? { subagentId: text4(h.subagentId) } : {},
|
|
8387
|
+
...h.role === "train" ? { role: "train" } : {},
|
|
8388
|
+
...text4(h.pane) ? { pane: text4(h.pane) } : {},
|
|
8389
|
+
...text4(h.terminal) ? { terminal: text4(h.terminal) } : {},
|
|
8390
|
+
...cwd ? { cwd } : {}
|
|
8391
|
+
};
|
|
8392
|
+
owners.set(id, record);
|
|
8393
|
+
save();
|
|
8394
|
+
d.log("procs-hello", { owner: id, pid: h.pid, worktree: record.roots.length > 0 });
|
|
8395
|
+
return { id };
|
|
8396
|
+
},
|
|
8397
|
+
beat(id) {
|
|
8398
|
+
const r = owners.get(id);
|
|
8399
|
+
if (!r || r.ended) return false;
|
|
8400
|
+
r.seenAt = d.now();
|
|
8401
|
+
return true;
|
|
8402
|
+
},
|
|
8403
|
+
bye(id) {
|
|
8404
|
+
const r = owners.get(id);
|
|
8405
|
+
if (!r || r.ended) return false;
|
|
8406
|
+
void end(r, "bye");
|
|
8407
|
+
return true;
|
|
8408
|
+
},
|
|
8409
|
+
async tick() {
|
|
8410
|
+
if (ticking) return ticking;
|
|
8411
|
+
ticking = takeTick().finally(() => void (ticking = void 0));
|
|
8412
|
+
return ticking;
|
|
8413
|
+
},
|
|
8414
|
+
async paneEvent(raw) {
|
|
8415
|
+
const event2 = { ...raw, event: raw.event.replace("_", ".") };
|
|
8416
|
+
if (event2.event !== "pane.exited" && event2.event !== "pane.closed") return;
|
|
8417
|
+
const pane = typeof event2.data.pane_id === "string" ? event2.data.pane_id : void 0, terminal = typeof event2.data.terminal_id === "string" ? event2.data.terminal_id : void 0;
|
|
8418
|
+
if (!pane) return;
|
|
8419
|
+
let theirs = false;
|
|
8420
|
+
for (const r of [...owners.values()]) {
|
|
8421
|
+
if (r.pane !== pane) continue;
|
|
8422
|
+
if (terminal !== void 0 && r.terminal !== void 0 && terminal !== r.terminal) continue;
|
|
8423
|
+
if (terminal === void 0 && r.terminal !== void 0) {
|
|
8424
|
+
const now = await d.panes?.().catch(() => void 0);
|
|
8425
|
+
const same = now?.find((p) => p.pane_id === pane);
|
|
8426
|
+
if (!now || same && (same.terminal_id === void 0 || same.terminal_id === r.terminal)) continue;
|
|
8427
|
+
}
|
|
8428
|
+
theirs = true;
|
|
8429
|
+
if (!r.ended) void end(r, event2.event);
|
|
8430
|
+
}
|
|
8431
|
+
const ws2 = typeof event2.data.workspace_id === "string" ? event2.data.workspace_id : typeof event2.data.pane?.workspace_id === "string" ? event2.data.pane.workspace_id : void 0;
|
|
8432
|
+
if (!theirs) await reapWorkspace(ws2, pane, event2.event);
|
|
8433
|
+
},
|
|
8434
|
+
list() {
|
|
8435
|
+
return [...owners.values()].map((r) => ({ id: r.id, pid: r.pid, started: r.started, live: !r.ended, roots: r.roots, snapshot: r.snapshot.length, ...r.pane ? { pane: r.pane } : {}, ...r.subagentId ? { subagentId: r.subagentId } : {}, ...r.role ? { role: r.role } : {}, ...r.ended ? { endedReason: r.ended.reason } : {} }));
|
|
8436
|
+
},
|
|
8437
|
+
async reapOwner(id) {
|
|
8438
|
+
const r = owners.get(id);
|
|
8439
|
+
if (!r) throw new ProcsRefusal(`no owner ${id}`);
|
|
8440
|
+
if (!r.ended) {
|
|
8441
|
+
const table = await read();
|
|
8442
|
+
if (table && alive2(table, r)) throw new ProcsRefusal(`owner ${id} (pid ${r.pid}) is alive; reap works on an owner that has ended`);
|
|
8443
|
+
}
|
|
8444
|
+
const result2 = await reapSet(r, "reap");
|
|
8445
|
+
if (!result2.left.length && !result2.deferred && owners.get(id) === r) owners.delete(id), save();
|
|
8446
|
+
return result2;
|
|
8447
|
+
},
|
|
8448
|
+
async reapCheckout(path, bounded) {
|
|
8449
|
+
const physicalPath = real3(path), table = await read();
|
|
8450
|
+
const holders2 = (where) => [...owners.values()].filter((r) => !r.ended && table && alive2(table, r) && r.roots.some((root2) => overlap(where, root2)));
|
|
8451
|
+
const spare = [];
|
|
8452
|
+
let root;
|
|
8453
|
+
if (bounded) {
|
|
8454
|
+
const c = exists(physicalPath) ? checkout(physicalPath) : void 0;
|
|
8455
|
+
if (c?.kind !== "linked" || c.root !== physicalPath) throw new ProcsRefusal(`${path} is not the root of a linked worktree: a pane close reaps a checkout by its root only`);
|
|
8456
|
+
const at2 = (pid) => table?.find((p) => p.pid === pid && !p.zombie);
|
|
8457
|
+
if (!table || at2(bounded.caller.pid)?.started !== bounded.caller.started) throw new ProcsRefusal(`the caller (pid ${bounded.caller.pid}) is not in the process table with the start time it gave; nothing was signalled`);
|
|
8458
|
+
for (let p = at2(bounded.caller.pid); p && p.pid > 1 && !spare.includes(p.pid); p = at2(p.ppid)) spare.push(p.pid);
|
|
8459
|
+
root = c.root;
|
|
8460
|
+
} else if (exists(physicalPath)) {
|
|
8461
|
+
const c = checkout(physicalPath);
|
|
8462
|
+
if (c.kind !== "linked") throw new ProcsRefusal(`${path} is ${c.kind === "none" ? "in no checkout" : `in a ${c.kind} checkout`}: reap --checkout takes a linked worktree (or a removed one a worker was known to work in)`);
|
|
8463
|
+
root = c.root;
|
|
8464
|
+
} else {
|
|
8465
|
+
root = [...owners.values()].flatMap((r) => r.roots).filter((known3) => under(physicalPath, known3)).sort((a, b) => b.length - a.length)[0];
|
|
8466
|
+
if (!root) throw new ProcsRefusal(`${path} is gone and no known worker worked in it: reap --checkout takes a linked worktree (or a removed one a worker was known to work in)`);
|
|
8467
|
+
}
|
|
8468
|
+
const live = [...holders2(physicalPath), ...holders2(root)];
|
|
8469
|
+
if (live.length) throw new ProcsRefusal(`a live owner works in ${path}: ${[...new Set(live)].map((h) => `${h.id} (pid ${h.pid})`).join(", ")}`);
|
|
8470
|
+
const known2 = bounded ? void 0 : [...owners.values()].find((r) => r.roots.some((r2) => r2 === root));
|
|
8471
|
+
const subject = known2 ?? { id: "(checkout)", pid: -1, started: "", roots: [root], snapshot: [], helloAt: d.now(), seenAt: d.now() };
|
|
8472
|
+
const result2 = await reapSet(subject, bounded ? "pane-close" : "reap", true, bounded ? { spare } : void 0);
|
|
8473
|
+
if (known2 && !result2.left.length && owners.get(known2.id) === known2) owners.delete(known2.id), save();
|
|
8474
|
+
return result2;
|
|
8475
|
+
},
|
|
8476
|
+
tickMs: () => tickEvery,
|
|
8477
|
+
settled: async () => void await Promise.allSettled([...pending2])
|
|
8478
|
+
};
|
|
8479
|
+
}
|
|
8480
|
+
function serverPids(stateDir) {
|
|
8481
|
+
const files = [join29(stateDir, "server.json")];
|
|
8482
|
+
try {
|
|
8483
|
+
for (const hash3 of readdirSync16(join29(stateDir, "servers"))) files.push(join29(stateDir, "servers", hash3, "server.json"));
|
|
8484
|
+
} catch {
|
|
8485
|
+
}
|
|
8486
|
+
const found = [];
|
|
8487
|
+
for (const f of files) {
|
|
8488
|
+
try {
|
|
8489
|
+
const { pid, started } = JSON.parse(readFileSync27(f, "utf8"));
|
|
8490
|
+
if (typeof pid === "number" && Number.isSafeInteger(pid) && pid > 1) found.push({ pid, ...typeof started === "string" && started ? { started } : {} });
|
|
8491
|
+
} catch {
|
|
8492
|
+
}
|
|
8493
|
+
}
|
|
8494
|
+
return found;
|
|
8495
|
+
}
|
|
8496
|
+
async function protectedSet(table, { run: run4, stateDir, agentDir, self }) {
|
|
8497
|
+
const out = [{ pid: self, why: "sb-server", tree: true }];
|
|
8498
|
+
for (const p of table) if (baseName(p.comm) === "herdr") out.push({ pid: p.pid, started: p.started, why: "herdr" });
|
|
8499
|
+
for (const server of serverPids(stateDir)) out.push({ ...server, why: "sb-server", tree: true });
|
|
8500
|
+
try {
|
|
8501
|
+
const pid = Number(readFileSync27(join29(agentDir, "intercom", "broker.pid"), "utf8").trim());
|
|
8502
|
+
if (Number.isSafeInteger(pid) && pid > 1) out.push({ pid, why: "intercom-broker", tree: true });
|
|
8503
|
+
} catch {
|
|
8504
|
+
}
|
|
8505
|
+
if (run4) {
|
|
8506
|
+
try {
|
|
8507
|
+
const listed2 = JSON.parse(await run4(["pane", "list"])).result?.panes ?? [];
|
|
8508
|
+
for (const pane of listed2) {
|
|
8509
|
+
try {
|
|
8510
|
+
const info = JSON.parse(await run4(["pane", "process-info", "--pane", pane.pane_id])).result?.process_info;
|
|
8511
|
+
if (typeof info?.shell_pid === "number" && info.shell_pid > 1) out.push({ pid: info.shell_pid, why: "pane" });
|
|
8512
|
+
for (const f of info?.foreground_processes ?? []) if (typeof f.pid === "number" && f.pid > 1) out.push({ pid: f.pid, why: "pane-foreground", pane: pane.pane_id });
|
|
8513
|
+
} catch {
|
|
8514
|
+
}
|
|
8515
|
+
}
|
|
8516
|
+
} catch {
|
|
8517
|
+
}
|
|
8518
|
+
}
|
|
8519
|
+
return out;
|
|
8520
|
+
}
|
|
8521
|
+
|
|
8522
|
+
// src/deadpane.ts
|
|
8523
|
+
import { statSync as statSync15 } from "node:fs";
|
|
8524
|
+
var LOCK_MS = 5 * 6e4;
|
|
8525
|
+
var marked = (p) => Object.keys(p.tokens ?? {}).some((k) => k.startsWith("sb_") || k.startsWith("piha_")) || Boolean(p.agent_session?.value);
|
|
8526
|
+
function refusal(f) {
|
|
8527
|
+
const p = f.pane, id = f.query;
|
|
8528
|
+
if (!p) return `No pane ${id} in Herdr's list`;
|
|
8529
|
+
if (p.workspace_id !== f.workspace) return `${id} is not in this workspace; a tower closes only its own workspace's panes`;
|
|
8530
|
+
const role = p.tokens?.sb_role;
|
|
8531
|
+
if (p.pane_id === f.tower.pane || f.tower.tab !== void 0 && p.tab_id === f.tower.tab || p.pane_id === f.tower.session || role === "tower" || role === "head") return `${id} is a tower's pane; a tower never closes one`;
|
|
8532
|
+
if (!marked(p)) return `${id} carries no mark of an agent (no Straw Boss or pi token): it is no agent pane, so the boss closes it`;
|
|
8533
|
+
if (p.tokens?.sb_open_main !== void 0 || p.tokens?.[REVIVE_TOKEN] !== void 0) return `${id} is being opened right now (an open, a revive, or a handover is starting its pi)`;
|
|
8534
|
+
if (f.reviving) return `${id} is being revived right now (a tower holds the revive lock of its session)`;
|
|
8535
|
+
if (f.listed) return `${id} is not dead: a live session (${f.listed.name ?? f.listed.id}) is at it`;
|
|
8536
|
+
if (p.agent) return `${id} runs ${p.agent}, so it is not dead`;
|
|
8537
|
+
return void 0;
|
|
8538
|
+
}
|
|
8539
|
+
function notBare(id, info) {
|
|
8540
|
+
const fg = info?.foreground_processes ?? [];
|
|
8541
|
+
if (typeof info?.shell_pid !== "number" || !fg.length) return `Herdr cannot show what runs in ${id}, so it is not proven dead`;
|
|
8542
|
+
if (fg.length === 1 && fg[0].pid === info.shell_pid) return void 0;
|
|
8543
|
+
const names = [...new Set(fg.filter((x) => x.pid !== info.shell_pid).map((x) => x.name ?? x.argv0 ?? `pid ${x.pid}`))].slice(0, 4).join(", ");
|
|
8544
|
+
return `${id} is not at a bare shell: ${names} runs in it`;
|
|
8545
|
+
}
|
|
8546
|
+
var revivingAt = (stateDir, now, p) => readRecords(stateDir).some(({ record }) => {
|
|
8547
|
+
if (record.pane !== p.pane_id || record.workspace !== p.workspace_id) return false;
|
|
8548
|
+
try {
|
|
8549
|
+
return now - statSync15(`${ledgerPath(stateDir, record.file)}.lock`).mtimeMs < LOCK_MS;
|
|
8550
|
+
} catch {
|
|
8551
|
+
return false;
|
|
8552
|
+
}
|
|
8553
|
+
});
|
|
8554
|
+
var paneWhat = (p) => {
|
|
8555
|
+
const t = p.tokens ?? {}, kind = t.sb_role ?? (t.sb_train ? "train" : Object.keys(t).some((k) => k.startsWith("piha_")) ? "subagent" : "pi"), name = t.sb_main_tab ?? t.sb_name ?? t.sb_tab ?? t.sb_member_tab;
|
|
8556
|
+
return name ? `${kind} ${name}` : kind;
|
|
8557
|
+
};
|
|
8558
|
+
var deadList = (rows) => ({
|
|
8559
|
+
text: rows.length ? `Dead agent panes of this workspace (close one with boss_close_main pane):
|
|
8560
|
+
${rows.map((r) => `- ${r.pane} (tab ${r.tab}${r.alone ? ", its only pane" : ""}): ${r.what}`).join("\n")}` : "No dead agent pane in this workspace.",
|
|
8561
|
+
details: rows
|
|
8562
|
+
});
|
|
8563
|
+
var WINDOWS_REFUSAL = "Dead agent panes are listed and closed on macOS and Linux only: on Windows Herdr's process-info names the shell alone even while a program runs under it, so no pane is proven dead; the boss closes it";
|
|
8564
|
+
function createDeadPanes(env) {
|
|
8565
|
+
const { run: run4 } = env;
|
|
8566
|
+
const platform = env.platform ?? process.platform;
|
|
8567
|
+
const onWindows = () => {
|
|
8568
|
+
if (platform === "win32") throw new Error(WINDOWS_REFUSAL);
|
|
8569
|
+
};
|
|
8570
|
+
const reviving = (p) => revivingAt(env.stateDir, env.now(), p);
|
|
8571
|
+
const look2 = async () => {
|
|
8572
|
+
const [all, sessions, ws2] = await Promise.all([panes(run4), env.sessions(), workspaces(run4)]);
|
|
8573
|
+
const here = env.here(), towerId2 = ws2.find((w) => w.workspace_id === here.workspace)?.tokens?.tower;
|
|
8574
|
+
const own2 = all.find((p) => p.pane_id === here.pane);
|
|
8575
|
+
const tower = { pane: here.pane, ...own2 ? { tab: own2.tab_id } : {}, .../* @__PURE__ */ ((s) => s ? { session: s } : {})(sessions.find((x) => x.id === towerId2)?.herdrLocation?.paneId) };
|
|
8576
|
+
const facts = (query) => {
|
|
8577
|
+
const pane = all.find((p) => p.pane_id === query);
|
|
8578
|
+
return { query, workspace: here.workspace, tower, reviving: pane ? reviving(pane) : false, ...pane ? { pane } : {}, .../* @__PURE__ */ ((x) => x ? { listed: x } : {})(sessions.find((x) => x.herdrLocation?.paneId === query)) };
|
|
8579
|
+
};
|
|
8580
|
+
return { all, facts, workspace: here.workspace };
|
|
8581
|
+
};
|
|
8582
|
+
const info = async (pane) => JSON.parse(await run4(["pane", "process-info", "--pane", pane])).result?.process_info;
|
|
8583
|
+
const why = async (f) => refusal(f) ?? notBare(f.query, await info(f.query).catch(() => void 0));
|
|
8584
|
+
return {
|
|
8585
|
+
/** The dead agent panes of this workspace: each passed every row of the table. */
|
|
8586
|
+
async list() {
|
|
8587
|
+
onWindows();
|
|
8588
|
+
const { all, facts, workspace } = await look2();
|
|
8589
|
+
const rows = [];
|
|
8590
|
+
for (const p of all.filter((x) => x.workspace_id === workspace && marked(x))) {
|
|
8591
|
+
if (await why(facts(p.pane_id))) continue;
|
|
8592
|
+
rows.push({ pane: p.pane_id, tab: p.tab_id, what: paneWhat(p), alone: all.filter((x) => x.tab_id === p.tab_id).length === 1 });
|
|
8593
|
+
}
|
|
8594
|
+
return rows;
|
|
8595
|
+
},
|
|
8596
|
+
/** Closes one dead agent pane, its tab with it when it is the tab's only pane; refused with the table's reason otherwise. */
|
|
8597
|
+
async close(query) {
|
|
8598
|
+
onWindows();
|
|
8599
|
+
const { all, facts } = await look2();
|
|
8600
|
+
const f = facts(query), refused3 = await why(f);
|
|
8601
|
+
if (refused3) throw new Error(refused3);
|
|
8602
|
+
const p = f.pane, alone = all.filter((x) => x.tab_id === p.tab_id).length === 1;
|
|
8603
|
+
if (alone) {
|
|
8604
|
+
await run4(["tab", "close", p.tab_id]);
|
|
8605
|
+
return { text: `Closed tab ${p.tab_id} with its only pane ${p.pane_id} (${paneWhat(p)}), whose pi was gone.`, pane: p.pane_id, tab: p.tab_id };
|
|
8606
|
+
}
|
|
8607
|
+
await run4(["pane", "close", p.pane_id]);
|
|
8608
|
+
return { text: `Closed pane ${p.pane_id} (${paneWhat(p)}), whose pi was gone; its tab ${p.tab_id} keeps its other panes.`, pane: p.pane_id };
|
|
8609
|
+
}
|
|
8610
|
+
};
|
|
8611
|
+
}
|
|
8612
|
+
|
|
7963
8613
|
// src/memorycmd.ts
|
|
7964
8614
|
var WORDS = {
|
|
7965
8615
|
en: {
|
|
@@ -8319,13 +8969,11 @@ export {
|
|
|
8319
8969
|
refOf,
|
|
8320
8970
|
sameProc,
|
|
8321
8971
|
baseName,
|
|
8322
|
-
|
|
8972
|
+
programOf,
|
|
8323
8973
|
readTable,
|
|
8324
|
-
|
|
8974
|
+
named,
|
|
8325
8975
|
descendants,
|
|
8326
8976
|
readCwds,
|
|
8327
|
-
classifyCwd,
|
|
8328
|
-
inodeOnDisk,
|
|
8329
8977
|
reap,
|
|
8330
8978
|
holding,
|
|
8331
8979
|
stepWaits,
|
|
@@ -8410,7 +9058,6 @@ export {
|
|
|
8410
9058
|
ReviveRefused,
|
|
8411
9059
|
sessionsDir,
|
|
8412
9060
|
keyOf3 as keyOf,
|
|
8413
|
-
ledgerPath,
|
|
8414
9061
|
sessionHeader,
|
|
8415
9062
|
idOfName,
|
|
8416
9063
|
fileRefusal,
|
|
@@ -8432,6 +9079,18 @@ export {
|
|
|
8432
9079
|
reviveHandoff,
|
|
8433
9080
|
pruneState,
|
|
8434
9081
|
startPrune,
|
|
9082
|
+
physical,
|
|
9083
|
+
checkoutOf,
|
|
9084
|
+
under,
|
|
9085
|
+
killSet,
|
|
9086
|
+
ProcsRefusal,
|
|
9087
|
+
createProcs,
|
|
9088
|
+
protectedSet,
|
|
9089
|
+
refusal,
|
|
9090
|
+
notBare,
|
|
9091
|
+
revivingAt,
|
|
9092
|
+
deadList,
|
|
9093
|
+
createDeadPanes,
|
|
8435
9094
|
memoryWords,
|
|
8436
9095
|
tidy
|
|
8437
9096
|
};
|