@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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type RegistryFile } from "./contract";
|
|
2
|
+
export type Result = {
|
|
3
|
+
ok: true;
|
|
4
|
+
} | {
|
|
5
|
+
ok: false;
|
|
6
|
+
error: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* `$XDG_CONFIG_HOME/space-bus/rosters.json`, falling back to
|
|
10
|
+
* `~/.config/space-bus/rosters.json`. Config lane (durable, user-editable),
|
|
11
|
+
* distinct from the daemon-owned state lane (`~/.local/state/space-bus`,
|
|
12
|
+
* see discovery.ts's `stateDirFor`).
|
|
13
|
+
*/
|
|
14
|
+
export declare function registryPath(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Reads and validates the registry file. An absent file (ENOENT) is a
|
|
17
|
+
* valid, empty registry (not an error) — nothing has been registered yet.
|
|
18
|
+
* Every OTHER read failure (permission denied, target is a directory,
|
|
19
|
+
* etc.) is reported as `ok:false` rather than silently treated as empty —
|
|
20
|
+
* a masked read failure could otherwise cause a registration to appear to
|
|
21
|
+
* "lose" existing entries. Unparseable JSON or a schema-invalid document
|
|
22
|
+
* is likewise `ok:false` with an actionable message. After schema parse,
|
|
23
|
+
* every entry's `path` must be absolute — the schema itself stays
|
|
24
|
+
* platform-agnostic (just non-empty); absolute-path enforcement is a
|
|
25
|
+
* Node-boundary concern. This function never throws.
|
|
26
|
+
*/
|
|
27
|
+
export declare function readRegistry(): {
|
|
28
|
+
ok: true;
|
|
29
|
+
registry: RegistryFile;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
error: string;
|
|
33
|
+
};
|
|
34
|
+
export declare function registerRoster(name: string, rosterPath: string): Result;
|
|
35
|
+
/**
|
|
36
|
+
* Removes the named entry (and clears the default pointer if it pointed to
|
|
37
|
+
* that name). Never touches the roster file itself on disk.
|
|
38
|
+
*/
|
|
39
|
+
export declare function unregisterRoster(name: string): Result;
|
|
40
|
+
/** Sets the default roster by name; the name must already be registered. */
|
|
41
|
+
export declare function setDefaultRoster(name: string): Result;
|
|
42
|
+
/**
|
|
43
|
+
* Resolves a registered roster name to its canonical path, revalidating
|
|
44
|
+
* the target at resolution time rather than trusting the stored path
|
|
45
|
+
* blindly: the roster file must still exist, its final path component
|
|
46
|
+
* must not be a symlink, and it must be a regular file — then it is
|
|
47
|
+
* realpathSync'd fresh (not just returned from storage) so a moved/
|
|
48
|
+
* replaced target is caught rather than silently trusted. On a name miss,
|
|
49
|
+
* the error lists all known names.
|
|
50
|
+
*/
|
|
51
|
+
export declare function resolveRosterByName(name: string): {
|
|
52
|
+
ok: true;
|
|
53
|
+
path: string;
|
|
54
|
+
name: string;
|
|
55
|
+
} | {
|
|
56
|
+
ok: false;
|
|
57
|
+
error: string;
|
|
58
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type ServerConfig } from "./config";
|
|
2
|
+
export type Result = {
|
|
3
|
+
ok: true;
|
|
4
|
+
} | {
|
|
5
|
+
ok: false;
|
|
6
|
+
error: string;
|
|
7
|
+
fileCreated?: boolean;
|
|
8
|
+
};
|
|
9
|
+
export interface RosterProjectInput {
|
|
10
|
+
name: string;
|
|
11
|
+
path: string;
|
|
12
|
+
description: string;
|
|
13
|
+
}
|
|
14
|
+
export type ProjectPatch = Partial<RosterProjectInput>;
|
|
15
|
+
export interface CreateRosterOpts {
|
|
16
|
+
name: string;
|
|
17
|
+
rosterPath: string;
|
|
18
|
+
server?: ServerConfig;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Writes a schema-valid spacebus.json at `rosterPath` (default server mode
|
|
22
|
+
* `{ managed: {} }`; empty projects array unless `opts.server` overrides
|
|
23
|
+
* the server block) and registers it under `opts.name` via
|
|
24
|
+
* `registry.registerRoster`.
|
|
25
|
+
*
|
|
26
|
+
* Preflight (before any write): name charset validity, registry
|
|
27
|
+
* readability, and no name collision — every preflight failure returns
|
|
28
|
+
* `ok:false` with NO file created. The write itself is an exclusive
|
|
29
|
+
* create (`writeRosterExclusive`) so two concurrent `createRoster` calls
|
|
30
|
+
* targeting the same path can't silently clobber each other — exactly one
|
|
31
|
+
* wins.
|
|
32
|
+
*
|
|
33
|
+
* If the file write succeeds but registration fails (a rare race: the
|
|
34
|
+
* registry write lost a concurrent contention window or is corrupt), the
|
|
35
|
+
* file is NOT deleted — the returned error names both facts and sets
|
|
36
|
+
* `fileCreated: true` so a caller can recover programmatically (retrying
|
|
37
|
+
* `registerRoster` directly is the recovery op).
|
|
38
|
+
*/
|
|
39
|
+
export declare function createRoster(opts: CreateRosterOpts): Result;
|
|
40
|
+
/** Adds a project. Duplicate `project.name` (exact match) is rejected. */
|
|
41
|
+
export declare function addProject(rosterPath: string, project: RosterProjectInput): Result;
|
|
42
|
+
/** Removes a project by name. Unknown name → ok:false listing known
|
|
43
|
+
* project names (an empty resulting `projects` array is still valid). */
|
|
44
|
+
export declare function removeProject(rosterPath: string, projectName: string): Result;
|
|
45
|
+
/** Patches an existing project's fields. Unknown name → ok:false listing
|
|
46
|
+
* known project names. A `patch.name` that collides with another existing
|
|
47
|
+
* project (exact match, mirroring addProject) is rejected without
|
|
48
|
+
* mutating the file. */
|
|
49
|
+
export declare function updateProject(rosterPath: string, projectName: string, patch: ProjectPatch): Result;
|
|
50
|
+
/**
|
|
51
|
+
* Replaces the roster's `server` block wholesale (`baseUrl` XOR `managed`,
|
|
52
|
+
* enforced by re-validation). A non-loopback `baseUrl` is rejected and the
|
|
53
|
+
* file is left byte-identical (AE4).
|
|
54
|
+
*
|
|
55
|
+
* Live-daemon guard: if a managed daemon is currently live for this
|
|
56
|
+
* roster (per `discovery.ts`'s `attachLive`), server-block edits are
|
|
57
|
+
* rejected — changing `server` out from under a running daemon would
|
|
58
|
+
* leave the on-disk roster and the live discovery record pointing at
|
|
59
|
+
* different worlds (split-brain). Project edits are unaffected; only
|
|
60
|
+
* `editServer` carries this guard.
|
|
61
|
+
*/
|
|
62
|
+
export declare function editServer(rosterPath: string, server: ServerConfig): Result;
|
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
|
|
@@ -127,13 +127,44 @@ var managedSpawnConfigSchema = z.object({
|
|
|
127
127
|
cwd: z.string().optional(),
|
|
128
128
|
port: z.number().int().nonnegative().optional()
|
|
129
129
|
});
|
|
130
|
+
var registryEntrySchema = z.object({
|
|
131
|
+
name: z.string().regex(/^[a-z0-9-]{1,64}$/, "roster name must be 1-64 lowercase letters, digits, or hyphens"),
|
|
132
|
+
path: z.string().min(1, "roster path must not be empty")
|
|
133
|
+
});
|
|
134
|
+
var registryFileSchema = z.object({
|
|
135
|
+
version: z.literal(1),
|
|
136
|
+
default: z.string().optional(),
|
|
137
|
+
rosters: z.array(registryEntrySchema)
|
|
138
|
+
}).superRefine((val, ctx) => {
|
|
139
|
+
const seen = new Set;
|
|
140
|
+
for (const entry of val.rosters) {
|
|
141
|
+
const key = entry.name.toLowerCase();
|
|
142
|
+
if (seen.has(key)) {
|
|
143
|
+
ctx.addIssue({
|
|
144
|
+
code: "custom",
|
|
145
|
+
message: `space-bus: duplicate roster name "${entry.name}" (case-insensitive) in registry`
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
seen.add(key);
|
|
149
|
+
}
|
|
150
|
+
if (val.default !== undefined) {
|
|
151
|
+
const matches = val.rosters.filter((entry) => entry.name === val.default);
|
|
152
|
+
if (matches.length !== 1) {
|
|
153
|
+
ctx.addIssue({
|
|
154
|
+
code: "custom",
|
|
155
|
+
message: `space-bus: registry's default "${val.default}" must match exactly one roster entry's name (matched ${matches.length})`
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
130
160
|
var discoveryFileSchema = z.object({
|
|
131
161
|
port: z.number().int().nonnegative(),
|
|
132
162
|
pid: z.number().int().min(2),
|
|
133
163
|
identity: z.string(),
|
|
134
164
|
password: z.string(),
|
|
135
165
|
spawnConfig: managedSpawnConfigSchema,
|
|
136
|
-
baseUrl: z.url()
|
|
166
|
+
baseUrl: z.url(),
|
|
167
|
+
rosterPath: z.string().optional()
|
|
137
168
|
});
|
|
138
169
|
|
|
139
170
|
// src/discovery.ts
|
|
@@ -402,6 +433,380 @@ function removeProvisional(rosterPath) {
|
|
|
402
433
|
} catch {}
|
|
403
434
|
}
|
|
404
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
|
+
|
|
405
810
|
// src/config.ts
|
|
406
811
|
var managedServerConfigSchema = z3.object({
|
|
407
812
|
command: z3.array(z3.string()).optional(),
|
|
@@ -435,7 +840,7 @@ var manifestSchema = z3.object({
|
|
|
435
840
|
}))
|
|
436
841
|
});
|
|
437
842
|
function expandHome(path) {
|
|
438
|
-
return path.startsWith("~") ?
|
|
843
|
+
return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
|
|
439
844
|
}
|
|
440
845
|
function resolveRosterPath(directory) {
|
|
441
846
|
const override = process.env["SPACE_BUS_CONFIG"];
|
|
@@ -447,19 +852,18 @@ function resolveRosterPath(directory) {
|
|
|
447
852
|
if (!expanded.startsWith("/")) {
|
|
448
853
|
throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
|
|
449
854
|
}
|
|
450
|
-
return existsSync(expanded) ?
|
|
855
|
+
return existsSync(expanded) ? realpathSync2(expanded) : expanded;
|
|
451
856
|
}
|
|
452
857
|
if (directory) {
|
|
453
|
-
const candidate =
|
|
454
|
-
return existsSync(candidate) ?
|
|
858
|
+
const candidate = join3(directory, "spacebus.json");
|
|
859
|
+
return existsSync(candidate) ? realpathSync2(candidate) : candidate;
|
|
455
860
|
}
|
|
456
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");
|
|
457
862
|
}
|
|
458
|
-
function
|
|
459
|
-
const manifestPath = resolveRosterPath(directory);
|
|
863
|
+
function getRosterAtPath(manifestPath) {
|
|
460
864
|
let raw;
|
|
461
865
|
try {
|
|
462
|
-
raw =
|
|
866
|
+
raw = readFileSync3(manifestPath, "utf8");
|
|
463
867
|
} catch (err) {
|
|
464
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)`);
|
|
465
869
|
}
|
|
@@ -482,6 +886,10 @@ function getRoster(directory) {
|
|
|
482
886
|
}
|
|
483
887
|
return parsed.data;
|
|
484
888
|
}
|
|
889
|
+
function getRoster(directory) {
|
|
890
|
+
const manifestPath = resolveRosterPath(directory);
|
|
891
|
+
return getRosterAtPath(manifestPath);
|
|
892
|
+
}
|
|
485
893
|
function getProjects(manifest) {
|
|
486
894
|
return manifest.projects.map((p) => {
|
|
487
895
|
const expandedPath = expandHome(p.path);
|
|
@@ -495,9 +903,8 @@ function getCredentials() {
|
|
|
495
903
|
const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
|
|
496
904
|
return { username, password };
|
|
497
905
|
}
|
|
498
|
-
function
|
|
499
|
-
const
|
|
500
|
-
const manifest = getRoster(directory);
|
|
906
|
+
function loadContextAtPath(rosterPath) {
|
|
907
|
+
const manifest = getRosterAtPath(rosterPath);
|
|
501
908
|
const projects = getProjects(manifest);
|
|
502
909
|
if (manifest.server.baseUrl === undefined) {
|
|
503
910
|
const live = attachLive(rosterPath);
|
|
@@ -514,6 +921,28 @@ function loadContext(directory) {
|
|
|
514
921
|
credentials: getCredentials()
|
|
515
922
|
};
|
|
516
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
|
+
}
|
|
517
946
|
function isManagedRoster(directory) {
|
|
518
947
|
const manifest = getRoster(directory);
|
|
519
948
|
return manifest.server.managed !== undefined;
|
|
@@ -521,8 +950,8 @@ function isManagedRoster(directory) {
|
|
|
521
950
|
|
|
522
951
|
// src/server.ts
|
|
523
952
|
import { spawn } from "node:child_process";
|
|
524
|
-
import { randomBytes as
|
|
525
|
-
import { 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";
|
|
526
955
|
import { dirname } from "node:path";
|
|
527
956
|
var DEFAULT_READINESS_BUDGET_MS = 15000;
|
|
528
957
|
var DEFAULT_LOCK_WAIT_BUDGET_MS = 15000;
|
|
@@ -537,13 +966,13 @@ function sleep(ms) {
|
|
|
537
966
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
538
967
|
}
|
|
539
968
|
function readManifest(rosterPath) {
|
|
540
|
-
const raw =
|
|
969
|
+
const raw = readFileSync4(rosterPath, "utf8");
|
|
541
970
|
const json = JSON.parse(raw);
|
|
542
971
|
return manifestSchema.parse(json);
|
|
543
972
|
}
|
|
544
973
|
function readLogTail(logPath, maxChars = 4000) {
|
|
545
974
|
try {
|
|
546
|
-
const content =
|
|
975
|
+
const content = readFileSync4(logPath, "utf8");
|
|
547
976
|
return content.length > maxChars ? content.slice(-maxChars) : content;
|
|
548
977
|
} catch {
|
|
549
978
|
return "";
|
|
@@ -716,12 +1145,12 @@ function reapOrphanedProvisional(rosterPath) {
|
|
|
716
1145
|
}
|
|
717
1146
|
async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
718
1147
|
reapOrphanedProvisional(rosterPath);
|
|
719
|
-
const password =
|
|
1148
|
+
const password = randomBytes3(32).toString("base64url");
|
|
720
1149
|
const command = managedConfig.command && managedConfig.command.length > 0 ? managedConfig.command : ["harness", "serve"];
|
|
721
1150
|
const cwd = managedConfig.cwd ?? dirname(rosterPath);
|
|
722
1151
|
const requestedPort = managedConfig.port ?? 0;
|
|
723
1152
|
const logPath = logFilePath(rosterPath);
|
|
724
|
-
const logFd =
|
|
1153
|
+
const logFd = openSync3(logPath, "w", 384);
|
|
725
1154
|
let child;
|
|
726
1155
|
let pid;
|
|
727
1156
|
let spawnError;
|
|
@@ -776,7 +1205,8 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
776
1205
|
identity: finalIdentity,
|
|
777
1206
|
password,
|
|
778
1207
|
spawnConfig: { command, cwd, port: requestedPort },
|
|
779
|
-
baseUrl
|
|
1208
|
+
baseUrl,
|
|
1209
|
+
rosterPath
|
|
780
1210
|
});
|
|
781
1211
|
return {
|
|
782
1212
|
baseUrl,
|
|
@@ -786,6 +1216,9 @@ async function spawnAndWaitReady(rosterPath, managedConfig, readinessBudgetMs) {
|
|
|
786
1216
|
};
|
|
787
1217
|
}
|
|
788
1218
|
async function ensureServer(rosterPath, options = {}) {
|
|
1219
|
+
if (existsSync2(rosterPath)) {
|
|
1220
|
+
rosterPath = realpathSync3(rosterPath);
|
|
1221
|
+
}
|
|
789
1222
|
const readinessBudgetMs = options.readinessBudgetMs ?? DEFAULT_READINESS_BUDGET_MS;
|
|
790
1223
|
const lockWaitBudgetMs = options.lockWaitBudgetMs ?? DEFAULT_LOCK_WAIT_BUDGET_MS;
|
|
791
1224
|
const fastPath = attachServer(rosterPath);
|