@fro.bot/space-bus 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/attach.js +1 -1
- package/dist/cli.d.ts +25 -0
- package/dist/cli.js +206 -27
- package/dist/config.js +20 -4
- package/dist/contract.js +4 -1
- package/dist/core.js +4 -1
- package/dist/discovery.d.ts +14 -0
- package/dist/format.js +3 -0
- package/dist/index.js +155 -12
- package/dist/mcp.js +156 -13
- package/dist/server.d.ts +50 -9
- package/dist/server.js +161 -13
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
3
|
+
|
|
1
4
|
// src/contract.ts
|
|
2
5
|
import { z } from "zod";
|
|
3
6
|
var sessionSchema = z.looseObject({
|
|
@@ -103,7 +106,7 @@ var managedSpawnConfigSchema = z.object({
|
|
|
103
106
|
});
|
|
104
107
|
var discoveryFileSchema = z.object({
|
|
105
108
|
port: z.number().int().nonnegative(),
|
|
106
|
-
pid: z.number().int().
|
|
109
|
+
pid: z.number().int().min(2),
|
|
107
110
|
identity: z.string(),
|
|
108
111
|
password: z.string(),
|
|
109
112
|
spawnConfig: managedSpawnConfigSchema,
|
|
@@ -773,14 +776,27 @@ function removeDiscovery(rosterPath) {
|
|
|
773
776
|
throw err2;
|
|
774
777
|
}
|
|
775
778
|
}
|
|
779
|
+
function removeDiscoveryIfMatches(rosterPath, expected) {
|
|
780
|
+
try {
|
|
781
|
+
const current = readDiscovery(rosterPath);
|
|
782
|
+
if (current && current.pid === expected.pid && current.identity === expected.identity) {
|
|
783
|
+
removeDiscovery(rosterPath);
|
|
784
|
+
}
|
|
785
|
+
} catch {}
|
|
786
|
+
}
|
|
776
787
|
function attachLive(rosterPath) {
|
|
777
788
|
const discovery = readDiscovery(rosterPath);
|
|
778
789
|
if (!discovery)
|
|
779
790
|
return null;
|
|
780
791
|
if (!loopbackOk(discovery.baseUrl))
|
|
781
792
|
return null;
|
|
782
|
-
if (!verifyIdentity(discovery.pid, discovery.identity))
|
|
793
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
794
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
795
|
+
pid: discovery.pid,
|
|
796
|
+
identity: discovery.identity
|
|
797
|
+
});
|
|
783
798
|
return null;
|
|
799
|
+
}
|
|
784
800
|
return {
|
|
785
801
|
baseUrl: discovery.baseUrl,
|
|
786
802
|
credentials: { username: "opencode", password: discovery.password },
|
|
@@ -815,7 +831,7 @@ function verifyIdentity(pid, storedIdentity) {
|
|
|
815
831
|
return current !== null && current === storedIdentity;
|
|
816
832
|
}
|
|
817
833
|
var lockFileSchema = z2.object({
|
|
818
|
-
pid: z2.number().int().
|
|
834
|
+
pid: z2.number().int().min(2),
|
|
819
835
|
startTime: z2.string(),
|
|
820
836
|
since: z2.number()
|
|
821
837
|
});
|
|
@@ -911,7 +927,7 @@ function provisionalFilePath(rosterPath) {
|
|
|
911
927
|
return join(stateDirFor(rosterPath), "spawn.provisional.json");
|
|
912
928
|
}
|
|
913
929
|
var provisionalFileSchema = z2.object({
|
|
914
|
-
pid: z2.number().int().
|
|
930
|
+
pid: z2.number().int().min(2),
|
|
915
931
|
identity: z2.string(),
|
|
916
932
|
password: z2.string(),
|
|
917
933
|
since: z2.number()
|
|
@@ -1080,6 +1096,9 @@ var POLL_INTERVAL_MS = 50;
|
|
|
1080
1096
|
var MIN_PROBE_PHASE_MS = 3000;
|
|
1081
1097
|
var PROBE_FETCH_TIMEOUT_MS = 2000;
|
|
1082
1098
|
var STOP_GRACE_MS = 2000;
|
|
1099
|
+
var SUPERVISE_INTERVAL_MS = 5000;
|
|
1100
|
+
var SUPERVISE_FAILURE_THRESHOLD = 3;
|
|
1101
|
+
var SUPERVISE_UNHEALTHY_BUDGET_MS = 60000;
|
|
1083
1102
|
function sleep(ms) {
|
|
1084
1103
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1085
1104
|
}
|
|
@@ -1107,11 +1126,34 @@ function redactedReadinessError(rosterPath, password, reason) {
|
|
|
1107
1126
|
return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
|
|
1108
1127
|
${tail}`);
|
|
1109
1128
|
}
|
|
1129
|
+
function isEpermError(err2) {
|
|
1130
|
+
return typeof err2 === "object" && err2 !== null && "code" in err2 && err2.code === "EPERM";
|
|
1131
|
+
}
|
|
1132
|
+
function signalGroup(pid, sig) {
|
|
1133
|
+
if (pid <= 1) {
|
|
1134
|
+
try {
|
|
1135
|
+
process.kill(pid, sig);
|
|
1136
|
+
} catch {}
|
|
1137
|
+
return false;
|
|
1138
|
+
}
|
|
1139
|
+
try {
|
|
1140
|
+
process.kill(-pid, sig);
|
|
1141
|
+
return true;
|
|
1142
|
+
} catch (err2) {
|
|
1143
|
+
if (err2.code === "EPERM") {
|
|
1144
|
+
throw err2;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
try {
|
|
1148
|
+
process.kill(pid, sig);
|
|
1149
|
+
} catch {}
|
|
1150
|
+
return false;
|
|
1151
|
+
}
|
|
1110
1152
|
function killIdentifiedProcess(pid, identity) {
|
|
1111
1153
|
try {
|
|
1112
1154
|
if (identity !== null && identity !== undefined && identity !== "") {
|
|
1113
1155
|
if (verifyIdentity(pid, identity))
|
|
1114
|
-
|
|
1156
|
+
signalGroup(pid, "SIGTERM");
|
|
1115
1157
|
return;
|
|
1116
1158
|
}
|
|
1117
1159
|
process.kill(pid, "SIGTERM");
|
|
@@ -1324,7 +1366,14 @@ async function ensureServer(rosterPath, options = {}) {
|
|
|
1324
1366
|
}
|
|
1325
1367
|
function serverStatus(rosterPath) {
|
|
1326
1368
|
const discovery = readDiscovery(rosterPath);
|
|
1327
|
-
if (!discovery
|
|
1369
|
+
if (!discovery) {
|
|
1370
|
+
return { running: false };
|
|
1371
|
+
}
|
|
1372
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
1373
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
1374
|
+
pid: discovery.pid,
|
|
1375
|
+
identity: discovery.identity
|
|
1376
|
+
});
|
|
1328
1377
|
return { running: false };
|
|
1329
1378
|
}
|
|
1330
1379
|
let configDrift;
|
|
@@ -1360,28 +1409,105 @@ async function stopServer(rosterPath) {
|
|
|
1360
1409
|
return { stopped: false };
|
|
1361
1410
|
}
|
|
1362
1411
|
const { pid } = discovery;
|
|
1412
|
+
let groupSignaled;
|
|
1413
|
+
const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
|
|
1363
1414
|
try {
|
|
1364
|
-
|
|
1365
|
-
} catch {
|
|
1415
|
+
groupSignaled = signalGroup(pid, "SIGTERM");
|
|
1416
|
+
} catch (err2) {
|
|
1417
|
+
if (isEpermError(err2)) {
|
|
1418
|
+
return { stopped: false };
|
|
1419
|
+
}
|
|
1366
1420
|
removeDiscovery(rosterPath);
|
|
1367
1421
|
return { stopped: true };
|
|
1368
1422
|
}
|
|
1369
|
-
if (await
|
|
1423
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
1370
1424
|
removeDiscovery(rosterPath);
|
|
1371
1425
|
return { stopped: true };
|
|
1372
1426
|
}
|
|
1373
1427
|
try {
|
|
1374
|
-
|
|
1375
|
-
} catch {
|
|
1428
|
+
groupSignaled = signalGroup(pid, "SIGKILL");
|
|
1429
|
+
} catch (err2) {
|
|
1430
|
+
if (isEpermError(err2)) {
|
|
1431
|
+
return { stopped: false };
|
|
1432
|
+
}
|
|
1376
1433
|
removeDiscovery(rosterPath);
|
|
1377
1434
|
return { stopped: true };
|
|
1378
1435
|
}
|
|
1379
|
-
if (await
|
|
1436
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
1380
1437
|
removeDiscovery(rosterPath);
|
|
1381
1438
|
return { stopped: true };
|
|
1382
1439
|
}
|
|
1383
1440
|
return { stopped: false };
|
|
1384
1441
|
}
|
|
1442
|
+
async function stopForHung(tick) {
|
|
1443
|
+
try {
|
|
1444
|
+
await tick.stop(tick.rosterPath);
|
|
1445
|
+
} catch {}
|
|
1446
|
+
return { kind: "outcome", outcome: { reason: "hung" } };
|
|
1447
|
+
}
|
|
1448
|
+
async function superviseTick(tick, state) {
|
|
1449
|
+
const st = tick.status(tick.rosterPath);
|
|
1450
|
+
if (!st.running) {
|
|
1451
|
+
return { kind: "outcome", outcome: { reason: "died" } };
|
|
1452
|
+
}
|
|
1453
|
+
const probeOutcome = await tick.probe(tick.handle.baseUrl, tick.password);
|
|
1454
|
+
const failures = probeOutcome === "retry" ? state.failures + 1 : 0;
|
|
1455
|
+
const lastReadyAt = probeOutcome === "ready" ? tick.now() : state.lastReadyAt;
|
|
1456
|
+
if (failures >= tick.threshold) {
|
|
1457
|
+
return stopForHung(tick);
|
|
1458
|
+
}
|
|
1459
|
+
if (tick.now() - lastReadyAt >= tick.unhealthyBudgetMs) {
|
|
1460
|
+
return stopForHung(tick);
|
|
1461
|
+
}
|
|
1462
|
+
return { kind: "tick", failures, lastReadyAt };
|
|
1463
|
+
}
|
|
1464
|
+
function makeSuperviseTick(rosterPath, handle, opts) {
|
|
1465
|
+
return {
|
|
1466
|
+
rosterPath,
|
|
1467
|
+
handle,
|
|
1468
|
+
password: handle.credentials.password ?? "",
|
|
1469
|
+
threshold: opts.threshold ?? SUPERVISE_FAILURE_THRESHOLD,
|
|
1470
|
+
unhealthyBudgetMs: opts.unhealthyBudgetMs ?? SUPERVISE_UNHEALTHY_BUDGET_MS,
|
|
1471
|
+
now: opts.now ?? Date.now,
|
|
1472
|
+
status: opts.status ?? serverStatus,
|
|
1473
|
+
probe: opts.probe ?? probe,
|
|
1474
|
+
stop: opts.stop ?? stopServer
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
async function interruptibleSleep(doSleep, intervalMs, interrupt) {
|
|
1478
|
+
if (!interrupt) {
|
|
1479
|
+
await doSleep(intervalMs);
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
let timer;
|
|
1483
|
+
const timed = new Promise((resolve) => {
|
|
1484
|
+
timer = setTimeout(resolve, intervalMs);
|
|
1485
|
+
});
|
|
1486
|
+
try {
|
|
1487
|
+
await Promise.race([timed, interrupt]);
|
|
1488
|
+
} finally {
|
|
1489
|
+
if (timer !== undefined)
|
|
1490
|
+
clearTimeout(timer);
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
async function superviseServer(rosterPath, handle, opts = {}) {
|
|
1494
|
+
const intervalMs = opts.intervalMs ?? SUPERVISE_INTERVAL_MS;
|
|
1495
|
+
const tick = makeSuperviseTick(rosterPath, handle, opts);
|
|
1496
|
+
const doSleep = opts.sleep ?? sleep;
|
|
1497
|
+
const shouldStop = opts.shouldStop ?? (() => false);
|
|
1498
|
+
let state = { failures: 0, lastReadyAt: tick.now() };
|
|
1499
|
+
for (;; ) {
|
|
1500
|
+
if (shouldStop())
|
|
1501
|
+
return { reason: "signal" };
|
|
1502
|
+
const result2 = await superviseTick(tick, state);
|
|
1503
|
+
if (result2.kind === "outcome")
|
|
1504
|
+
return result2.outcome;
|
|
1505
|
+
state = { failures: result2.failures, lastReadyAt: result2.lastReadyAt };
|
|
1506
|
+
if (shouldStop())
|
|
1507
|
+
return { reason: "signal" };
|
|
1508
|
+
await interruptibleSleep(doSleep, intervalMs, opts.interrupt);
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1385
1511
|
async function waitForDeath(pid, budgetMs) {
|
|
1386
1512
|
const deadline = Date.now() + budgetMs;
|
|
1387
1513
|
while (Date.now() < deadline) {
|
|
@@ -1391,6 +1517,23 @@ async function waitForDeath(pid, budgetMs) {
|
|
|
1391
1517
|
}
|
|
1392
1518
|
return !isAlive(pid);
|
|
1393
1519
|
}
|
|
1520
|
+
async function waitForGroupDeath(pid, budgetMs) {
|
|
1521
|
+
const deadline = Date.now() + budgetMs;
|
|
1522
|
+
const groupAlive = () => {
|
|
1523
|
+
try {
|
|
1524
|
+
process.kill(-pid, 0);
|
|
1525
|
+
return true;
|
|
1526
|
+
} catch {
|
|
1527
|
+
return false;
|
|
1528
|
+
}
|
|
1529
|
+
};
|
|
1530
|
+
while (Date.now() < deadline) {
|
|
1531
|
+
if (!groupAlive())
|
|
1532
|
+
return true;
|
|
1533
|
+
await sleep(POLL_INTERVAL_MS);
|
|
1534
|
+
}
|
|
1535
|
+
return !groupAlive();
|
|
1536
|
+
}
|
|
1394
1537
|
|
|
1395
1538
|
// src/tools/bus_result.ts
|
|
1396
1539
|
import { tool } from "@opencode-ai/plugin";
|
package/dist/mcp.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
|
+
|
|
2
5
|
// src/contract.ts
|
|
3
6
|
import { z } from "zod";
|
|
4
7
|
var sessionSchema = z.looseObject({
|
|
@@ -104,7 +107,7 @@ var managedSpawnConfigSchema = z.object({
|
|
|
104
107
|
});
|
|
105
108
|
var discoveryFileSchema = z.object({
|
|
106
109
|
port: z.number().int().nonnegative(),
|
|
107
|
-
pid: z.number().int().
|
|
110
|
+
pid: z.number().int().min(2),
|
|
108
111
|
identity: z.string(),
|
|
109
112
|
password: z.string(),
|
|
110
113
|
spawnConfig: managedSpawnConfigSchema,
|
|
@@ -774,14 +777,27 @@ function removeDiscovery(rosterPath) {
|
|
|
774
777
|
throw err2;
|
|
775
778
|
}
|
|
776
779
|
}
|
|
780
|
+
function removeDiscoveryIfMatches(rosterPath, expected) {
|
|
781
|
+
try {
|
|
782
|
+
const current = readDiscovery(rosterPath);
|
|
783
|
+
if (current && current.pid === expected.pid && current.identity === expected.identity) {
|
|
784
|
+
removeDiscovery(rosterPath);
|
|
785
|
+
}
|
|
786
|
+
} catch {}
|
|
787
|
+
}
|
|
777
788
|
function attachLive(rosterPath) {
|
|
778
789
|
const discovery = readDiscovery(rosterPath);
|
|
779
790
|
if (!discovery)
|
|
780
791
|
return null;
|
|
781
792
|
if (!loopbackOk(discovery.baseUrl))
|
|
782
793
|
return null;
|
|
783
|
-
if (!verifyIdentity(discovery.pid, discovery.identity))
|
|
794
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
795
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
796
|
+
pid: discovery.pid,
|
|
797
|
+
identity: discovery.identity
|
|
798
|
+
});
|
|
784
799
|
return null;
|
|
800
|
+
}
|
|
785
801
|
return {
|
|
786
802
|
baseUrl: discovery.baseUrl,
|
|
787
803
|
credentials: { username: "opencode", password: discovery.password },
|
|
@@ -816,7 +832,7 @@ function verifyIdentity(pid, storedIdentity) {
|
|
|
816
832
|
return current !== null && current === storedIdentity;
|
|
817
833
|
}
|
|
818
834
|
var lockFileSchema = z2.object({
|
|
819
|
-
pid: z2.number().int().
|
|
835
|
+
pid: z2.number().int().min(2),
|
|
820
836
|
startTime: z2.string(),
|
|
821
837
|
since: z2.number()
|
|
822
838
|
});
|
|
@@ -912,7 +928,7 @@ function provisionalFilePath(rosterPath) {
|
|
|
912
928
|
return join(stateDirFor(rosterPath), "spawn.provisional.json");
|
|
913
929
|
}
|
|
914
930
|
var provisionalFileSchema = z2.object({
|
|
915
|
-
pid: z2.number().int().
|
|
931
|
+
pid: z2.number().int().min(2),
|
|
916
932
|
identity: z2.string(),
|
|
917
933
|
password: z2.string(),
|
|
918
934
|
since: z2.number()
|
|
@@ -1081,6 +1097,9 @@ var POLL_INTERVAL_MS = 50;
|
|
|
1081
1097
|
var MIN_PROBE_PHASE_MS = 3000;
|
|
1082
1098
|
var PROBE_FETCH_TIMEOUT_MS = 2000;
|
|
1083
1099
|
var STOP_GRACE_MS = 2000;
|
|
1100
|
+
var SUPERVISE_INTERVAL_MS = 5000;
|
|
1101
|
+
var SUPERVISE_FAILURE_THRESHOLD = 3;
|
|
1102
|
+
var SUPERVISE_UNHEALTHY_BUDGET_MS = 60000;
|
|
1084
1103
|
function sleep(ms) {
|
|
1085
1104
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1086
1105
|
}
|
|
@@ -1108,11 +1127,34 @@ function redactedReadinessError(rosterPath, password, reason) {
|
|
|
1108
1127
|
return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
|
|
1109
1128
|
${tail}`);
|
|
1110
1129
|
}
|
|
1130
|
+
function isEpermError(err2) {
|
|
1131
|
+
return typeof err2 === "object" && err2 !== null && "code" in err2 && err2.code === "EPERM";
|
|
1132
|
+
}
|
|
1133
|
+
function signalGroup(pid, sig) {
|
|
1134
|
+
if (pid <= 1) {
|
|
1135
|
+
try {
|
|
1136
|
+
process.kill(pid, sig);
|
|
1137
|
+
} catch {}
|
|
1138
|
+
return false;
|
|
1139
|
+
}
|
|
1140
|
+
try {
|
|
1141
|
+
process.kill(-pid, sig);
|
|
1142
|
+
return true;
|
|
1143
|
+
} catch (err2) {
|
|
1144
|
+
if (err2.code === "EPERM") {
|
|
1145
|
+
throw err2;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
try {
|
|
1149
|
+
process.kill(pid, sig);
|
|
1150
|
+
} catch {}
|
|
1151
|
+
return false;
|
|
1152
|
+
}
|
|
1111
1153
|
function killIdentifiedProcess(pid, identity) {
|
|
1112
1154
|
try {
|
|
1113
1155
|
if (identity !== null && identity !== undefined && identity !== "") {
|
|
1114
1156
|
if (verifyIdentity(pid, identity))
|
|
1115
|
-
|
|
1157
|
+
signalGroup(pid, "SIGTERM");
|
|
1116
1158
|
return;
|
|
1117
1159
|
}
|
|
1118
1160
|
process.kill(pid, "SIGTERM");
|
|
@@ -1325,7 +1367,14 @@ async function ensureServer(rosterPath, options = {}) {
|
|
|
1325
1367
|
}
|
|
1326
1368
|
function serverStatus(rosterPath) {
|
|
1327
1369
|
const discovery = readDiscovery(rosterPath);
|
|
1328
|
-
if (!discovery
|
|
1370
|
+
if (!discovery) {
|
|
1371
|
+
return { running: false };
|
|
1372
|
+
}
|
|
1373
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
1374
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
1375
|
+
pid: discovery.pid,
|
|
1376
|
+
identity: discovery.identity
|
|
1377
|
+
});
|
|
1329
1378
|
return { running: false };
|
|
1330
1379
|
}
|
|
1331
1380
|
let configDrift;
|
|
@@ -1361,28 +1410,105 @@ async function stopServer(rosterPath) {
|
|
|
1361
1410
|
return { stopped: false };
|
|
1362
1411
|
}
|
|
1363
1412
|
const { pid } = discovery;
|
|
1413
|
+
let groupSignaled;
|
|
1414
|
+
const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
|
|
1364
1415
|
try {
|
|
1365
|
-
|
|
1366
|
-
} catch {
|
|
1416
|
+
groupSignaled = signalGroup(pid, "SIGTERM");
|
|
1417
|
+
} catch (err2) {
|
|
1418
|
+
if (isEpermError(err2)) {
|
|
1419
|
+
return { stopped: false };
|
|
1420
|
+
}
|
|
1367
1421
|
removeDiscovery(rosterPath);
|
|
1368
1422
|
return { stopped: true };
|
|
1369
1423
|
}
|
|
1370
|
-
if (await
|
|
1424
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
1371
1425
|
removeDiscovery(rosterPath);
|
|
1372
1426
|
return { stopped: true };
|
|
1373
1427
|
}
|
|
1374
1428
|
try {
|
|
1375
|
-
|
|
1376
|
-
} catch {
|
|
1429
|
+
groupSignaled = signalGroup(pid, "SIGKILL");
|
|
1430
|
+
} catch (err2) {
|
|
1431
|
+
if (isEpermError(err2)) {
|
|
1432
|
+
return { stopped: false };
|
|
1433
|
+
}
|
|
1377
1434
|
removeDiscovery(rosterPath);
|
|
1378
1435
|
return { stopped: true };
|
|
1379
1436
|
}
|
|
1380
|
-
if (await
|
|
1437
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
1381
1438
|
removeDiscovery(rosterPath);
|
|
1382
1439
|
return { stopped: true };
|
|
1383
1440
|
}
|
|
1384
1441
|
return { stopped: false };
|
|
1385
1442
|
}
|
|
1443
|
+
async function stopForHung(tick) {
|
|
1444
|
+
try {
|
|
1445
|
+
await tick.stop(tick.rosterPath);
|
|
1446
|
+
} catch {}
|
|
1447
|
+
return { kind: "outcome", outcome: { reason: "hung" } };
|
|
1448
|
+
}
|
|
1449
|
+
async function superviseTick(tick, state) {
|
|
1450
|
+
const st = tick.status(tick.rosterPath);
|
|
1451
|
+
if (!st.running) {
|
|
1452
|
+
return { kind: "outcome", outcome: { reason: "died" } };
|
|
1453
|
+
}
|
|
1454
|
+
const probeOutcome = await tick.probe(tick.handle.baseUrl, tick.password);
|
|
1455
|
+
const failures = probeOutcome === "retry" ? state.failures + 1 : 0;
|
|
1456
|
+
const lastReadyAt = probeOutcome === "ready" ? tick.now() : state.lastReadyAt;
|
|
1457
|
+
if (failures >= tick.threshold) {
|
|
1458
|
+
return stopForHung(tick);
|
|
1459
|
+
}
|
|
1460
|
+
if (tick.now() - lastReadyAt >= tick.unhealthyBudgetMs) {
|
|
1461
|
+
return stopForHung(tick);
|
|
1462
|
+
}
|
|
1463
|
+
return { kind: "tick", failures, lastReadyAt };
|
|
1464
|
+
}
|
|
1465
|
+
function makeSuperviseTick(rosterPath, handle, opts) {
|
|
1466
|
+
return {
|
|
1467
|
+
rosterPath,
|
|
1468
|
+
handle,
|
|
1469
|
+
password: handle.credentials.password ?? "",
|
|
1470
|
+
threshold: opts.threshold ?? SUPERVISE_FAILURE_THRESHOLD,
|
|
1471
|
+
unhealthyBudgetMs: opts.unhealthyBudgetMs ?? SUPERVISE_UNHEALTHY_BUDGET_MS,
|
|
1472
|
+
now: opts.now ?? Date.now,
|
|
1473
|
+
status: opts.status ?? serverStatus,
|
|
1474
|
+
probe: opts.probe ?? probe,
|
|
1475
|
+
stop: opts.stop ?? stopServer
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1478
|
+
async function interruptibleSleep(doSleep, intervalMs, interrupt) {
|
|
1479
|
+
if (!interrupt) {
|
|
1480
|
+
await doSleep(intervalMs);
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
let timer;
|
|
1484
|
+
const timed = new Promise((resolve) => {
|
|
1485
|
+
timer = setTimeout(resolve, intervalMs);
|
|
1486
|
+
});
|
|
1487
|
+
try {
|
|
1488
|
+
await Promise.race([timed, interrupt]);
|
|
1489
|
+
} finally {
|
|
1490
|
+
if (timer !== undefined)
|
|
1491
|
+
clearTimeout(timer);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
async function superviseServer(rosterPath, handle, opts = {}) {
|
|
1495
|
+
const intervalMs = opts.intervalMs ?? SUPERVISE_INTERVAL_MS;
|
|
1496
|
+
const tick = makeSuperviseTick(rosterPath, handle, opts);
|
|
1497
|
+
const doSleep = opts.sleep ?? sleep;
|
|
1498
|
+
const shouldStop = opts.shouldStop ?? (() => false);
|
|
1499
|
+
let state = { failures: 0, lastReadyAt: tick.now() };
|
|
1500
|
+
for (;; ) {
|
|
1501
|
+
if (shouldStop())
|
|
1502
|
+
return { reason: "signal" };
|
|
1503
|
+
const result2 = await superviseTick(tick, state);
|
|
1504
|
+
if (result2.kind === "outcome")
|
|
1505
|
+
return result2.outcome;
|
|
1506
|
+
state = { failures: result2.failures, lastReadyAt: result2.lastReadyAt };
|
|
1507
|
+
if (shouldStop())
|
|
1508
|
+
return { reason: "signal" };
|
|
1509
|
+
await interruptibleSleep(doSleep, intervalMs, opts.interrupt);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1386
1512
|
async function waitForDeath(pid, budgetMs) {
|
|
1387
1513
|
const deadline = Date.now() + budgetMs;
|
|
1388
1514
|
while (Date.now() < deadline) {
|
|
@@ -1392,6 +1518,23 @@ async function waitForDeath(pid, budgetMs) {
|
|
|
1392
1518
|
}
|
|
1393
1519
|
return !isAlive(pid);
|
|
1394
1520
|
}
|
|
1521
|
+
async function waitForGroupDeath(pid, budgetMs) {
|
|
1522
|
+
const deadline = Date.now() + budgetMs;
|
|
1523
|
+
const groupAlive = () => {
|
|
1524
|
+
try {
|
|
1525
|
+
process.kill(-pid, 0);
|
|
1526
|
+
return true;
|
|
1527
|
+
} catch {
|
|
1528
|
+
return false;
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
while (Date.now() < deadline) {
|
|
1532
|
+
if (!groupAlive())
|
|
1533
|
+
return true;
|
|
1534
|
+
await sleep(POLL_INTERVAL_MS);
|
|
1535
|
+
}
|
|
1536
|
+
return !groupAlive();
|
|
1537
|
+
}
|
|
1395
1538
|
|
|
1396
1539
|
// src/mcp.ts
|
|
1397
1540
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
@@ -1521,7 +1664,7 @@ async function mcpLoadContext() {
|
|
|
1521
1664
|
}
|
|
1522
1665
|
return loadContext();
|
|
1523
1666
|
}
|
|
1524
|
-
var version = "0.
|
|
1667
|
+
var version = "0.8.0";
|
|
1525
1668
|
var server = new McpServer({
|
|
1526
1669
|
name: "space-bus",
|
|
1527
1670
|
version
|
package/dist/server.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/** Interval between supervision liveness ticks. */
|
|
2
|
+
export declare const SUPERVISE_INTERVAL_MS = 5000;
|
|
3
|
+
/** Consecutive unreachable-probe failures (pid still alive) before the
|
|
4
|
+
* supervisor declares the daemon hung and kills it. */
|
|
5
|
+
export declare const SUPERVISE_FAILURE_THRESHOLD = 3;
|
|
6
|
+
/** Absolute-lifetime watchdog: how long the daemon may go without a
|
|
7
|
+
* genuinely-"ready" probe before it's declared hung, regardless of whether
|
|
8
|
+
* failures ever hit 3-in-a-row. Bounds two cases the consecutive-failure
|
|
9
|
+
* counter can't catch: a FLAPPING daemon (retry, ready, retry, ready, ...
|
|
10
|
+
* never 3 consecutive) and a persistent auth-failure (server answers but
|
|
11
|
+
* never actually serves, looping "silently" forever). Tunable; 60s gives
|
|
12
|
+
* ~12 ticks at the default 5s interval to recover before being killed. */
|
|
13
|
+
export declare const SUPERVISE_UNHEALTHY_BUDGET_MS = 60000;
|
|
1
14
|
export interface EnsureServerOptions {
|
|
2
15
|
/** Bounds the readiness probe loop after spawning. Default 15s. */
|
|
3
16
|
readinessBudgetMs?: number;
|
|
@@ -37,6 +50,8 @@ export declare function redactSensitive(tail: string, password: string): string;
|
|
|
37
50
|
* config->server->config cycle since server.ts imports config.ts already).
|
|
38
51
|
*/
|
|
39
52
|
export declare function attachServer(rosterPath: string): ServerHandle | null;
|
|
53
|
+
export type ProbeOutcome = "ready" | "auth-failure" | "retry";
|
|
54
|
+
export declare function probe(baseUrl: string, password: string): Promise<ProbeOutcome>;
|
|
40
55
|
/**
|
|
41
56
|
* Ensures a managed server is running for the roster and returns a handle
|
|
42
57
|
* to it. Fast-paths to an existing live discovery; otherwise races for the
|
|
@@ -51,15 +66,41 @@ export declare function ensureServer(rosterPath: string, options?: EnsureServerO
|
|
|
51
66
|
* recorded at spawn time (R5b) — surfaced, not auto-corrected.
|
|
52
67
|
*/
|
|
53
68
|
export declare function serverStatus(rosterPath: string): ServerStatus;
|
|
54
|
-
/**
|
|
55
|
-
* Stops a managed server: identity-verified SIGTERM, then polls for actual
|
|
56
|
-
* death (bounded by STOP_GRACE_MS) before escalating to SIGKILL and polling
|
|
57
|
-
* again. The discovery file is removed ONLY once the process is confirmed
|
|
58
|
-
* dead (or was never verified-live/already gone) — a process that ignores
|
|
59
|
-
* both signals is reported as not stopped and the discovery file is left in
|
|
60
|
-
* place, so credentials aren't silently discarded while the server still
|
|
61
|
-
* holds the port. OS-authz only (R7b) — no additional permission model.
|
|
62
|
-
*/
|
|
63
69
|
export declare function stopServer(rosterPath: string): Promise<{
|
|
64
70
|
stopped: boolean;
|
|
65
71
|
}>;
|
|
72
|
+
export type SuperviseOutcome = {
|
|
73
|
+
reason: "signal";
|
|
74
|
+
} | {
|
|
75
|
+
reason: "died";
|
|
76
|
+
} | {
|
|
77
|
+
reason: "hung";
|
|
78
|
+
};
|
|
79
|
+
export interface SuperviseServerOptions {
|
|
80
|
+
/** Interval between liveness ticks. Default `SUPERVISE_INTERVAL_MS`. */
|
|
81
|
+
intervalMs?: number;
|
|
82
|
+
/** Consecutive `retry` probes (pid alive) before declaring the daemon
|
|
83
|
+
* hung. Default `SUPERVISE_FAILURE_THRESHOLD`. */
|
|
84
|
+
threshold?: number;
|
|
85
|
+
/** Absolute-lifetime watchdog budget. Default
|
|
86
|
+
* `SUPERVISE_UNHEALTHY_BUDGET_MS`. */
|
|
87
|
+
unhealthyBudgetMs?: number;
|
|
88
|
+
/** Injectable sleep, for deterministic tests. */
|
|
89
|
+
sleep?: (ms: number) => Promise<void>;
|
|
90
|
+
/** Injectable clock, for deterministic tests. Default `Date.now`. */
|
|
91
|
+
now?: () => number;
|
|
92
|
+
/** Injectable serverStatus, for deterministic tests. */
|
|
93
|
+
status?: typeof serverStatus;
|
|
94
|
+
/** Injectable probe, for deterministic tests. */
|
|
95
|
+
probe?: typeof probe;
|
|
96
|
+
/** Injectable stopServer, for deterministic tests. */
|
|
97
|
+
stop?: typeof stopServer;
|
|
98
|
+
/** Checked at the top of every tick; returning true breaks the loop with
|
|
99
|
+
* `{ reason: "signal" }` before any liveness work happens on that tick. */
|
|
100
|
+
shouldStop?: () => boolean;
|
|
101
|
+
/** Resolves to break the inter-tick sleep immediately (e.g. wired to a
|
|
102
|
+
* signal handler) instead of waiting out the full `intervalMs`. If
|
|
103
|
+
* undefined, the loop just sleeps the full interval. */
|
|
104
|
+
interrupt?: Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
export declare function superviseServer(rosterPath: string, handle: ServerHandle, opts?: SuperviseServerOptions): Promise<SuperviseOutcome>;
|