@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/server.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,
|
|
@@ -188,14 +191,27 @@ function removeDiscovery(rosterPath) {
|
|
|
188
191
|
throw err;
|
|
189
192
|
}
|
|
190
193
|
}
|
|
194
|
+
function removeDiscoveryIfMatches(rosterPath, expected) {
|
|
195
|
+
try {
|
|
196
|
+
const current = readDiscovery(rosterPath);
|
|
197
|
+
if (current && current.pid === expected.pid && current.identity === expected.identity) {
|
|
198
|
+
removeDiscovery(rosterPath);
|
|
199
|
+
}
|
|
200
|
+
} catch {}
|
|
201
|
+
}
|
|
191
202
|
function attachLive(rosterPath) {
|
|
192
203
|
const discovery = readDiscovery(rosterPath);
|
|
193
204
|
if (!discovery)
|
|
194
205
|
return null;
|
|
195
206
|
if (!loopbackOk(discovery.baseUrl))
|
|
196
207
|
return null;
|
|
197
|
-
if (!verifyIdentity(discovery.pid, discovery.identity))
|
|
208
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
209
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
210
|
+
pid: discovery.pid,
|
|
211
|
+
identity: discovery.identity
|
|
212
|
+
});
|
|
198
213
|
return null;
|
|
214
|
+
}
|
|
199
215
|
return {
|
|
200
216
|
baseUrl: discovery.baseUrl,
|
|
201
217
|
credentials: { username: "opencode", password: discovery.password },
|
|
@@ -230,7 +246,7 @@ function verifyIdentity(pid, storedIdentity) {
|
|
|
230
246
|
return current !== null && current === storedIdentity;
|
|
231
247
|
}
|
|
232
248
|
var lockFileSchema = z2.object({
|
|
233
|
-
pid: z2.number().int().
|
|
249
|
+
pid: z2.number().int().min(2),
|
|
234
250
|
startTime: z2.string(),
|
|
235
251
|
since: z2.number()
|
|
236
252
|
});
|
|
@@ -326,7 +342,7 @@ function provisionalFilePath(rosterPath) {
|
|
|
326
342
|
return join(stateDirFor(rosterPath), "spawn.provisional.json");
|
|
327
343
|
}
|
|
328
344
|
var provisionalFileSchema = z2.object({
|
|
329
|
-
pid: z2.number().int().
|
|
345
|
+
pid: z2.number().int().min(2),
|
|
330
346
|
identity: z2.string(),
|
|
331
347
|
password: z2.string(),
|
|
332
348
|
since: z2.number()
|
|
@@ -495,6 +511,9 @@ var POLL_INTERVAL_MS = 50;
|
|
|
495
511
|
var MIN_PROBE_PHASE_MS = 3000;
|
|
496
512
|
var PROBE_FETCH_TIMEOUT_MS = 2000;
|
|
497
513
|
var STOP_GRACE_MS = 2000;
|
|
514
|
+
var SUPERVISE_INTERVAL_MS = 5000;
|
|
515
|
+
var SUPERVISE_FAILURE_THRESHOLD = 3;
|
|
516
|
+
var SUPERVISE_UNHEALTHY_BUDGET_MS = 60000;
|
|
498
517
|
function sleep(ms) {
|
|
499
518
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
500
519
|
}
|
|
@@ -522,11 +541,34 @@ function redactedReadinessError(rosterPath, password, reason) {
|
|
|
522
541
|
return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
|
|
523
542
|
${tail}`);
|
|
524
543
|
}
|
|
544
|
+
function isEpermError(err) {
|
|
545
|
+
return typeof err === "object" && err !== null && "code" in err && err.code === "EPERM";
|
|
546
|
+
}
|
|
547
|
+
function signalGroup(pid, sig) {
|
|
548
|
+
if (pid <= 1) {
|
|
549
|
+
try {
|
|
550
|
+
process.kill(pid, sig);
|
|
551
|
+
} catch {}
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
try {
|
|
555
|
+
process.kill(-pid, sig);
|
|
556
|
+
return true;
|
|
557
|
+
} catch (err) {
|
|
558
|
+
if (err.code === "EPERM") {
|
|
559
|
+
throw err;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
try {
|
|
563
|
+
process.kill(pid, sig);
|
|
564
|
+
} catch {}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
525
567
|
function killIdentifiedProcess(pid, identity) {
|
|
526
568
|
try {
|
|
527
569
|
if (identity !== null && identity !== undefined && identity !== "") {
|
|
528
570
|
if (verifyIdentity(pid, identity))
|
|
529
|
-
|
|
571
|
+
signalGroup(pid, "SIGTERM");
|
|
530
572
|
return;
|
|
531
573
|
}
|
|
532
574
|
process.kill(pid, "SIGTERM");
|
|
@@ -739,7 +781,14 @@ async function ensureServer(rosterPath, options = {}) {
|
|
|
739
781
|
}
|
|
740
782
|
function serverStatus(rosterPath) {
|
|
741
783
|
const discovery = readDiscovery(rosterPath);
|
|
742
|
-
if (!discovery
|
|
784
|
+
if (!discovery) {
|
|
785
|
+
return { running: false };
|
|
786
|
+
}
|
|
787
|
+
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
788
|
+
removeDiscoveryIfMatches(rosterPath, {
|
|
789
|
+
pid: discovery.pid,
|
|
790
|
+
identity: discovery.identity
|
|
791
|
+
});
|
|
743
792
|
return { running: false };
|
|
744
793
|
}
|
|
745
794
|
let configDrift;
|
|
@@ -775,28 +824,105 @@ async function stopServer(rosterPath) {
|
|
|
775
824
|
return { stopped: false };
|
|
776
825
|
}
|
|
777
826
|
const { pid } = discovery;
|
|
827
|
+
let groupSignaled;
|
|
828
|
+
const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
|
|
778
829
|
try {
|
|
779
|
-
|
|
780
|
-
} catch {
|
|
830
|
+
groupSignaled = signalGroup(pid, "SIGTERM");
|
|
831
|
+
} catch (err) {
|
|
832
|
+
if (isEpermError(err)) {
|
|
833
|
+
return { stopped: false };
|
|
834
|
+
}
|
|
781
835
|
removeDiscovery(rosterPath);
|
|
782
836
|
return { stopped: true };
|
|
783
837
|
}
|
|
784
|
-
if (await
|
|
838
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
785
839
|
removeDiscovery(rosterPath);
|
|
786
840
|
return { stopped: true };
|
|
787
841
|
}
|
|
788
842
|
try {
|
|
789
|
-
|
|
790
|
-
} catch {
|
|
843
|
+
groupSignaled = signalGroup(pid, "SIGKILL");
|
|
844
|
+
} catch (err) {
|
|
845
|
+
if (isEpermError(err)) {
|
|
846
|
+
return { stopped: false };
|
|
847
|
+
}
|
|
791
848
|
removeDiscovery(rosterPath);
|
|
792
849
|
return { stopped: true };
|
|
793
850
|
}
|
|
794
|
-
if (await
|
|
851
|
+
if (await waitForCompletion(STOP_GRACE_MS)) {
|
|
795
852
|
removeDiscovery(rosterPath);
|
|
796
853
|
return { stopped: true };
|
|
797
854
|
}
|
|
798
855
|
return { stopped: false };
|
|
799
856
|
}
|
|
857
|
+
async function stopForHung(tick) {
|
|
858
|
+
try {
|
|
859
|
+
await tick.stop(tick.rosterPath);
|
|
860
|
+
} catch {}
|
|
861
|
+
return { kind: "outcome", outcome: { reason: "hung" } };
|
|
862
|
+
}
|
|
863
|
+
async function superviseTick(tick, state) {
|
|
864
|
+
const st = tick.status(tick.rosterPath);
|
|
865
|
+
if (!st.running) {
|
|
866
|
+
return { kind: "outcome", outcome: { reason: "died" } };
|
|
867
|
+
}
|
|
868
|
+
const probeOutcome = await tick.probe(tick.handle.baseUrl, tick.password);
|
|
869
|
+
const failures = probeOutcome === "retry" ? state.failures + 1 : 0;
|
|
870
|
+
const lastReadyAt = probeOutcome === "ready" ? tick.now() : state.lastReadyAt;
|
|
871
|
+
if (failures >= tick.threshold) {
|
|
872
|
+
return stopForHung(tick);
|
|
873
|
+
}
|
|
874
|
+
if (tick.now() - lastReadyAt >= tick.unhealthyBudgetMs) {
|
|
875
|
+
return stopForHung(tick);
|
|
876
|
+
}
|
|
877
|
+
return { kind: "tick", failures, lastReadyAt };
|
|
878
|
+
}
|
|
879
|
+
function makeSuperviseTick(rosterPath, handle, opts) {
|
|
880
|
+
return {
|
|
881
|
+
rosterPath,
|
|
882
|
+
handle,
|
|
883
|
+
password: handle.credentials.password ?? "",
|
|
884
|
+
threshold: opts.threshold ?? SUPERVISE_FAILURE_THRESHOLD,
|
|
885
|
+
unhealthyBudgetMs: opts.unhealthyBudgetMs ?? SUPERVISE_UNHEALTHY_BUDGET_MS,
|
|
886
|
+
now: opts.now ?? Date.now,
|
|
887
|
+
status: opts.status ?? serverStatus,
|
|
888
|
+
probe: opts.probe ?? probe,
|
|
889
|
+
stop: opts.stop ?? stopServer
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
async function interruptibleSleep(doSleep, intervalMs, interrupt) {
|
|
893
|
+
if (!interrupt) {
|
|
894
|
+
await doSleep(intervalMs);
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
let timer;
|
|
898
|
+
const timed = new Promise((resolve) => {
|
|
899
|
+
timer = setTimeout(resolve, intervalMs);
|
|
900
|
+
});
|
|
901
|
+
try {
|
|
902
|
+
await Promise.race([timed, interrupt]);
|
|
903
|
+
} finally {
|
|
904
|
+
if (timer !== undefined)
|
|
905
|
+
clearTimeout(timer);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
async function superviseServer(rosterPath, handle, opts = {}) {
|
|
909
|
+
const intervalMs = opts.intervalMs ?? SUPERVISE_INTERVAL_MS;
|
|
910
|
+
const tick = makeSuperviseTick(rosterPath, handle, opts);
|
|
911
|
+
const doSleep = opts.sleep ?? sleep;
|
|
912
|
+
const shouldStop = opts.shouldStop ?? (() => false);
|
|
913
|
+
let state = { failures: 0, lastReadyAt: tick.now() };
|
|
914
|
+
for (;; ) {
|
|
915
|
+
if (shouldStop())
|
|
916
|
+
return { reason: "signal" };
|
|
917
|
+
const result = await superviseTick(tick, state);
|
|
918
|
+
if (result.kind === "outcome")
|
|
919
|
+
return result.outcome;
|
|
920
|
+
state = { failures: result.failures, lastReadyAt: result.lastReadyAt };
|
|
921
|
+
if (shouldStop())
|
|
922
|
+
return { reason: "signal" };
|
|
923
|
+
await interruptibleSleep(doSleep, intervalMs, opts.interrupt);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
800
926
|
async function waitForDeath(pid, budgetMs) {
|
|
801
927
|
const deadline = Date.now() + budgetMs;
|
|
802
928
|
while (Date.now() < deadline) {
|
|
@@ -806,10 +932,32 @@ async function waitForDeath(pid, budgetMs) {
|
|
|
806
932
|
}
|
|
807
933
|
return !isAlive(pid);
|
|
808
934
|
}
|
|
935
|
+
async function waitForGroupDeath(pid, budgetMs) {
|
|
936
|
+
const deadline = Date.now() + budgetMs;
|
|
937
|
+
const groupAlive = () => {
|
|
938
|
+
try {
|
|
939
|
+
process.kill(-pid, 0);
|
|
940
|
+
return true;
|
|
941
|
+
} catch {
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
while (Date.now() < deadline) {
|
|
946
|
+
if (!groupAlive())
|
|
947
|
+
return true;
|
|
948
|
+
await sleep(POLL_INTERVAL_MS);
|
|
949
|
+
}
|
|
950
|
+
return !groupAlive();
|
|
951
|
+
}
|
|
809
952
|
export {
|
|
953
|
+
superviseServer,
|
|
810
954
|
stopServer,
|
|
811
955
|
serverStatus,
|
|
812
956
|
redactSensitive,
|
|
957
|
+
probe,
|
|
813
958
|
ensureServer,
|
|
814
|
-
attachServer
|
|
959
|
+
attachServer,
|
|
960
|
+
SUPERVISE_UNHEALTHY_BUDGET_MS,
|
|
961
|
+
SUPERVISE_INTERVAL_MS,
|
|
962
|
+
SUPERVISE_FAILURE_THRESHOLD
|
|
815
963
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fro.bot/space-bus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Space Bus — workspace agent bus for OpenCode. One control agent tasking per-project agents over the OpenCode server API, with an MCP facade for Claude Desktop.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@biomejs/biome": "2.5.2",
|
|
76
76
|
"@changesets/cli": "2.31.0",
|
|
77
|
-
"@opencode-ai/plugin": "1.17.
|
|
77
|
+
"@opencode-ai/plugin": "1.17.15",
|
|
78
78
|
"@types/bun": "1.3.14",
|
|
79
79
|
"typescript": "5.9.3"
|
|
80
80
|
}
|