@fro.bot/space-bus 0.12.0 → 0.13.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/README.md +7 -1
- package/dist/cli.js +457 -59
- package/dist/config.d.ts +53 -0
- package/dist/config.js +415 -13
- package/dist/format.d.ts +13 -0
- package/dist/format.js +4 -0
- package/dist/index.js +1201 -195
- package/dist/mcp.d.ts +3 -1
- package/dist/mcp.js +1235 -117
- package/dist/registry-entry.js +159 -135
- package/dist/registry.d.ts +1 -0
- package/dist/server.js +418 -20
- package/dist/tools/bus_registry.d.ts +123 -0
- package/dist/tools/shared.d.ts +31 -1
- package/package.json +2 -2
package/dist/server.js
CHANGED
|
@@ -2,9 +2,9 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
3
3
|
|
|
4
4
|
// src/config.ts
|
|
5
|
-
import { existsSync, readFileSync as
|
|
6
|
-
import { homedir as
|
|
7
|
-
import { join as
|
|
5
|
+
import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
|
|
6
|
+
import { homedir as homedir3 } from "node:os";
|
|
7
|
+
import { join as join3 } from "node:path";
|
|
8
8
|
import { z as z3 } from "zod";
|
|
9
9
|
|
|
10
10
|
// src/contract.ts
|
|
@@ -433,6 +433,380 @@ function removeProvisional(rosterPath) {
|
|
|
433
433
|
} catch {}
|
|
434
434
|
}
|
|
435
435
|
|
|
436
|
+
// src/registry.ts
|
|
437
|
+
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
438
|
+
import {
|
|
439
|
+
closeSync as closeSync2,
|
|
440
|
+
constants as fsConstants2,
|
|
441
|
+
fstatSync,
|
|
442
|
+
lstatSync,
|
|
443
|
+
mkdirSync as mkdirSync2,
|
|
444
|
+
openSync as openSync2,
|
|
445
|
+
readFileSync as readFileSync2,
|
|
446
|
+
realpathSync,
|
|
447
|
+
renameSync as renameSync2,
|
|
448
|
+
statSync as statSync2,
|
|
449
|
+
unlinkSync as unlinkSync2,
|
|
450
|
+
writeSync
|
|
451
|
+
} from "node:fs";
|
|
452
|
+
import { homedir as homedir2 } from "node:os";
|
|
453
|
+
import { isAbsolute, join as join2 } from "node:path";
|
|
454
|
+
var EMPTY_REGISTRY = { version: 1, rosters: [] };
|
|
455
|
+
function registryPath() {
|
|
456
|
+
const base = process.env["XDG_CONFIG_HOME"] ?? join2(homedir2(), ".config");
|
|
457
|
+
return join2(base, "space-bus", "rosters.json");
|
|
458
|
+
}
|
|
459
|
+
function isEnoent2(err) {
|
|
460
|
+
return isErrnoCode2(err, "ENOENT");
|
|
461
|
+
}
|
|
462
|
+
function isErrnoCode2(err, code) {
|
|
463
|
+
return typeof err === "object" && err !== null && "code" in err && err.code === code;
|
|
464
|
+
}
|
|
465
|
+
function closeQuietly2(fd) {
|
|
466
|
+
try {
|
|
467
|
+
closeSync2(fd);
|
|
468
|
+
} catch {}
|
|
469
|
+
}
|
|
470
|
+
function readRegistry() {
|
|
471
|
+
const target = registryPath();
|
|
472
|
+
let raw;
|
|
473
|
+
try {
|
|
474
|
+
raw = readFileSync2(target, "utf8");
|
|
475
|
+
} catch (err) {
|
|
476
|
+
if (isEnoent2(err)) {
|
|
477
|
+
return { ok: true, registry: EMPTY_REGISTRY };
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
ok: false,
|
|
481
|
+
error: `space-bus: cannot read registry at ${target}: ${String(err)}`
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
let json;
|
|
485
|
+
try {
|
|
486
|
+
json = JSON.parse(raw);
|
|
487
|
+
} catch (err) {
|
|
488
|
+
return {
|
|
489
|
+
ok: false,
|
|
490
|
+
error: `space-bus: registry at ${target} is not valid JSON: ${String(err)}`
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
const parsed = registryFileSchema.safeParse(json);
|
|
494
|
+
if (!parsed.success) {
|
|
495
|
+
return {
|
|
496
|
+
ok: false,
|
|
497
|
+
error: `space-bus: registry at ${target} failed schema validation: ${parsed.error.message}`
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
const nonAbsolute = parsed.data.rosters.find((entry) => !isAbsolute(entry.path));
|
|
501
|
+
if (nonAbsolute) {
|
|
502
|
+
return {
|
|
503
|
+
ok: false,
|
|
504
|
+
error: `space-bus: registry at ${target} contains a non-absolute path for roster "${nonAbsolute.name}": ${nonAbsolute.path}`
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
return { ok: true, registry: parsed.data };
|
|
508
|
+
}
|
|
509
|
+
function writeRegistryAtomic(data) {
|
|
510
|
+
const target = registryPath();
|
|
511
|
+
const dir = join2(target, "..");
|
|
512
|
+
try {
|
|
513
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
514
|
+
} catch (err) {
|
|
515
|
+
return {
|
|
516
|
+
ok: false,
|
|
517
|
+
error: `space-bus: failed to create registry dir ${dir}: ${String(err)}`
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
const tempPath = join2(dir, `.rosters.${randomBytes2(8).toString("hex")}.tmp`);
|
|
521
|
+
let fd;
|
|
522
|
+
try {
|
|
523
|
+
fd = openSync2(tempPath, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
524
|
+
writeSync(fd, JSON.stringify(data, null, 2));
|
|
525
|
+
closeSync2(fd);
|
|
526
|
+
fd = undefined;
|
|
527
|
+
renameSync2(tempPath, target);
|
|
528
|
+
} catch (err) {
|
|
529
|
+
if (fd !== undefined)
|
|
530
|
+
closeQuietly2(fd);
|
|
531
|
+
try {
|
|
532
|
+
unlinkSync2(tempPath);
|
|
533
|
+
} catch {}
|
|
534
|
+
return {
|
|
535
|
+
ok: false,
|
|
536
|
+
error: `space-bus: failed to write registry at ${target}: ${String(err)}`
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
return { ok: true };
|
|
540
|
+
}
|
|
541
|
+
function registryLockPath() {
|
|
542
|
+
return join2(registryPath(), "..", "rosters.lock");
|
|
543
|
+
}
|
|
544
|
+
var LOCK_RETRY_COUNT = 10;
|
|
545
|
+
var LOCK_RETRY_DELAY_MS = 50;
|
|
546
|
+
var LOCK_STALE_MS = 1e4;
|
|
547
|
+
function sleepSync(ms) {
|
|
548
|
+
const buf = new Int32Array(new SharedArrayBuffer(4));
|
|
549
|
+
Atomics.wait(buf, 0, 0, ms);
|
|
550
|
+
}
|
|
551
|
+
function tryCreateRegistryLock(target) {
|
|
552
|
+
let fd;
|
|
553
|
+
try {
|
|
554
|
+
fd = openSync2(target, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
555
|
+
} catch (err) {
|
|
556
|
+
if (isErrnoCode2(err, "EEXIST"))
|
|
557
|
+
return false;
|
|
558
|
+
throw err;
|
|
559
|
+
}
|
|
560
|
+
try {
|
|
561
|
+
writeSync(fd, String(process.pid));
|
|
562
|
+
} finally {
|
|
563
|
+
closeQuietly2(fd);
|
|
564
|
+
}
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
function isRegistryLockStale(target) {
|
|
568
|
+
try {
|
|
569
|
+
return Date.now() - statSync2(target).mtimeMs > LOCK_STALE_MS;
|
|
570
|
+
} catch {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
function breakStaleAndRetry(target) {
|
|
575
|
+
if (!isRegistryLockStale(target))
|
|
576
|
+
return false;
|
|
577
|
+
try {
|
|
578
|
+
unlinkSync2(target);
|
|
579
|
+
} catch (err) {
|
|
580
|
+
if (!isEnoent2(err))
|
|
581
|
+
throw err;
|
|
582
|
+
}
|
|
583
|
+
return tryCreateRegistryLock(target);
|
|
584
|
+
}
|
|
585
|
+
function acquireRegistryLock() {
|
|
586
|
+
const dir = join2(registryPath(), "..");
|
|
587
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
588
|
+
const target = registryLockPath();
|
|
589
|
+
for (let attempt = 0;attempt <= LOCK_RETRY_COUNT; attempt++) {
|
|
590
|
+
if (tryCreateRegistryLock(target))
|
|
591
|
+
return true;
|
|
592
|
+
if (breakStaleAndRetry(target))
|
|
593
|
+
return true;
|
|
594
|
+
if (attempt < LOCK_RETRY_COUNT)
|
|
595
|
+
sleepSync(LOCK_RETRY_DELAY_MS);
|
|
596
|
+
}
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
function releaseRegistryLock() {
|
|
600
|
+
try {
|
|
601
|
+
unlinkSync2(registryLockPath());
|
|
602
|
+
} catch {}
|
|
603
|
+
}
|
|
604
|
+
function withRegistryLock(mutate) {
|
|
605
|
+
if (!acquireRegistryLock()) {
|
|
606
|
+
return {
|
|
607
|
+
ok: false,
|
|
608
|
+
error: "space-bus: timed out waiting for the registry lock (another process is mutating rosters.json) — try again"
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
try {
|
|
612
|
+
return mutate();
|
|
613
|
+
} finally {
|
|
614
|
+
releaseRegistryLock();
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function openAndValidateRosterFile(rosterPath) {
|
|
618
|
+
let fd;
|
|
619
|
+
try {
|
|
620
|
+
fd = openSync2(rosterPath, fsConstants2.O_RDONLY | fsConstants2.O_NOFOLLOW);
|
|
621
|
+
} catch (err) {
|
|
622
|
+
if (isErrnoCode2(err, "ELOOP")) {
|
|
623
|
+
return {
|
|
624
|
+
ok: false,
|
|
625
|
+
error: `space-bus: roster path ${rosterPath} is a symlink — refusing to register a symlinked roster file`
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
if (isEnoent2(err)) {
|
|
629
|
+
return {
|
|
630
|
+
ok: false,
|
|
631
|
+
error: `space-bus: roster path ${rosterPath} does not exist: ${String(err)}`
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
ok: false,
|
|
636
|
+
error: `space-bus: failed to open roster path ${rosterPath}: ${String(err)}`
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
try {
|
|
640
|
+
let stat;
|
|
641
|
+
try {
|
|
642
|
+
stat = fstatSync(fd);
|
|
643
|
+
} catch (err) {
|
|
644
|
+
return {
|
|
645
|
+
ok: false,
|
|
646
|
+
error: `space-bus: failed to stat roster path ${rosterPath}: ${String(err)}`
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
if (!stat.isFile()) {
|
|
650
|
+
return {
|
|
651
|
+
ok: false,
|
|
652
|
+
error: `space-bus: roster path ${rosterPath} is not a regular file — refusing to register`
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
let raw;
|
|
656
|
+
try {
|
|
657
|
+
raw = readFileSync2(fd, "utf8");
|
|
658
|
+
} catch (err) {
|
|
659
|
+
return {
|
|
660
|
+
ok: false,
|
|
661
|
+
error: `space-bus: failed to read roster path ${rosterPath}: ${String(err)}`
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
let json;
|
|
665
|
+
try {
|
|
666
|
+
json = JSON.parse(raw);
|
|
667
|
+
} catch (err) {
|
|
668
|
+
return {
|
|
669
|
+
ok: false,
|
|
670
|
+
error: `space-bus: roster path ${rosterPath} is not valid JSON: ${String(err)}`
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
const parsed = manifestSchema.safeParse(json);
|
|
674
|
+
if (!parsed.success) {
|
|
675
|
+
return {
|
|
676
|
+
ok: false,
|
|
677
|
+
error: `space-bus: roster path ${rosterPath} is not a valid spacebus.json — refusing to register: ${parsed.error.message}`
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
} finally {
|
|
681
|
+
closeQuietly2(fd);
|
|
682
|
+
}
|
|
683
|
+
return { ok: true };
|
|
684
|
+
}
|
|
685
|
+
function registerRoster(name, rosterPath) {
|
|
686
|
+
const nameCheck = registryEntrySchema.shape.name.safeParse(name);
|
|
687
|
+
if (!nameCheck.success) {
|
|
688
|
+
return {
|
|
689
|
+
ok: false,
|
|
690
|
+
error: `space-bus: invalid roster name "${name}": ${nameCheck.error.issues[0]?.message ?? "must be 1-64 lowercase letters, digits, or hyphens"}`
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
const validated = openAndValidateRosterFile(rosterPath);
|
|
694
|
+
if (!validated.ok)
|
|
695
|
+
return validated;
|
|
696
|
+
let canonicalPath;
|
|
697
|
+
try {
|
|
698
|
+
canonicalPath = realpathSync(rosterPath);
|
|
699
|
+
} catch (err) {
|
|
700
|
+
return {
|
|
701
|
+
ok: false,
|
|
702
|
+
error: `space-bus: failed to canonicalize roster path ${rosterPath}: ${String(err)}`
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
return withRegistryLock(() => {
|
|
706
|
+
const readResult = readRegistry();
|
|
707
|
+
if (!readResult.ok)
|
|
708
|
+
return readResult;
|
|
709
|
+
const registry = readResult.registry;
|
|
710
|
+
const collision = registry.rosters.find((entry2) => entry2.name.toLowerCase() === name.toLowerCase());
|
|
711
|
+
if (collision) {
|
|
712
|
+
return {
|
|
713
|
+
ok: false,
|
|
714
|
+
error: `space-bus: a roster named "${collision.name}" is already registered`
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
const entry = { name, path: canonicalPath };
|
|
718
|
+
const next = {
|
|
719
|
+
...registry,
|
|
720
|
+
rosters: [...registry.rosters, entry]
|
|
721
|
+
};
|
|
722
|
+
return writeRegistryAtomic(next);
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
function unregisterRoster(name) {
|
|
726
|
+
return withRegistryLock(() => {
|
|
727
|
+
const readResult = readRegistry();
|
|
728
|
+
if (!readResult.ok)
|
|
729
|
+
return readResult;
|
|
730
|
+
const registry = readResult.registry;
|
|
731
|
+
const exists = registry.rosters.some((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
732
|
+
if (!exists) {
|
|
733
|
+
return {
|
|
734
|
+
ok: false,
|
|
735
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
const rosters = registry.rosters.filter((entry) => entry.name.toLowerCase() !== name.toLowerCase());
|
|
739
|
+
const next = {
|
|
740
|
+
...registry,
|
|
741
|
+
rosters,
|
|
742
|
+
default: registry.default !== undefined && registry.default.toLowerCase() === name.toLowerCase() ? undefined : registry.default
|
|
743
|
+
};
|
|
744
|
+
return writeRegistryAtomic(next);
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
function setDefaultRoster(name) {
|
|
748
|
+
return withRegistryLock(() => {
|
|
749
|
+
const readResult = readRegistry();
|
|
750
|
+
if (!readResult.ok)
|
|
751
|
+
return readResult;
|
|
752
|
+
const registry = readResult.registry;
|
|
753
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
754
|
+
if (!entry) {
|
|
755
|
+
return {
|
|
756
|
+
ok: false,
|
|
757
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
const next = { ...registry, default: entry.name };
|
|
761
|
+
return writeRegistryAtomic(next);
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
function resolveRosterByName(name) {
|
|
765
|
+
const readResult = readRegistry();
|
|
766
|
+
if (!readResult.ok)
|
|
767
|
+
return readResult;
|
|
768
|
+
const registry = readResult.registry;
|
|
769
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
770
|
+
if (!entry) {
|
|
771
|
+
const known = registry.rosters.map((r) => r.name);
|
|
772
|
+
return {
|
|
773
|
+
ok: false,
|
|
774
|
+
error: known.length > 0 ? `space-bus: no roster named "${name}" is registered (known: ${known.join(", ")})` : `space-bus: no roster named "${name}" is registered (no rosters are registered)`
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
let stat;
|
|
778
|
+
try {
|
|
779
|
+
stat = lstatSync(entry.path);
|
|
780
|
+
} catch {
|
|
781
|
+
return {
|
|
782
|
+
ok: false,
|
|
783
|
+
error: `space-bus: registered roster file no longer exists at ${entry.path} (name "${entry.name}") — was it moved or deleted?`
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
if (stat.isSymbolicLink()) {
|
|
787
|
+
return {
|
|
788
|
+
ok: false,
|
|
789
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is now a symlink — refusing to resolve a symlinked roster file`
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
if (!stat.isFile()) {
|
|
793
|
+
return {
|
|
794
|
+
ok: false,
|
|
795
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is no longer a regular file`
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
let canonicalPath;
|
|
799
|
+
try {
|
|
800
|
+
canonicalPath = realpathSync(entry.path);
|
|
801
|
+
} catch (err) {
|
|
802
|
+
return {
|
|
803
|
+
ok: false,
|
|
804
|
+
error: `space-bus: failed to canonicalize registered roster path ${entry.path} (name "${entry.name}"): ${String(err)}`
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
return { ok: true, path: canonicalPath, name: entry.name };
|
|
808
|
+
}
|
|
809
|
+
|
|
436
810
|
// src/config.ts
|
|
437
811
|
var managedServerConfigSchema = z3.object({
|
|
438
812
|
command: z3.array(z3.string()).optional(),
|
|
@@ -466,7 +840,7 @@ var manifestSchema = z3.object({
|
|
|
466
840
|
}))
|
|
467
841
|
});
|
|
468
842
|
function expandHome(path) {
|
|
469
|
-
return path.startsWith("~") ?
|
|
843
|
+
return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
|
|
470
844
|
}
|
|
471
845
|
function resolveRosterPath(directory) {
|
|
472
846
|
const override = process.env["SPACE_BUS_CONFIG"];
|
|
@@ -478,19 +852,18 @@ function resolveRosterPath(directory) {
|
|
|
478
852
|
if (!expanded.startsWith("/")) {
|
|
479
853
|
throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
|
|
480
854
|
}
|
|
481
|
-
return existsSync(expanded) ?
|
|
855
|
+
return existsSync(expanded) ? realpathSync2(expanded) : expanded;
|
|
482
856
|
}
|
|
483
857
|
if (directory) {
|
|
484
|
-
const candidate =
|
|
485
|
-
return existsSync(candidate) ?
|
|
858
|
+
const candidate = join3(directory, "spacebus.json");
|
|
859
|
+
return existsSync(candidate) ? realpathSync2(candidate) : candidate;
|
|
486
860
|
}
|
|
487
861
|
throw new Error("space-bus: cannot locate spacebus.json — pass a directory containing spacebus.json, or set SPACE_BUS_CONFIG to an absolute path (or ~-prefixed path) to the roster file");
|
|
488
862
|
}
|
|
489
|
-
function
|
|
490
|
-
const manifestPath = resolveRosterPath(directory);
|
|
863
|
+
function getRosterAtPath(manifestPath) {
|
|
491
864
|
let raw;
|
|
492
865
|
try {
|
|
493
|
-
raw =
|
|
866
|
+
raw = readFileSync3(manifestPath, "utf8");
|
|
494
867
|
} catch (err) {
|
|
495
868
|
throw new Error(`space-bus: cannot read roster at ${manifestPath}: ${err.message} (set SPACE_BUS_CONFIG or ensure spacebus.json exists in the workspace directory)`);
|
|
496
869
|
}
|
|
@@ -513,6 +886,10 @@ function getRoster(directory) {
|
|
|
513
886
|
}
|
|
514
887
|
return parsed.data;
|
|
515
888
|
}
|
|
889
|
+
function getRoster(directory) {
|
|
890
|
+
const manifestPath = resolveRosterPath(directory);
|
|
891
|
+
return getRosterAtPath(manifestPath);
|
|
892
|
+
}
|
|
516
893
|
function getProjects(manifest) {
|
|
517
894
|
return manifest.projects.map((p) => {
|
|
518
895
|
const expandedPath = expandHome(p.path);
|
|
@@ -526,9 +903,8 @@ function getCredentials() {
|
|
|
526
903
|
const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
|
|
527
904
|
return { username, password };
|
|
528
905
|
}
|
|
529
|
-
function
|
|
530
|
-
const
|
|
531
|
-
const manifest = getRoster(directory);
|
|
906
|
+
function loadContextAtPath(rosterPath) {
|
|
907
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
532
908
|
const projects = getProjects(manifest);
|
|
533
909
|
if (manifest.server.baseUrl === undefined) {
|
|
534
910
|
const live = attachLive(rosterPath);
|
|
@@ -545,6 +921,28 @@ function loadContext(directory) {
|
|
|
545
921
|
credentials: getCredentials()
|
|
546
922
|
};
|
|
547
923
|
}
|
|
924
|
+
function loadContext(directory) {
|
|
925
|
+
const rosterPath = resolveRosterPath(directory);
|
|
926
|
+
return loadContextAtPath(rosterPath);
|
|
927
|
+
}
|
|
928
|
+
function loadContextForRoster(rosterName) {
|
|
929
|
+
const resolved = resolveRosterByName(rosterName);
|
|
930
|
+
if (!resolved.ok) {
|
|
931
|
+
throw new Error(resolved.error);
|
|
932
|
+
}
|
|
933
|
+
return {
|
|
934
|
+
context: loadContextAtPath(resolved.path),
|
|
935
|
+
rosterPath: resolved.path,
|
|
936
|
+
rosterName: resolved.name
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
function loadContextForRosterPath(rosterPath) {
|
|
940
|
+
return loadContextAtPath(rosterPath);
|
|
941
|
+
}
|
|
942
|
+
function isManagedRosterAtPath(rosterPath) {
|
|
943
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
944
|
+
return manifest.server.managed !== undefined;
|
|
945
|
+
}
|
|
548
946
|
function isManagedRoster(directory) {
|
|
549
947
|
const manifest = getRoster(directory);
|
|
550
948
|
return manifest.server.managed !== undefined;
|
|
@@ -552,8 +950,8 @@ function isManagedRoster(directory) {
|
|
|
552
950
|
|
|
553
951
|
// src/server.ts
|
|
554
952
|
import { spawn } from "node:child_process";
|
|
555
|
-
import { randomBytes as
|
|
556
|
-
import { existsSync as existsSync2, openSync as
|
|
953
|
+
import { randomBytes as randomBytes3 } from "node:crypto";
|
|
954
|
+
import { existsSync as existsSync2, openSync as openSync3, readFileSync as readFileSync4, realpathSync as realpathSync3 } from "node:fs";
|
|
557
955
|
import { dirname } from "node:path";
|
|
558
956
|
var DEFAULT_READINESS_BUDGET_MS = 15000;
|
|
559
957
|
var DEFAULT_LOCK_WAIT_BUDGET_MS = 15000;
|
|
@@ -568,13 +966,13 @@ function sleep(ms) {
|
|
|
568
966
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
569
967
|
}
|
|
570
968
|
function readManifest(rosterPath) {
|
|
571
|
-
const raw =
|
|
969
|
+
const raw = readFileSync4(rosterPath, "utf8");
|
|
572
970
|
const json = JSON.parse(raw);
|
|
573
971
|
return manifestSchema.parse(json);
|
|
574
972
|
}
|
|
575
973
|
function readLogTail(logPath, maxChars = 4000) {
|
|
576
974
|
try {
|
|
577
|
-
const content =
|
|
975
|
+
const content = readFileSync4(logPath, "utf8");
|
|
578
976
|
return content.length > maxChars ? content.slice(-maxChars) : content;
|
|
579
977
|
} catch {
|
|
580
978
|
return "";
|
|
@@ -747,12 +1145,12 @@ function reapOrphanedProvisional(rosterPath) {
|
|
|
747
1145
|
}
|
|
748
1146
|
async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
749
1147
|
reapOrphanedProvisional(rosterPath);
|
|
750
|
-
const password =
|
|
1148
|
+
const password = randomBytes3(32).toString("base64url");
|
|
751
1149
|
const command = managedConfig.command && managedConfig.command.length > 0 ? managedConfig.command : ["harness", "serve"];
|
|
752
1150
|
const cwd = managedConfig.cwd ?? dirname(rosterPath);
|
|
753
1151
|
const requestedPort = managedConfig.port ?? 0;
|
|
754
1152
|
const logPath = logFilePath(rosterPath);
|
|
755
|
-
const logFd =
|
|
1153
|
+
const logFd = openSync3(logPath, "w", 384);
|
|
756
1154
|
let child;
|
|
757
1155
|
let pid;
|
|
758
1156
|
let spawnError;
|
|
@@ -819,7 +1217,7 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
819
1217
|
}
|
|
820
1218
|
async function ensureServer(rosterPath, options = {}) {
|
|
821
1219
|
if (existsSync2(rosterPath)) {
|
|
822
|
-
rosterPath =
|
|
1220
|
+
rosterPath = realpathSync3(rosterPath);
|
|
823
1221
|
}
|
|
824
1222
|
const readinessBudgetMs = options.readinessBudgetMs ?? DEFAULT_READINESS_BUDGET_MS;
|
|
825
1223
|
const lockWaitBudgetMs = options.lockWaitBudgetMs ?? DEFAULT_LOCK_WAIT_BUDGET_MS;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @experimental
|
|
3
|
+
* Experimental — shapes may change in minor releases.
|
|
4
|
+
*
|
|
5
|
+
* The sixth bus tool: roster registry management (list/use/create/register/
|
|
6
|
+
* unregister/set-default/add-project/remove-project/update-project) behind
|
|
7
|
+
* ONE action-discriminated tool, mirroring the other five tools' shared
|
|
8
|
+
* factory/description-constant parity pattern. Node-only (imports
|
|
9
|
+
* registry.ts + roster-edit.ts), same lane as config.ts/server.ts.
|
|
10
|
+
*
|
|
11
|
+
* `use` is an MCP-only concept (R10): the plugin surface resolves ambient
|
|
12
|
+
* roster from `ctx.directory`, so there is no session to select a default
|
|
13
|
+
* for. The factory takes an optional `session` seam — when absent (the
|
|
14
|
+
* plugin surface), `use` returns an actionable error explaining why;
|
|
15
|
+
* when present (MCP, wired to an in-memory module-level variable — see
|
|
16
|
+
* mcp.ts), `use` mutates it after revalidating the name resolves.
|
|
17
|
+
*/
|
|
18
|
+
import { type ToolDefinition } from "@opencode-ai/plugin";
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
import { type ServerConfig } from "../config";
|
|
21
|
+
import { type ProjectPatch, type RosterProjectInput } from "../roster-edit";
|
|
22
|
+
export declare const BUS_REGISTRY_DESCRIPTION = "Manage the roster registry: list registered rosters, create/register/unregister rosters, set the default, select an active roster for this connector session (MCP only), and add/remove/update projects on a registered roster.";
|
|
23
|
+
/** Session seam (R10): ephemeral, connector-side "active roster" state.
|
|
24
|
+
* Only the MCP surface wires this (mcp.ts's module-level `activeRoster`
|
|
25
|
+
* variable) — the plugin surface omits it, so `use` fails actionably. */
|
|
26
|
+
export interface RegistrySession {
|
|
27
|
+
getActive(): string | undefined;
|
|
28
|
+
setActive(name: string): void;
|
|
29
|
+
clearActive(): void;
|
|
30
|
+
}
|
|
31
|
+
export type BusRegistryAction = "list" | "use" | "create" | "register" | "unregister" | "set-default" | "add-project" | "remove-project" | "update-project";
|
|
32
|
+
export type BusRegistryArgs = {
|
|
33
|
+
action: BusRegistryAction;
|
|
34
|
+
roster?: string;
|
|
35
|
+
name?: string;
|
|
36
|
+
path?: string;
|
|
37
|
+
server?: ServerConfig;
|
|
38
|
+
project?: RosterProjectInput;
|
|
39
|
+
projectName?: string;
|
|
40
|
+
patch?: ProjectPatch;
|
|
41
|
+
};
|
|
42
|
+
export declare const ACTION_SCHEMA: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
43
|
+
action: z.ZodLiteral<"list">;
|
|
44
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
45
|
+
action: z.ZodLiteral<"use">;
|
|
46
|
+
roster: z.ZodString;
|
|
47
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
48
|
+
action: z.ZodLiteral<"create">;
|
|
49
|
+
name: z.ZodString;
|
|
50
|
+
path: z.ZodString;
|
|
51
|
+
server: z.ZodOptional<z.ZodObject<{
|
|
52
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
53
|
+
managed: z.ZodOptional<z.ZodObject<{
|
|
54
|
+
command: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
55
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
56
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
57
|
+
}, z.core.$strip>>;
|
|
58
|
+
}, z.core.$strip>>;
|
|
59
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
60
|
+
action: z.ZodLiteral<"register">;
|
|
61
|
+
name: z.ZodString;
|
|
62
|
+
path: z.ZodString;
|
|
63
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
64
|
+
action: z.ZodLiteral<"unregister">;
|
|
65
|
+
name: z.ZodString;
|
|
66
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
67
|
+
action: z.ZodLiteral<"set-default">;
|
|
68
|
+
name: z.ZodString;
|
|
69
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
70
|
+
action: z.ZodLiteral<"add-project">;
|
|
71
|
+
roster: z.ZodString;
|
|
72
|
+
project: z.ZodObject<{
|
|
73
|
+
name: z.ZodString;
|
|
74
|
+
path: z.ZodString;
|
|
75
|
+
description: z.ZodString;
|
|
76
|
+
}, z.core.$strip>;
|
|
77
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
78
|
+
action: z.ZodLiteral<"remove-project">;
|
|
79
|
+
roster: z.ZodString;
|
|
80
|
+
projectName: z.ZodString;
|
|
81
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
82
|
+
action: z.ZodLiteral<"update-project">;
|
|
83
|
+
roster: z.ZodString;
|
|
84
|
+
projectName: z.ZodString;
|
|
85
|
+
patch: z.ZodObject<{
|
|
86
|
+
name: z.ZodOptional<z.ZodString>;
|
|
87
|
+
path: z.ZodOptional<z.ZodString>;
|
|
88
|
+
description: z.ZodOptional<z.ZodString>;
|
|
89
|
+
}, z.core.$strip>;
|
|
90
|
+
}, z.core.$strict>], "action">;
|
|
91
|
+
export type RegistryListEntry = {
|
|
92
|
+
name: string;
|
|
93
|
+
path: string;
|
|
94
|
+
default: boolean;
|
|
95
|
+
active: boolean;
|
|
96
|
+
};
|
|
97
|
+
/** Machine-readable list metadata, shared verbatim by both surfaces
|
|
98
|
+
* (plugin ToolResult.metadata and MCP structuredContent), same pattern as
|
|
99
|
+
* bus_task's dispatchMetadata. Only `list` gets structured output — every
|
|
100
|
+
* other action is a one-line mutation confirmation, not worth a shape. */
|
|
101
|
+
export declare function registryListMetadata(session?: RegistrySession): {
|
|
102
|
+
ok: true;
|
|
103
|
+
rosters: RegistryListEntry[];
|
|
104
|
+
} | {
|
|
105
|
+
ok: false;
|
|
106
|
+
error: string;
|
|
107
|
+
};
|
|
108
|
+
type ActionResult = {
|
|
109
|
+
text: string;
|
|
110
|
+
listMetadata?: RegistryListEntry[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Runs one bus_registry action and returns its formatted text result.
|
|
114
|
+
* Throws on any invalid input or `ok:false` underneath — every message
|
|
115
|
+
* names the action, per the plan's per-action-error contract. Shared by
|
|
116
|
+
* both the plugin tool's execute() and mcp.ts's registerTool handler so
|
|
117
|
+
* the two surfaces can't drift. Dispatch is a thin switch over
|
|
118
|
+
* per-action helper functions (kept small individually to stay under the
|
|
119
|
+
* lint complexity ceiling).
|
|
120
|
+
*/
|
|
121
|
+
export declare function runBusRegistryAction(rawArgs: BusRegistryArgs, session?: RegistrySession): Promise<ActionResult>;
|
|
122
|
+
export declare function makeBusRegistry(session?: RegistrySession): ToolDefinition;
|
|
123
|
+
export {};
|
package/dist/tools/shared.d.ts
CHANGED
|
@@ -1,2 +1,32 @@
|
|
|
1
1
|
import type { BusContext } from "../contract";
|
|
2
|
-
|
|
2
|
+
/** Result of context resolution, carrying the roster name/path needed for
|
|
3
|
+
* the result-echo header (formatRosterHeader in format.ts). */
|
|
4
|
+
export type ResolvedContext = {
|
|
5
|
+
context: BusContext;
|
|
6
|
+
/** Registry name, when resolution went through an explicit `roster` param. */
|
|
7
|
+
rosterName?: string;
|
|
8
|
+
/** The roster file path actually resolved (ambient or registry-resolved). */
|
|
9
|
+
rosterPath: string;
|
|
10
|
+
};
|
|
11
|
+
/** Injectable `ensureServer` seam — defaults to the real implementation.
|
|
12
|
+
* Exists so tests can prove the registry name is resolved exactly ONCE
|
|
13
|
+
* per call: a test can rebind the registry mid-call (inside this
|
|
14
|
+
* function) and assert the loaded context still matches the path
|
|
15
|
+
* resolved BEFORE the rebind, not a re-resolution after it. */
|
|
16
|
+
export declare function ensureAndLoadContext(directory?: string, rosterName?: string, ensure?: (rosterPath: string) => Promise<unknown>): Promise<ResolvedContext>;
|
|
17
|
+
/**
|
|
18
|
+
* Prepends the resolved-roster header to any post-resolution result text —
|
|
19
|
+
* success OR error. Centralizing this closes the gap where a bare `throw
|
|
20
|
+
* new Error(r.error)` (plugin adapters) or a bare `content:[{text: r.error}]`
|
|
21
|
+
* (MCP handlers) would otherwise omit the header on core `ok:false` error
|
|
22
|
+
* paths (Fix 1): once context resolution has succeeded, EVERY result must
|
|
23
|
+
* carry the header, since the header is the split-brain/confused-deputy
|
|
24
|
+
* mitigation — the caller needs to know which roster produced this error
|
|
25
|
+
* just as much as which roster produced a success. Pre-resolution errors
|
|
26
|
+
* (unknown roster name, missing spacebus.json, etc.) never reach this
|
|
27
|
+
* helper — there's no resolved roster to report yet.
|
|
28
|
+
*/
|
|
29
|
+
export declare function withRosterHeader(source: {
|
|
30
|
+
name?: string;
|
|
31
|
+
path: string;
|
|
32
|
+
}, text: string): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fro.bot/space-bus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
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",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"devDependencies": {
|
|
83
83
|
"@biomejs/biome": "2.5.2",
|
|
84
84
|
"@changesets/cli": "2.31.0",
|
|
85
|
-
"@opencode-ai/plugin": "1.17.
|
|
85
|
+
"@opencode-ai/plugin": "1.17.18",
|
|
86
86
|
"@types/bun": "1.3.14",
|
|
87
87
|
"typescript": "5.9.3"
|
|
88
88
|
}
|