@fro.bot/space-bus 0.11.0 → 0.13.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 +7 -1
- package/dist/attach.js +32 -1
- package/dist/cli.js +496 -63
- package/dist/config.d.ts +53 -0
- package/dist/config.js +447 -14
- package/dist/contract.d.ts +15 -0
- package/dist/contract.js +34 -1
- package/dist/core.js +32 -1
- package/dist/format.d.ts +13 -0
- package/dist/format.js +4 -0
- package/dist/index.js +1224 -183
- package/dist/mcp.js +1262 -114
- package/dist/registry-entry.d.ts +21 -0
- package/dist/registry-entry.js +1242 -0
- package/dist/registry.d.ts +58 -0
- package/dist/roster-edit.d.ts +62 -0
- package/dist/server.js +454 -21
- package/dist/tools/bus_registry.d.ts +123 -0
- package/dist/tools/shared.d.ts +31 -1
- package/package.json +6 -2
package/dist/cli.js
CHANGED
|
@@ -3,9 +3,9 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
4
|
|
|
5
5
|
// src/config.ts
|
|
6
|
-
import { existsSync, readFileSync as
|
|
7
|
-
import { homedir as
|
|
8
|
-
import { join as
|
|
6
|
+
import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
|
|
7
|
+
import { homedir as homedir3 } from "node:os";
|
|
8
|
+
import { join as join3 } from "node:path";
|
|
9
9
|
import { z as z3 } from "zod";
|
|
10
10
|
|
|
11
11
|
// src/contract.ts
|
|
@@ -128,13 +128,44 @@ var managedSpawnConfigSchema = z.object({
|
|
|
128
128
|
cwd: z.string().optional(),
|
|
129
129
|
port: z.number().int().nonnegative().optional()
|
|
130
130
|
});
|
|
131
|
+
var registryEntrySchema = z.object({
|
|
132
|
+
name: z.string().regex(/^[a-z0-9-]{1,64}$/, "roster name must be 1-64 lowercase letters, digits, or hyphens"),
|
|
133
|
+
path: z.string().min(1, "roster path must not be empty")
|
|
134
|
+
});
|
|
135
|
+
var registryFileSchema = z.object({
|
|
136
|
+
version: z.literal(1),
|
|
137
|
+
default: z.string().optional(),
|
|
138
|
+
rosters: z.array(registryEntrySchema)
|
|
139
|
+
}).superRefine((val, ctx) => {
|
|
140
|
+
const seen = new Set;
|
|
141
|
+
for (const entry of val.rosters) {
|
|
142
|
+
const key = entry.name.toLowerCase();
|
|
143
|
+
if (seen.has(key)) {
|
|
144
|
+
ctx.addIssue({
|
|
145
|
+
code: "custom",
|
|
146
|
+
message: `space-bus: duplicate roster name "${entry.name}" (case-insensitive) in registry`
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
seen.add(key);
|
|
150
|
+
}
|
|
151
|
+
if (val.default !== undefined) {
|
|
152
|
+
const matches = val.rosters.filter((entry) => entry.name === val.default);
|
|
153
|
+
if (matches.length !== 1) {
|
|
154
|
+
ctx.addIssue({
|
|
155
|
+
code: "custom",
|
|
156
|
+
message: `space-bus: registry's default "${val.default}" must match exactly one roster entry's name (matched ${matches.length})`
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
131
161
|
var discoveryFileSchema = z.object({
|
|
132
162
|
port: z.number().int().nonnegative(),
|
|
133
163
|
pid: z.number().int().min(2),
|
|
134
164
|
identity: z.string(),
|
|
135
165
|
password: z.string(),
|
|
136
166
|
spawnConfig: managedSpawnConfigSchema,
|
|
137
|
-
baseUrl: z.url()
|
|
167
|
+
baseUrl: z.url(),
|
|
168
|
+
rosterPath: z.string().optional()
|
|
138
169
|
});
|
|
139
170
|
|
|
140
171
|
// src/discovery.ts
|
|
@@ -403,6 +434,380 @@ function removeProvisional(rosterPath) {
|
|
|
403
434
|
} catch {}
|
|
404
435
|
}
|
|
405
436
|
|
|
437
|
+
// src/registry.ts
|
|
438
|
+
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
439
|
+
import {
|
|
440
|
+
closeSync as closeSync2,
|
|
441
|
+
constants as fsConstants2,
|
|
442
|
+
fstatSync,
|
|
443
|
+
lstatSync,
|
|
444
|
+
mkdirSync as mkdirSync2,
|
|
445
|
+
openSync as openSync2,
|
|
446
|
+
readFileSync as readFileSync2,
|
|
447
|
+
realpathSync,
|
|
448
|
+
renameSync as renameSync2,
|
|
449
|
+
statSync as statSync2,
|
|
450
|
+
unlinkSync as unlinkSync2,
|
|
451
|
+
writeSync
|
|
452
|
+
} from "node:fs";
|
|
453
|
+
import { homedir as homedir2 } from "node:os";
|
|
454
|
+
import { isAbsolute, join as join2 } from "node:path";
|
|
455
|
+
var EMPTY_REGISTRY = { version: 1, rosters: [] };
|
|
456
|
+
function registryPath() {
|
|
457
|
+
const base = process.env["XDG_CONFIG_HOME"] ?? join2(homedir2(), ".config");
|
|
458
|
+
return join2(base, "space-bus", "rosters.json");
|
|
459
|
+
}
|
|
460
|
+
function isEnoent2(err) {
|
|
461
|
+
return isErrnoCode2(err, "ENOENT");
|
|
462
|
+
}
|
|
463
|
+
function isErrnoCode2(err, code) {
|
|
464
|
+
return typeof err === "object" && err !== null && "code" in err && err.code === code;
|
|
465
|
+
}
|
|
466
|
+
function closeQuietly2(fd) {
|
|
467
|
+
try {
|
|
468
|
+
closeSync2(fd);
|
|
469
|
+
} catch {}
|
|
470
|
+
}
|
|
471
|
+
function readRegistry() {
|
|
472
|
+
const target = registryPath();
|
|
473
|
+
let raw;
|
|
474
|
+
try {
|
|
475
|
+
raw = readFileSync2(target, "utf8");
|
|
476
|
+
} catch (err) {
|
|
477
|
+
if (isEnoent2(err)) {
|
|
478
|
+
return { ok: true, registry: EMPTY_REGISTRY };
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
ok: false,
|
|
482
|
+
error: `space-bus: cannot read registry at ${target}: ${String(err)}`
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
let json;
|
|
486
|
+
try {
|
|
487
|
+
json = JSON.parse(raw);
|
|
488
|
+
} catch (err) {
|
|
489
|
+
return {
|
|
490
|
+
ok: false,
|
|
491
|
+
error: `space-bus: registry at ${target} is not valid JSON: ${String(err)}`
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
const parsed = registryFileSchema.safeParse(json);
|
|
495
|
+
if (!parsed.success) {
|
|
496
|
+
return {
|
|
497
|
+
ok: false,
|
|
498
|
+
error: `space-bus: registry at ${target} failed schema validation: ${parsed.error.message}`
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
const nonAbsolute = parsed.data.rosters.find((entry) => !isAbsolute(entry.path));
|
|
502
|
+
if (nonAbsolute) {
|
|
503
|
+
return {
|
|
504
|
+
ok: false,
|
|
505
|
+
error: `space-bus: registry at ${target} contains a non-absolute path for roster "${nonAbsolute.name}": ${nonAbsolute.path}`
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return { ok: true, registry: parsed.data };
|
|
509
|
+
}
|
|
510
|
+
function writeRegistryAtomic(data) {
|
|
511
|
+
const target = registryPath();
|
|
512
|
+
const dir = join2(target, "..");
|
|
513
|
+
try {
|
|
514
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
515
|
+
} catch (err) {
|
|
516
|
+
return {
|
|
517
|
+
ok: false,
|
|
518
|
+
error: `space-bus: failed to create registry dir ${dir}: ${String(err)}`
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
const tempPath = join2(dir, `.rosters.${randomBytes2(8).toString("hex")}.tmp`);
|
|
522
|
+
let fd;
|
|
523
|
+
try {
|
|
524
|
+
fd = openSync2(tempPath, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
525
|
+
writeSync(fd, JSON.stringify(data, null, 2));
|
|
526
|
+
closeSync2(fd);
|
|
527
|
+
fd = undefined;
|
|
528
|
+
renameSync2(tempPath, target);
|
|
529
|
+
} catch (err) {
|
|
530
|
+
if (fd !== undefined)
|
|
531
|
+
closeQuietly2(fd);
|
|
532
|
+
try {
|
|
533
|
+
unlinkSync2(tempPath);
|
|
534
|
+
} catch {}
|
|
535
|
+
return {
|
|
536
|
+
ok: false,
|
|
537
|
+
error: `space-bus: failed to write registry at ${target}: ${String(err)}`
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return { ok: true };
|
|
541
|
+
}
|
|
542
|
+
function registryLockPath() {
|
|
543
|
+
return join2(registryPath(), "..", "rosters.lock");
|
|
544
|
+
}
|
|
545
|
+
var LOCK_RETRY_COUNT = 10;
|
|
546
|
+
var LOCK_RETRY_DELAY_MS = 50;
|
|
547
|
+
var LOCK_STALE_MS = 1e4;
|
|
548
|
+
function sleepSync(ms) {
|
|
549
|
+
const buf = new Int32Array(new SharedArrayBuffer(4));
|
|
550
|
+
Atomics.wait(buf, 0, 0, ms);
|
|
551
|
+
}
|
|
552
|
+
function tryCreateRegistryLock(target) {
|
|
553
|
+
let fd;
|
|
554
|
+
try {
|
|
555
|
+
fd = openSync2(target, fsConstants2.O_CREAT | fsConstants2.O_EXCL | fsConstants2.O_WRONLY, 384);
|
|
556
|
+
} catch (err) {
|
|
557
|
+
if (isErrnoCode2(err, "EEXIST"))
|
|
558
|
+
return false;
|
|
559
|
+
throw err;
|
|
560
|
+
}
|
|
561
|
+
try {
|
|
562
|
+
writeSync(fd, String(process.pid));
|
|
563
|
+
} finally {
|
|
564
|
+
closeQuietly2(fd);
|
|
565
|
+
}
|
|
566
|
+
return true;
|
|
567
|
+
}
|
|
568
|
+
function isRegistryLockStale(target) {
|
|
569
|
+
try {
|
|
570
|
+
return Date.now() - statSync2(target).mtimeMs > LOCK_STALE_MS;
|
|
571
|
+
} catch {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function breakStaleAndRetry(target) {
|
|
576
|
+
if (!isRegistryLockStale(target))
|
|
577
|
+
return false;
|
|
578
|
+
try {
|
|
579
|
+
unlinkSync2(target);
|
|
580
|
+
} catch (err) {
|
|
581
|
+
if (!isEnoent2(err))
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
return tryCreateRegistryLock(target);
|
|
585
|
+
}
|
|
586
|
+
function acquireRegistryLock() {
|
|
587
|
+
const dir = join2(registryPath(), "..");
|
|
588
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
589
|
+
const target = registryLockPath();
|
|
590
|
+
for (let attempt = 0;attempt <= LOCK_RETRY_COUNT; attempt++) {
|
|
591
|
+
if (tryCreateRegistryLock(target))
|
|
592
|
+
return true;
|
|
593
|
+
if (breakStaleAndRetry(target))
|
|
594
|
+
return true;
|
|
595
|
+
if (attempt < LOCK_RETRY_COUNT)
|
|
596
|
+
sleepSync(LOCK_RETRY_DELAY_MS);
|
|
597
|
+
}
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
function releaseRegistryLock() {
|
|
601
|
+
try {
|
|
602
|
+
unlinkSync2(registryLockPath());
|
|
603
|
+
} catch {}
|
|
604
|
+
}
|
|
605
|
+
function withRegistryLock(mutate) {
|
|
606
|
+
if (!acquireRegistryLock()) {
|
|
607
|
+
return {
|
|
608
|
+
ok: false,
|
|
609
|
+
error: "space-bus: timed out waiting for the registry lock (another process is mutating rosters.json) — try again"
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
try {
|
|
613
|
+
return mutate();
|
|
614
|
+
} finally {
|
|
615
|
+
releaseRegistryLock();
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function openAndValidateRosterFile(rosterPath) {
|
|
619
|
+
let fd;
|
|
620
|
+
try {
|
|
621
|
+
fd = openSync2(rosterPath, fsConstants2.O_RDONLY | fsConstants2.O_NOFOLLOW);
|
|
622
|
+
} catch (err) {
|
|
623
|
+
if (isErrnoCode2(err, "ELOOP")) {
|
|
624
|
+
return {
|
|
625
|
+
ok: false,
|
|
626
|
+
error: `space-bus: roster path ${rosterPath} is a symlink — refusing to register a symlinked roster file`
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
if (isEnoent2(err)) {
|
|
630
|
+
return {
|
|
631
|
+
ok: false,
|
|
632
|
+
error: `space-bus: roster path ${rosterPath} does not exist: ${String(err)}`
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
ok: false,
|
|
637
|
+
error: `space-bus: failed to open roster path ${rosterPath}: ${String(err)}`
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
try {
|
|
641
|
+
let stat;
|
|
642
|
+
try {
|
|
643
|
+
stat = fstatSync(fd);
|
|
644
|
+
} catch (err) {
|
|
645
|
+
return {
|
|
646
|
+
ok: false,
|
|
647
|
+
error: `space-bus: failed to stat roster path ${rosterPath}: ${String(err)}`
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
if (!stat.isFile()) {
|
|
651
|
+
return {
|
|
652
|
+
ok: false,
|
|
653
|
+
error: `space-bus: roster path ${rosterPath} is not a regular file — refusing to register`
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
let raw;
|
|
657
|
+
try {
|
|
658
|
+
raw = readFileSync2(fd, "utf8");
|
|
659
|
+
} catch (err) {
|
|
660
|
+
return {
|
|
661
|
+
ok: false,
|
|
662
|
+
error: `space-bus: failed to read roster path ${rosterPath}: ${String(err)}`
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
let json;
|
|
666
|
+
try {
|
|
667
|
+
json = JSON.parse(raw);
|
|
668
|
+
} catch (err) {
|
|
669
|
+
return {
|
|
670
|
+
ok: false,
|
|
671
|
+
error: `space-bus: roster path ${rosterPath} is not valid JSON: ${String(err)}`
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const parsed = manifestSchema.safeParse(json);
|
|
675
|
+
if (!parsed.success) {
|
|
676
|
+
return {
|
|
677
|
+
ok: false,
|
|
678
|
+
error: `space-bus: roster path ${rosterPath} is not a valid spacebus.json — refusing to register: ${parsed.error.message}`
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
} finally {
|
|
682
|
+
closeQuietly2(fd);
|
|
683
|
+
}
|
|
684
|
+
return { ok: true };
|
|
685
|
+
}
|
|
686
|
+
function registerRoster(name, rosterPath) {
|
|
687
|
+
const nameCheck = registryEntrySchema.shape.name.safeParse(name);
|
|
688
|
+
if (!nameCheck.success) {
|
|
689
|
+
return {
|
|
690
|
+
ok: false,
|
|
691
|
+
error: `space-bus: invalid roster name "${name}": ${nameCheck.error.issues[0]?.message ?? "must be 1-64 lowercase letters, digits, or hyphens"}`
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
const validated = openAndValidateRosterFile(rosterPath);
|
|
695
|
+
if (!validated.ok)
|
|
696
|
+
return validated;
|
|
697
|
+
let canonicalPath;
|
|
698
|
+
try {
|
|
699
|
+
canonicalPath = realpathSync(rosterPath);
|
|
700
|
+
} catch (err) {
|
|
701
|
+
return {
|
|
702
|
+
ok: false,
|
|
703
|
+
error: `space-bus: failed to canonicalize roster path ${rosterPath}: ${String(err)}`
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
return withRegistryLock(() => {
|
|
707
|
+
const readResult = readRegistry();
|
|
708
|
+
if (!readResult.ok)
|
|
709
|
+
return readResult;
|
|
710
|
+
const registry = readResult.registry;
|
|
711
|
+
const collision = registry.rosters.find((entry2) => entry2.name.toLowerCase() === name.toLowerCase());
|
|
712
|
+
if (collision) {
|
|
713
|
+
return {
|
|
714
|
+
ok: false,
|
|
715
|
+
error: `space-bus: a roster named "${collision.name}" is already registered`
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
const entry = { name, path: canonicalPath };
|
|
719
|
+
const next = {
|
|
720
|
+
...registry,
|
|
721
|
+
rosters: [...registry.rosters, entry]
|
|
722
|
+
};
|
|
723
|
+
return writeRegistryAtomic(next);
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
function unregisterRoster(name) {
|
|
727
|
+
return withRegistryLock(() => {
|
|
728
|
+
const readResult = readRegistry();
|
|
729
|
+
if (!readResult.ok)
|
|
730
|
+
return readResult;
|
|
731
|
+
const registry = readResult.registry;
|
|
732
|
+
const exists = registry.rosters.some((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
733
|
+
if (!exists) {
|
|
734
|
+
return {
|
|
735
|
+
ok: false,
|
|
736
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const rosters = registry.rosters.filter((entry) => entry.name.toLowerCase() !== name.toLowerCase());
|
|
740
|
+
const next = {
|
|
741
|
+
...registry,
|
|
742
|
+
rosters,
|
|
743
|
+
default: registry.default !== undefined && registry.default.toLowerCase() === name.toLowerCase() ? undefined : registry.default
|
|
744
|
+
};
|
|
745
|
+
return writeRegistryAtomic(next);
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
function setDefaultRoster(name) {
|
|
749
|
+
return withRegistryLock(() => {
|
|
750
|
+
const readResult = readRegistry();
|
|
751
|
+
if (!readResult.ok)
|
|
752
|
+
return readResult;
|
|
753
|
+
const registry = readResult.registry;
|
|
754
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
755
|
+
if (!entry) {
|
|
756
|
+
return {
|
|
757
|
+
ok: false,
|
|
758
|
+
error: `space-bus: no roster named "${name}" is registered`
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
const next = { ...registry, default: entry.name };
|
|
762
|
+
return writeRegistryAtomic(next);
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
function resolveRosterByName(name) {
|
|
766
|
+
const readResult = readRegistry();
|
|
767
|
+
if (!readResult.ok)
|
|
768
|
+
return readResult;
|
|
769
|
+
const registry = readResult.registry;
|
|
770
|
+
const entry = registry.rosters.find((r) => r.name.toLowerCase() === name.toLowerCase());
|
|
771
|
+
if (!entry) {
|
|
772
|
+
const known = registry.rosters.map((r) => r.name);
|
|
773
|
+
return {
|
|
774
|
+
ok: false,
|
|
775
|
+
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)`
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
let stat;
|
|
779
|
+
try {
|
|
780
|
+
stat = lstatSync(entry.path);
|
|
781
|
+
} catch {
|
|
782
|
+
return {
|
|
783
|
+
ok: false,
|
|
784
|
+
error: `space-bus: registered roster file no longer exists at ${entry.path} (name "${entry.name}") — was it moved or deleted?`
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
if (stat.isSymbolicLink()) {
|
|
788
|
+
return {
|
|
789
|
+
ok: false,
|
|
790
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is now a symlink — refusing to resolve a symlinked roster file`
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
if (!stat.isFile()) {
|
|
794
|
+
return {
|
|
795
|
+
ok: false,
|
|
796
|
+
error: `space-bus: registered roster path ${entry.path} (name "${entry.name}") is no longer a regular file`
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
let canonicalPath;
|
|
800
|
+
try {
|
|
801
|
+
canonicalPath = realpathSync(entry.path);
|
|
802
|
+
} catch (err) {
|
|
803
|
+
return {
|
|
804
|
+
ok: false,
|
|
805
|
+
error: `space-bus: failed to canonicalize registered roster path ${entry.path} (name "${entry.name}"): ${String(err)}`
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
return { ok: true, path: canonicalPath, name: entry.name };
|
|
809
|
+
}
|
|
810
|
+
|
|
406
811
|
// src/config.ts
|
|
407
812
|
var managedServerConfigSchema = z3.object({
|
|
408
813
|
command: z3.array(z3.string()).optional(),
|
|
@@ -436,7 +841,7 @@ var manifestSchema = z3.object({
|
|
|
436
841
|
}))
|
|
437
842
|
});
|
|
438
843
|
function expandHome(path) {
|
|
439
|
-
return path.startsWith("~") ?
|
|
844
|
+
return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
|
|
440
845
|
}
|
|
441
846
|
function resolveRosterPath(directory) {
|
|
442
847
|
const override = process.env["SPACE_BUS_CONFIG"];
|
|
@@ -448,19 +853,18 @@ function resolveRosterPath(directory) {
|
|
|
448
853
|
if (!expanded.startsWith("/")) {
|
|
449
854
|
throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
|
|
450
855
|
}
|
|
451
|
-
return existsSync(expanded) ?
|
|
856
|
+
return existsSync(expanded) ? realpathSync2(expanded) : expanded;
|
|
452
857
|
}
|
|
453
858
|
if (directory) {
|
|
454
|
-
const candidate =
|
|
455
|
-
return existsSync(candidate) ?
|
|
859
|
+
const candidate = join3(directory, "spacebus.json");
|
|
860
|
+
return existsSync(candidate) ? realpathSync2(candidate) : candidate;
|
|
456
861
|
}
|
|
457
862
|
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");
|
|
458
863
|
}
|
|
459
|
-
function
|
|
460
|
-
const manifestPath = resolveRosterPath(directory);
|
|
864
|
+
function getRosterAtPath(manifestPath) {
|
|
461
865
|
let raw;
|
|
462
866
|
try {
|
|
463
|
-
raw =
|
|
867
|
+
raw = readFileSync3(manifestPath, "utf8");
|
|
464
868
|
} catch (err) {
|
|
465
869
|
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)`);
|
|
466
870
|
}
|
|
@@ -483,6 +887,10 @@ function getRoster(directory) {
|
|
|
483
887
|
}
|
|
484
888
|
return parsed.data;
|
|
485
889
|
}
|
|
890
|
+
function getRoster(directory) {
|
|
891
|
+
const manifestPath = resolveRosterPath(directory);
|
|
892
|
+
return getRosterAtPath(manifestPath);
|
|
893
|
+
}
|
|
486
894
|
function getProjects(manifest) {
|
|
487
895
|
return manifest.projects.map((p) => {
|
|
488
896
|
const expandedPath = expandHome(p.path);
|
|
@@ -496,9 +904,8 @@ function getCredentials() {
|
|
|
496
904
|
const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
|
|
497
905
|
return { username, password };
|
|
498
906
|
}
|
|
499
|
-
function
|
|
500
|
-
const
|
|
501
|
-
const manifest = getRoster(directory);
|
|
907
|
+
function loadContextAtPath(rosterPath) {
|
|
908
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
502
909
|
const projects = getProjects(manifest);
|
|
503
910
|
if (manifest.server.baseUrl === undefined) {
|
|
504
911
|
const live = attachLive(rosterPath);
|
|
@@ -515,6 +922,28 @@ function loadContext(directory) {
|
|
|
515
922
|
credentials: getCredentials()
|
|
516
923
|
};
|
|
517
924
|
}
|
|
925
|
+
function loadContext(directory) {
|
|
926
|
+
const rosterPath = resolveRosterPath(directory);
|
|
927
|
+
return loadContextAtPath(rosterPath);
|
|
928
|
+
}
|
|
929
|
+
function loadContextForRoster(rosterName) {
|
|
930
|
+
const resolved = resolveRosterByName(rosterName);
|
|
931
|
+
if (!resolved.ok) {
|
|
932
|
+
throw new Error(resolved.error);
|
|
933
|
+
}
|
|
934
|
+
return {
|
|
935
|
+
context: loadContextAtPath(resolved.path),
|
|
936
|
+
rosterPath: resolved.path,
|
|
937
|
+
rosterName: resolved.name
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
function loadContextForRosterPath(rosterPath) {
|
|
941
|
+
return loadContextAtPath(rosterPath);
|
|
942
|
+
}
|
|
943
|
+
function isManagedRosterAtPath(rosterPath) {
|
|
944
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
945
|
+
return manifest.server.managed !== undefined;
|
|
946
|
+
}
|
|
518
947
|
function isManagedRoster(directory) {
|
|
519
948
|
const manifest = getRoster(directory);
|
|
520
949
|
return manifest.server.managed !== undefined;
|
|
@@ -522,8 +951,8 @@ function isManagedRoster(directory) {
|
|
|
522
951
|
|
|
523
952
|
// src/server.ts
|
|
524
953
|
import { spawn } from "node:child_process";
|
|
525
|
-
import { randomBytes as
|
|
526
|
-
import { openSync as
|
|
954
|
+
import { randomBytes as randomBytes3 } from "node:crypto";
|
|
955
|
+
import { existsSync as existsSync2, openSync as openSync3, readFileSync as readFileSync4, realpathSync as realpathSync3 } from "node:fs";
|
|
527
956
|
import { dirname } from "node:path";
|
|
528
957
|
var DEFAULT_READINESS_BUDGET_MS = 15000;
|
|
529
958
|
var DEFAULT_LOCK_WAIT_BUDGET_MS = 15000;
|
|
@@ -538,13 +967,13 @@ function sleep(ms) {
|
|
|
538
967
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
539
968
|
}
|
|
540
969
|
function readManifest(rosterPath) {
|
|
541
|
-
const raw =
|
|
970
|
+
const raw = readFileSync4(rosterPath, "utf8");
|
|
542
971
|
const json = JSON.parse(raw);
|
|
543
972
|
return manifestSchema.parse(json);
|
|
544
973
|
}
|
|
545
974
|
function readLogTail(logPath, maxChars = 4000) {
|
|
546
975
|
try {
|
|
547
|
-
const content =
|
|
976
|
+
const content = readFileSync4(logPath, "utf8");
|
|
548
977
|
return content.length > maxChars ? content.slice(-maxChars) : content;
|
|
549
978
|
} catch {
|
|
550
979
|
return "";
|
|
@@ -717,12 +1146,12 @@ function reapOrphanedProvisional(rosterPath) {
|
|
|
717
1146
|
}
|
|
718
1147
|
async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
719
1148
|
reapOrphanedProvisional(rosterPath);
|
|
720
|
-
const password =
|
|
1149
|
+
const password = randomBytes3(32).toString("base64url");
|
|
721
1150
|
const command = managedConfig.command && managedConfig.command.length > 0 ? managedConfig.command : ["harness", "serve"];
|
|
722
1151
|
const cwd = managedConfig.cwd ?? dirname(rosterPath);
|
|
723
1152
|
const requestedPort = managedConfig.port ?? 0;
|
|
724
1153
|
const logPath = logFilePath(rosterPath);
|
|
725
|
-
const logFd =
|
|
1154
|
+
const logFd = openSync3(logPath, "w", 384);
|
|
726
1155
|
let child;
|
|
727
1156
|
let pid;
|
|
728
1157
|
let spawnError;
|
|
@@ -777,7 +1206,8 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
777
1206
|
identity: finalIdentity,
|
|
778
1207
|
password,
|
|
779
1208
|
spawnConfig: { command, cwd, port: requestedPort },
|
|
780
|
-
baseUrl
|
|
1209
|
+
baseUrl,
|
|
1210
|
+
rosterPath
|
|
781
1211
|
});
|
|
782
1212
|
return {
|
|
783
1213
|
baseUrl,
|
|
@@ -787,6 +1217,9 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
787
1217
|
};
|
|
788
1218
|
}
|
|
789
1219
|
async function ensureServer(rosterPath, options = {}) {
|
|
1220
|
+
if (existsSync2(rosterPath)) {
|
|
1221
|
+
rosterPath = realpathSync3(rosterPath);
|
|
1222
|
+
}
|
|
790
1223
|
const readinessBudgetMs = options.readinessBudgetMs ?? DEFAULT_READINESS_BUDGET_MS;
|
|
791
1224
|
const lockWaitBudgetMs = options.lockWaitBudgetMs ?? DEFAULT_LOCK_WAIT_BUDGET_MS;
|
|
792
1225
|
const fastPath = attachServer(rosterPath);
|
|
@@ -1009,37 +1442,37 @@ import { fileURLToPath } from "node:url";
|
|
|
1009
1442
|
// src/service.ts
|
|
1010
1443
|
import {
|
|
1011
1444
|
chmodSync as chmodSync3,
|
|
1012
|
-
closeSync as
|
|
1013
|
-
existsSync as
|
|
1445
|
+
closeSync as closeSync4,
|
|
1446
|
+
existsSync as existsSync3,
|
|
1014
1447
|
fchmodSync,
|
|
1015
|
-
constants as
|
|
1016
|
-
mkdirSync as
|
|
1017
|
-
openSync as
|
|
1018
|
-
unlinkSync as
|
|
1448
|
+
constants as fsConstants4,
|
|
1449
|
+
mkdirSync as mkdirSync3,
|
|
1450
|
+
openSync as openSync5,
|
|
1451
|
+
unlinkSync as unlinkSync3
|
|
1019
1452
|
} from "node:fs";
|
|
1020
|
-
import { homedir as
|
|
1021
|
-
import { isAbsolute, join as
|
|
1453
|
+
import { homedir as homedir5 } from "node:os";
|
|
1454
|
+
import { isAbsolute as isAbsolute2, join as join5 } from "node:path";
|
|
1022
1455
|
|
|
1023
1456
|
// src/launchd.ts
|
|
1024
1457
|
import { spawn as spawn2 } from "node:child_process";
|
|
1025
|
-
import { randomBytes as
|
|
1458
|
+
import { randomBytes as randomBytes4 } from "node:crypto";
|
|
1026
1459
|
import {
|
|
1027
1460
|
chmodSync as chmodSync2,
|
|
1028
|
-
closeSync as
|
|
1029
|
-
constants as
|
|
1030
|
-
lstatSync,
|
|
1031
|
-
openSync as
|
|
1032
|
-
renameSync as
|
|
1033
|
-
statSync as
|
|
1461
|
+
closeSync as closeSync3,
|
|
1462
|
+
constants as fsConstants3,
|
|
1463
|
+
lstatSync as lstatSync2,
|
|
1464
|
+
openSync as openSync4,
|
|
1465
|
+
renameSync as renameSync3,
|
|
1466
|
+
statSync as statSync3,
|
|
1034
1467
|
writeFileSync as writeFileSync2
|
|
1035
1468
|
} from "node:fs";
|
|
1036
|
-
import { homedir as
|
|
1037
|
-
import { join as
|
|
1469
|
+
import { homedir as homedir4 } from "node:os";
|
|
1470
|
+
import { join as join4 } from "node:path";
|
|
1038
1471
|
function serviceLabel(rosterPath) {
|
|
1039
1472
|
return `bot.fro.space-bus.${rosterKey(rosterPath)}`;
|
|
1040
1473
|
}
|
|
1041
1474
|
function plistPath(label, baseDir) {
|
|
1042
|
-
return
|
|
1475
|
+
return join4(baseDir ?? join4(homedir4(), "Library", "LaunchAgents"), `${label}.plist`);
|
|
1043
1476
|
}
|
|
1044
1477
|
function xmlEscape(value) {
|
|
1045
1478
|
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
@@ -1049,8 +1482,8 @@ function renderPlist(opts) {
|
|
|
1049
1482
|
const cliEntry = xmlEscape(opts.cliEntry);
|
|
1050
1483
|
const rosterPath = xmlEscape(opts.rosterPath);
|
|
1051
1484
|
const label = xmlEscape(opts.label);
|
|
1052
|
-
const outLog = xmlEscape(
|
|
1053
|
-
const errLog = xmlEscape(
|
|
1485
|
+
const outLog = xmlEscape(join4(opts.stateDir, "service.log"));
|
|
1486
|
+
const errLog = xmlEscape(join4(opts.stateDir, "service.err.log"));
|
|
1054
1487
|
const envEntries = [
|
|
1055
1488
|
` <key>SPACE_BUS_CONFIG</key>
|
|
1056
1489
|
<string>${rosterPath}</string>`
|
|
@@ -1099,24 +1532,24 @@ ${envEntries.join(`
|
|
|
1099
1532
|
`;
|
|
1100
1533
|
}
|
|
1101
1534
|
function writePlistAtomic(path, content) {
|
|
1102
|
-
const dir =
|
|
1103
|
-
const tempPath =
|
|
1535
|
+
const dir = join4(path, "..");
|
|
1536
|
+
const tempPath = join4(dir, `.${randomBytes4(8).toString("hex")}.tmp`);
|
|
1104
1537
|
let fd;
|
|
1105
1538
|
try {
|
|
1106
|
-
fd =
|
|
1539
|
+
fd = openSync4(tempPath, fsConstants3.O_EXCL | fsConstants3.O_CREAT | fsConstants3.O_WRONLY, 420);
|
|
1107
1540
|
} catch (err) {
|
|
1108
1541
|
return { ok: false, error: `failed to create temp plist: ${String(err)}` };
|
|
1109
1542
|
}
|
|
1110
1543
|
try {
|
|
1111
1544
|
writeFileSync2(fd, content);
|
|
1112
1545
|
} catch (err) {
|
|
1113
|
-
|
|
1546
|
+
closeQuietly3(fd);
|
|
1114
1547
|
return { ok: false, error: `failed to write temp plist: ${String(err)}` };
|
|
1115
1548
|
}
|
|
1116
|
-
|
|
1549
|
+
closeQuietly3(fd);
|
|
1117
1550
|
try {
|
|
1118
1551
|
chmodSync2(tempPath, 420);
|
|
1119
|
-
|
|
1552
|
+
renameSync3(tempPath, path);
|
|
1120
1553
|
} catch (err) {
|
|
1121
1554
|
return { ok: false, error: `failed to install plist: ${String(err)}` };
|
|
1122
1555
|
}
|
|
@@ -1126,7 +1559,7 @@ function verifyPlistSafe(path) {
|
|
|
1126
1559
|
let mode;
|
|
1127
1560
|
let uid;
|
|
1128
1561
|
try {
|
|
1129
|
-
const linkStat =
|
|
1562
|
+
const linkStat = lstatSync2(path);
|
|
1130
1563
|
if (linkStat.isSymbolicLink()) {
|
|
1131
1564
|
return { safe: false, reason: "plist path is a symlink" };
|
|
1132
1565
|
}
|
|
@@ -1134,7 +1567,7 @@ function verifyPlistSafe(path) {
|
|
|
1134
1567
|
return { safe: false, reason: `failed to stat plist: ${String(err)}` };
|
|
1135
1568
|
}
|
|
1136
1569
|
try {
|
|
1137
|
-
const stat =
|
|
1570
|
+
const stat = statSync3(path);
|
|
1138
1571
|
mode = stat.mode & 511;
|
|
1139
1572
|
uid = stat.uid;
|
|
1140
1573
|
} catch (err) {
|
|
@@ -1154,9 +1587,9 @@ function verifyPlistSafe(path) {
|
|
|
1154
1587
|
}
|
|
1155
1588
|
return { safe: true };
|
|
1156
1589
|
}
|
|
1157
|
-
function
|
|
1590
|
+
function closeQuietly3(fd) {
|
|
1158
1591
|
try {
|
|
1159
|
-
|
|
1592
|
+
closeSync3(fd);
|
|
1160
1593
|
} catch {}
|
|
1161
1594
|
}
|
|
1162
1595
|
var LAUNCHCTL_TIMEOUT_MS = 15000;
|
|
@@ -1269,7 +1702,7 @@ function resolveDeps(deps) {
|
|
|
1269
1702
|
now: deps.now ?? Date.now,
|
|
1270
1703
|
verifyBudgetMs: deps.verifyBudgetMs ?? DEFAULT_VERIFY_BUDGET_MS,
|
|
1271
1704
|
verifyPollMs: deps.verifyPollMs ?? DEFAULT_VERIFY_POLL_MS,
|
|
1272
|
-
launchAgentsDir: deps.launchAgentsDir ??
|
|
1705
|
+
launchAgentsDir: deps.launchAgentsDir ?? join5(homedir5(), "Library", "LaunchAgents")
|
|
1273
1706
|
};
|
|
1274
1707
|
}
|
|
1275
1708
|
function isEphemeralCachePath(path) {
|
|
@@ -1296,7 +1729,7 @@ function identityFor(rosterPath, launchAgentsDir) {
|
|
|
1296
1729
|
function preCreateLog(path) {
|
|
1297
1730
|
let fd;
|
|
1298
1731
|
try {
|
|
1299
|
-
fd =
|
|
1732
|
+
fd = openSync5(path, fsConstants4.O_WRONLY | fsConstants4.O_APPEND | fsConstants4.O_CREAT | fsConstants4.O_NOFOLLOW, 384);
|
|
1300
1733
|
} catch (err) {
|
|
1301
1734
|
if (isSymlinkError(err)) {
|
|
1302
1735
|
return { ok: false, error: `refusing symlinked log path: ${path}` };
|
|
@@ -1308,7 +1741,7 @@ function preCreateLog(path) {
|
|
|
1308
1741
|
} catch (err) {
|
|
1309
1742
|
return { ok: false, error: `failed to chmod log ${path}: ${String(err)}` };
|
|
1310
1743
|
} finally {
|
|
1311
|
-
|
|
1744
|
+
closeSync4(fd);
|
|
1312
1745
|
}
|
|
1313
1746
|
return { ok: true };
|
|
1314
1747
|
}
|
|
@@ -1317,7 +1750,7 @@ function isSymlinkError(err) {
|
|
|
1317
1750
|
}
|
|
1318
1751
|
function ensureStateDir(stateDir) {
|
|
1319
1752
|
try {
|
|
1320
|
-
|
|
1753
|
+
mkdirSync3(stateDir, { recursive: true, mode: 448 });
|
|
1321
1754
|
} catch (err) {
|
|
1322
1755
|
return {
|
|
1323
1756
|
ok: false,
|
|
@@ -1345,7 +1778,7 @@ function resolveXdgStateHome() {
|
|
|
1345
1778
|
const xdgStateHomeEnv = process.env["XDG_STATE_HOME"];
|
|
1346
1779
|
if (xdgStateHomeEnv === undefined)
|
|
1347
1780
|
return { ok: true, value: undefined };
|
|
1348
|
-
if (!
|
|
1781
|
+
if (!isAbsolute2(xdgStateHomeEnv)) {
|
|
1349
1782
|
return {
|
|
1350
1783
|
ok: false,
|
|
1351
1784
|
error: `XDG_STATE_HOME must be an absolute path to pin into the launchd environment, got: ${xdgStateHomeEnv}`
|
|
@@ -1384,7 +1817,7 @@ async function bootstrapWithExit5Retry(resolved, label, plistFilePath) {
|
|
|
1384
1817
|
}
|
|
1385
1818
|
function prepareInstallFilesystem(resolved, stateDir) {
|
|
1386
1819
|
try {
|
|
1387
|
-
|
|
1820
|
+
mkdirSync3(resolved.launchAgentsDir, { recursive: true, mode: 493 });
|
|
1388
1821
|
} catch (err) {
|
|
1389
1822
|
return {
|
|
1390
1823
|
ok: false,
|
|
@@ -1513,7 +1946,7 @@ async function verifyRunning(rosterPath, label, resolved, plistFilePath) {
|
|
|
1513
1946
|
await resolved.sleep(resolved.verifyPollMs);
|
|
1514
1947
|
}
|
|
1515
1948
|
const stateDir = stateDirFor(rosterPath);
|
|
1516
|
-
const logPath =
|
|
1949
|
+
const logPath = join5(stateDir, "service.log");
|
|
1517
1950
|
return {
|
|
1518
1951
|
ok: false,
|
|
1519
1952
|
error: `install did not verify as running within ${resolved.verifyBudgetMs}ms ` + `(loaded=${lastLoaded}, running=${lastRunning}` + `${lastProbeError ? `, last probe error: ${lastProbeError}` : ""})` + ` — label=${label}, plist=${plistFilePath ?? plistPath(label, resolved.launchAgentsDir)}, log=${logPath}`
|
|
@@ -1538,10 +1971,10 @@ async function uninstallService(rosterPath, deps = {}) {
|
|
|
1538
1971
|
}
|
|
1539
1972
|
let plistRemoved = false;
|
|
1540
1973
|
try {
|
|
1541
|
-
|
|
1974
|
+
unlinkSync3(plistFilePath);
|
|
1542
1975
|
plistRemoved = true;
|
|
1543
1976
|
} catch (err) {
|
|
1544
|
-
if (!
|
|
1977
|
+
if (!isEnoent3(err)) {
|
|
1545
1978
|
return {
|
|
1546
1979
|
ok: false,
|
|
1547
1980
|
error: `failed to remove plist ${plistFilePath}: ${String(err)}`
|
|
@@ -1554,7 +1987,7 @@ async function uninstallService(rosterPath, deps = {}) {
|
|
|
1554
1987
|
label
|
|
1555
1988
|
};
|
|
1556
1989
|
}
|
|
1557
|
-
function
|
|
1990
|
+
function isEnoent3(err) {
|
|
1558
1991
|
return typeof err === "object" && err !== null && "code" in err && err.code === "ENOENT";
|
|
1559
1992
|
}
|
|
1560
1993
|
async function serviceStatus(rosterPath, deps = {}) {
|
|
@@ -1563,7 +1996,7 @@ async function serviceStatus(rosterPath, deps = {}) {
|
|
|
1563
1996
|
if (!gate.ok)
|
|
1564
1997
|
return { ok: false, error: gate.error };
|
|
1565
1998
|
const { label, plistFilePath } = identityFor(rosterPath, resolved.launchAgentsDir);
|
|
1566
|
-
const installed =
|
|
1999
|
+
const installed = existsSync3(plistFilePath);
|
|
1567
2000
|
const job = await probeJob(resolved, label);
|
|
1568
2001
|
if (!job.ok) {
|
|
1569
2002
|
return { ok: false, error: `failed to probe job: ${job.error}` };
|
|
@@ -1603,7 +2036,7 @@ async function startService(rosterPath, deps = {}) {
|
|
|
1603
2036
|
if (!gate.ok)
|
|
1604
2037
|
return { ok: false, error: gate.error };
|
|
1605
2038
|
const { label, plistFilePath, stateDir } = identityFor(rosterPath, resolved.launchAgentsDir);
|
|
1606
|
-
if (!
|
|
2039
|
+
if (!existsSync3(plistFilePath)) {
|
|
1607
2040
|
return {
|
|
1608
2041
|
ok: false,
|
|
1609
2042
|
error: `space-bus service is not installed for this roster — run \`space-bus service install\``
|