@fro.bot/space-bus 0.6.1 → 0.7.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.
@@ -0,0 +1,30 @@
1
+ type Ok<T> = {
2
+ ok: true;
3
+ } & T;
4
+ type Err = {
5
+ ok: false;
6
+ error: string;
7
+ };
8
+ export type Result<T> = Ok<T> | Err;
9
+ export interface AttachSeams {
10
+ /** Canonicalize an absolute path (realpath); null if it doesn't exist. */
11
+ realpath(path: string): Promise<string | null>;
12
+ /** Read a file as utf8; null if absent/unreadable. */
13
+ readTextFile(path: string): Promise<string | null>;
14
+ /** Read an env var; null if unset/empty. */
15
+ env(name: string): Promise<string | null>;
16
+ /** Absolute home dir (for the ~/.local/state default). */
17
+ homeDir(): Promise<string>;
18
+ }
19
+ export interface ResolvedManagedServer {
20
+ baseUrl: string;
21
+ credentials: {
22
+ username: "opencode";
23
+ password: string;
24
+ };
25
+ alive: true;
26
+ }
27
+ /** Pure posix path join — avoids node:path in a browser-safe module. */
28
+ export declare function posixJoin(...parts: string[]): string;
29
+ export declare function resolveManagedServer(workspaceDir: string, seams: AttachSeams): Promise<Result<ResolvedManagedServer>>;
30
+ export {};
package/dist/attach.js ADDED
@@ -0,0 +1,235 @@
1
+ // src/contract.ts
2
+ import { z } from "zod";
3
+ var sessionSchema = z.looseObject({
4
+ id: z.string(),
5
+ directory: z.string().optional(),
6
+ title: z.string().optional()
7
+ });
8
+ var sessionListSchema = z.array(sessionSchema);
9
+ var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
10
+ var todoSchema = z.array(z.looseObject({
11
+ content: z.string(),
12
+ status: z.string(),
13
+ priority: z.string()
14
+ })).default([]);
15
+ var diffEntrySchema = z.looseObject({
16
+ file: z.string().optional(),
17
+ additions: z.number(),
18
+ deletions: z.number(),
19
+ status: z.string().optional()
20
+ });
21
+ var diffSchema = z.array(diffEntrySchema).default([]);
22
+ var vcsStatusEntrySchema = z.looseObject({
23
+ file: z.string(),
24
+ additions: z.number(),
25
+ deletions: z.number(),
26
+ status: z.string().optional()
27
+ });
28
+ var vcsStatusSchema = z.array(vcsStatusEntrySchema).default([]);
29
+ var turnDiffEntrySchema = diffEntrySchema;
30
+ var turnMessageSchema = z.looseObject({
31
+ info: z.looseObject({
32
+ role: z.string(),
33
+ summary: z.looseObject({
34
+ diffs: z.array(turnDiffEntrySchema).optional()
35
+ }).optional()
36
+ })
37
+ });
38
+ var turnMessageListSchema = z.array(turnMessageSchema);
39
+ var sessionSummarySchema = z.looseObject({
40
+ summary: z.looseObject({
41
+ diffs: z.array(turnDiffEntrySchema).optional()
42
+ }).optional()
43
+ });
44
+ var messagePartSchema = z.looseObject({
45
+ type: z.string(),
46
+ text: z.string().optional()
47
+ });
48
+ var messageEnvelopeSchema = z.looseObject({
49
+ info: z.looseObject({ role: z.string() }),
50
+ parts: z.array(messagePartSchema)
51
+ });
52
+ var messageListSchema = z.array(messageEnvelopeSchema);
53
+ var pendingQuestionEntrySchema = z.looseObject({
54
+ id: z.string(),
55
+ sessionID: z.string(),
56
+ questions: z.array(z.looseObject({
57
+ question: z.string().optional(),
58
+ options: z.array(z.looseObject({ label: z.string().optional() })).optional()
59
+ })).optional()
60
+ });
61
+ var pendingQuestionListSchema = z.array(pendingQuestionEntrySchema);
62
+ var questionEntrySchema = z.looseObject({
63
+ id: z.string(),
64
+ sessionID: z.string()
65
+ });
66
+ var questionListSchema = z.array(questionEntrySchema);
67
+ var LOOPBACK_HOSTS = new Set([
68
+ "127.0.0.1",
69
+ "::1",
70
+ "[::1]",
71
+ "localhost"
72
+ ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
80
+ var projectSchema = z.looseObject({
81
+ name: z.string(),
82
+ path: z.string(),
83
+ description: z.string(),
84
+ expandedPath: z.string(),
85
+ exists: z.boolean()
86
+ });
87
+ var rosterSchema = z.object({
88
+ server: z.object({ baseUrl: z.url() }),
89
+ projects: z.array(projectSchema)
90
+ });
91
+ var credentialsSchema = z.object({
92
+ username: z.string().optional(),
93
+ password: z.string().optional()
94
+ });
95
+ var busContextSchema = z.object({
96
+ roster: rosterSchema,
97
+ credentials: credentialsSchema.optional()
98
+ });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
112
+
113
+ // src/attach.ts
114
+ function err(error) {
115
+ return { ok: false, error };
116
+ }
117
+ function posixJoin(...parts) {
118
+ const isAbsolute = (parts[0] ?? "").startsWith("/");
119
+ const segments = [];
120
+ for (const part of parts) {
121
+ for (const segment of part.split("/")) {
122
+ if (segment.length > 0)
123
+ segments.push(segment);
124
+ }
125
+ }
126
+ const joined = segments.join("/");
127
+ return isAbsolute ? `/${joined}` : joined;
128
+ }
129
+ async function sha256Hex16(input) {
130
+ const bytes = new TextEncoder().encode(input);
131
+ const digest = await crypto.subtle.digest("SHA-256", bytes);
132
+ const hex = Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
133
+ return hex.slice(0, 16);
134
+ }
135
+ function toBase64(s) {
136
+ const bytes = new TextEncoder().encode(s);
137
+ let bin = "";
138
+ for (const b of bytes)
139
+ bin += String.fromCharCode(b);
140
+ return btoa(bin);
141
+ }
142
+ function authHeader(password) {
143
+ const token = toBase64(`opencode:${password}`);
144
+ return { Authorization: `Basic ${token}` };
145
+ }
146
+ async function resolveRosterPath(workspaceDir, seams) {
147
+ const override = await seams.env("SPACE_BUS_CONFIG");
148
+ let candidate;
149
+ if (override) {
150
+ if (override.includes("://")) {
151
+ return err(`space-bus: SPACE_BUS_CONFIG needs an absolute filesystem path or a ~-prefixed path, not a URL (got: ${override})`);
152
+ }
153
+ const expanded = override.startsWith("~/") ? posixJoin(await seams.homeDir(), override.slice(2)) : override;
154
+ if (!expanded.startsWith("/")) {
155
+ return err(`space-bus: SPACE_BUS_CONFIG needs an absolute filesystem path or a ~-prefixed path (got: ${override})`);
156
+ }
157
+ candidate = expanded;
158
+ } else {
159
+ candidate = posixJoin(workspaceDir, "spacebus.json");
160
+ }
161
+ const canonical = await seams.realpath(candidate);
162
+ if (canonical === null) {
163
+ return err(`space-bus: no spacebus.json at ${candidate}`);
164
+ }
165
+ return { ok: true, rosterPath: canonical };
166
+ }
167
+ async function discoveryPathFor(rosterPath, seams) {
168
+ const xdgStateHome = await seams.env("XDG_STATE_HOME");
169
+ const base = xdgStateHome ?? posixJoin(await seams.homeDir(), ".local", "state");
170
+ const hash = await sha256Hex16(rosterPath);
171
+ return posixJoin(base, "space-bus", hash, "discovery.json");
172
+ }
173
+ async function resolveManagedServer(workspaceDir, seams) {
174
+ const rosterResult = await resolveRosterPath(workspaceDir, seams);
175
+ if (!rosterResult.ok)
176
+ return rosterResult;
177
+ const { rosterPath } = rosterResult;
178
+ const discoveryPath = await discoveryPathFor(rosterPath, seams);
179
+ const raw = await seams.readTextFile(discoveryPath);
180
+ if (raw === null) {
181
+ return err("space-bus: managed space-bus server is not running for this workspace (no discovery file) — run `space-bus serve` in the workspace (or use a space-bus tool in an opencode session)");
182
+ }
183
+ let json;
184
+ try {
185
+ json = JSON.parse(raw);
186
+ } catch (e) {
187
+ return err(`space-bus: discovery file at ${discoveryPath} is not valid JSON: ${e.message}`);
188
+ }
189
+ const parsed = discoveryFileSchema.safeParse(json);
190
+ if (!parsed.success) {
191
+ return err(`space-bus: discovery file at ${discoveryPath} failed schema validation: ${parsed.error.message}`);
192
+ }
193
+ const discovery = parsed.data;
194
+ if (!loopbackOk(discovery.baseUrl)) {
195
+ const host = (() => {
196
+ try {
197
+ return new URL(discovery.baseUrl).hostname;
198
+ } catch {
199
+ return discovery.baseUrl;
200
+ }
201
+ })();
202
+ return err(`space-bus: refusing to send credentials off-machine (got ${host})`);
203
+ }
204
+ const probe = await probeLiveness(discovery.baseUrl, discovery.password);
205
+ if (!probe.ok)
206
+ return probe;
207
+ return {
208
+ ok: true,
209
+ baseUrl: discovery.baseUrl,
210
+ credentials: { username: "opencode", password: discovery.password },
211
+ alive: true
212
+ };
213
+ }
214
+ async function probeLiveness(baseUrl, password) {
215
+ let res;
216
+ try {
217
+ res = await fetch(`${baseUrl}/session?limit=1`, {
218
+ headers: authHeader(password),
219
+ signal: AbortSignal.timeout(3000)
220
+ });
221
+ } catch {
222
+ return err(`space-bus: managed daemon not answering at ${baseUrl} — it may have exited; run \`space-bus serve\` in the workspace to restart it`);
223
+ }
224
+ if (res.status === 401) {
225
+ return err("space-bus: managed server rejected credentials (stale discovery?) — restart the workspace daemon");
226
+ }
227
+ if (!res.ok) {
228
+ return err(`space-bus: managed daemon not answering at ${baseUrl} — it may have exited; run \`space-bus serve\` in the workspace to restart it`);
229
+ }
230
+ return { ok: true };
231
+ }
232
+ export {
233
+ resolveManagedServer,
234
+ posixJoin
235
+ };
package/dist/cli.js CHANGED
@@ -71,6 +71,13 @@ var LOOPBACK_HOSTS = new Set([
71
71
  "[::1]",
72
72
  "localhost"
73
73
  ]);
74
+ function loopbackOk(baseUrl) {
75
+ try {
76
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
77
+ } catch {
78
+ return false;
79
+ }
80
+ }
74
81
  var projectSchema = z.looseObject({
75
82
  name: z.string(),
76
83
  path: z.string(),
@@ -90,6 +97,19 @@ var busContextSchema = z.object({
90
97
  roster: rosterSchema,
91
98
  credentials: credentialsSchema.optional()
92
99
  });
100
+ var managedSpawnConfigSchema = z.object({
101
+ command: z.array(z.string()).optional(),
102
+ cwd: z.string().optional(),
103
+ port: z.number().int().nonnegative().optional()
104
+ });
105
+ var discoveryFileSchema = z.object({
106
+ port: z.number().int().nonnegative(),
107
+ pid: z.number().int().min(2),
108
+ identity: z.string(),
109
+ password: z.string(),
110
+ spawnConfig: managedSpawnConfigSchema,
111
+ baseUrl: z.url()
112
+ });
93
113
 
94
114
  // src/config.ts
95
115
  import { existsSync, readFileSync as readFileSync2, realpathSync } from "node:fs";
@@ -129,19 +149,6 @@ function lockFilePath(rosterPath) {
129
149
  function logFilePath(rosterPath) {
130
150
  return join(stateDirFor(rosterPath), "server.log");
131
151
  }
132
- var managedSpawnConfigSchema = z2.object({
133
- command: z2.array(z2.string()).optional(),
134
- cwd: z2.string().optional(),
135
- port: z2.number().int().nonnegative().optional()
136
- });
137
- var discoveryFileSchema = z2.object({
138
- port: z2.number().int().nonnegative(),
139
- pid: z2.number().int().positive(),
140
- identity: z2.string(),
141
- password: z2.string(),
142
- spawnConfig: managedSpawnConfigSchema,
143
- baseUrl: z2.url()
144
- });
145
152
  function writeDiscovery(rosterPath, data) {
146
153
  const dir = stateDirFor(rosterPath);
147
154
  mkdirSync(dir, { recursive: true, mode: 448 });
@@ -182,14 +189,6 @@ function removeDiscovery(rosterPath) {
182
189
  throw err;
183
190
  }
184
191
  }
185
- function loopbackOk(baseUrl) {
186
- try {
187
- const url = new URL(baseUrl);
188
- return LOOPBACK_HOSTS.has(url.hostname);
189
- } catch {
190
- return false;
191
- }
192
- }
193
192
  function attachLive(rosterPath) {
194
193
  const discovery = readDiscovery(rosterPath);
195
194
  if (!discovery)
@@ -232,7 +231,7 @@ function verifyIdentity(pid, storedIdentity) {
232
231
  return current !== null && current === storedIdentity;
233
232
  }
234
233
  var lockFileSchema = z2.object({
235
- pid: z2.number().int().positive(),
234
+ pid: z2.number().int().min(2),
236
235
  startTime: z2.string(),
237
236
  since: z2.number()
238
237
  });
@@ -328,7 +327,7 @@ function provisionalFilePath(rosterPath) {
328
327
  return join(stateDirFor(rosterPath), "spawn.provisional.json");
329
328
  }
330
329
  var provisionalFileSchema = z2.object({
331
- pid: z2.number().int().positive(),
330
+ pid: z2.number().int().min(2),
332
331
  identity: z2.string(),
333
332
  password: z2.string(),
334
333
  since: z2.number()
@@ -524,11 +523,34 @@ function redactedReadinessError(rosterPath, password, reason) {
524
523
  return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
525
524
  ${tail}`);
526
525
  }
526
+ function isEpermError(err) {
527
+ return typeof err === "object" && err !== null && "code" in err && err.code === "EPERM";
528
+ }
529
+ function signalGroup(pid, sig) {
530
+ if (pid <= 1) {
531
+ try {
532
+ process.kill(pid, sig);
533
+ } catch {}
534
+ return false;
535
+ }
536
+ try {
537
+ process.kill(-pid, sig);
538
+ return true;
539
+ } catch (err) {
540
+ if (err.code === "EPERM") {
541
+ throw err;
542
+ }
543
+ }
544
+ try {
545
+ process.kill(pid, sig);
546
+ } catch {}
547
+ return false;
548
+ }
527
549
  function killIdentifiedProcess(pid, identity) {
528
550
  try {
529
551
  if (identity !== null && identity !== undefined && identity !== "") {
530
552
  if (verifyIdentity(pid, identity))
531
- process.kill(pid, "SIGTERM");
553
+ signalGroup(pid, "SIGTERM");
532
554
  return;
533
555
  }
534
556
  process.kill(pid, "SIGTERM");
@@ -777,23 +799,31 @@ async function stopServer(rosterPath) {
777
799
  return { stopped: false };
778
800
  }
779
801
  const { pid } = discovery;
802
+ let groupSignaled;
803
+ const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
780
804
  try {
781
- process.kill(pid, "SIGTERM");
782
- } catch {
805
+ groupSignaled = signalGroup(pid, "SIGTERM");
806
+ } catch (err) {
807
+ if (isEpermError(err)) {
808
+ return { stopped: false };
809
+ }
783
810
  removeDiscovery(rosterPath);
784
811
  return { stopped: true };
785
812
  }
786
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
813
+ if (await waitForCompletion(STOP_GRACE_MS)) {
787
814
  removeDiscovery(rosterPath);
788
815
  return { stopped: true };
789
816
  }
790
817
  try {
791
- process.kill(pid, "SIGKILL");
792
- } catch {
818
+ groupSignaled = signalGroup(pid, "SIGKILL");
819
+ } catch (err) {
820
+ if (isEpermError(err)) {
821
+ return { stopped: false };
822
+ }
793
823
  removeDiscovery(rosterPath);
794
824
  return { stopped: true };
795
825
  }
796
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
826
+ if (await waitForCompletion(STOP_GRACE_MS)) {
797
827
  removeDiscovery(rosterPath);
798
828
  return { stopped: true };
799
829
  }
@@ -808,6 +838,23 @@ async function waitForDeath(pid, budgetMs) {
808
838
  }
809
839
  return !isAlive(pid);
810
840
  }
841
+ async function waitForGroupDeath(pid, budgetMs) {
842
+ const deadline = Date.now() + budgetMs;
843
+ const groupAlive = () => {
844
+ try {
845
+ process.kill(-pid, 0);
846
+ return true;
847
+ } catch {
848
+ return false;
849
+ }
850
+ };
851
+ while (Date.now() < deadline) {
852
+ if (!groupAlive())
853
+ return true;
854
+ await sleep(POLL_INTERVAL_MS);
855
+ }
856
+ return !groupAlive();
857
+ }
811
858
 
812
859
  // src/cli.ts
813
860
  var USAGE = `space-bus — thin CLI for the managed OpenCode bus server
package/dist/config.js CHANGED
@@ -70,6 +70,13 @@ var LOOPBACK_HOSTS = new Set([
70
70
  "[::1]",
71
71
  "localhost"
72
72
  ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
73
80
  var projectSchema = z.looseObject({
74
81
  name: z.string(),
75
82
  path: z.string(),
@@ -89,6 +96,19 @@ var busContextSchema = z.object({
89
96
  roster: rosterSchema,
90
97
  credentials: credentialsSchema.optional()
91
98
  });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
92
112
 
93
113
  // src/config.ts
94
114
  import { existsSync, readFileSync as readFileSync2, realpathSync } from "node:fs";
@@ -128,19 +148,6 @@ function lockFilePath(rosterPath) {
128
148
  function logFilePath(rosterPath) {
129
149
  return join(stateDirFor(rosterPath), "server.log");
130
150
  }
131
- var managedSpawnConfigSchema = z2.object({
132
- command: z2.array(z2.string()).optional(),
133
- cwd: z2.string().optional(),
134
- port: z2.number().int().nonnegative().optional()
135
- });
136
- var discoveryFileSchema = z2.object({
137
- port: z2.number().int().nonnegative(),
138
- pid: z2.number().int().positive(),
139
- identity: z2.string(),
140
- password: z2.string(),
141
- spawnConfig: managedSpawnConfigSchema,
142
- baseUrl: z2.url()
143
- });
144
151
  function writeDiscovery(rosterPath, data) {
145
152
  const dir = stateDirFor(rosterPath);
146
153
  mkdirSync(dir, { recursive: true, mode: 448 });
@@ -181,14 +188,6 @@ function removeDiscovery(rosterPath) {
181
188
  throw err;
182
189
  }
183
190
  }
184
- function loopbackOk(baseUrl) {
185
- try {
186
- const url = new URL(baseUrl);
187
- return LOOPBACK_HOSTS.has(url.hostname);
188
- } catch {
189
- return false;
190
- }
191
- }
192
191
  function attachLive(rosterPath) {
193
192
  const discovery = readDiscovery(rosterPath);
194
193
  if (!discovery)
@@ -231,7 +230,7 @@ function verifyIdentity(pid, storedIdentity) {
231
230
  return current !== null && current === storedIdentity;
232
231
  }
233
232
  var lockFileSchema = z2.object({
234
- pid: z2.number().int().positive(),
233
+ pid: z2.number().int().min(2),
235
234
  startTime: z2.string(),
236
235
  since: z2.number()
237
236
  });
@@ -327,7 +326,7 @@ function provisionalFilePath(rosterPath) {
327
326
  return join(stateDirFor(rosterPath), "spawn.provisional.json");
328
327
  }
329
328
  var provisionalFileSchema = z2.object({
330
- pid: z2.number().int().positive(),
329
+ pid: z2.number().int().min(2),
331
330
  identity: z2.string(),
332
331
  password: z2.string(),
333
332
  since: z2.number()
@@ -159,6 +159,13 @@ export declare const questionListSchema: z.ZodArray<z.ZodObject<{
159
159
  }, z.core.$loose>>;
160
160
  export type QuestionListSchema = z.infer<typeof questionListSchema>;
161
161
  export declare const LOOPBACK_HOSTS: Set<string>;
162
+ /**
163
+ * Shared loopback-hostname check, used identically by attach.ts (browser-safe
164
+ * reader) and discovery.ts (Node-only writer/re-validator) — both need the
165
+ * exact same `new URL(baseUrl).hostname` + LOOPBACK_HOSTS membership check,
166
+ * so it lives here once rather than duplicated in each.
167
+ */
168
+ export declare function loopbackOk(baseUrl: string): boolean;
162
169
  export declare const projectSchema: z.ZodObject<{
163
170
  name: z.ZodString;
164
171
  path: z.ZodString;
@@ -213,3 +220,20 @@ export declare const busContextSchema: z.ZodObject<{
213
220
  }, z.core.$strip>>;
214
221
  }, z.core.$strip>;
215
222
  export type BusContext = z.infer<typeof busContextSchema>;
223
+ export declare const managedSpawnConfigSchema: z.ZodObject<{
224
+ command: z.ZodOptional<z.ZodArray<z.ZodString>>;
225
+ cwd: z.ZodOptional<z.ZodString>;
226
+ port: z.ZodOptional<z.ZodNumber>;
227
+ }, z.core.$strip>;
228
+ export declare const discoveryFileSchema: z.ZodObject<{
229
+ port: z.ZodNumber;
230
+ pid: z.ZodNumber;
231
+ identity: z.ZodString;
232
+ password: z.ZodString;
233
+ spawnConfig: z.ZodObject<{
234
+ command: z.ZodOptional<z.ZodArray<z.ZodString>>;
235
+ cwd: z.ZodOptional<z.ZodString>;
236
+ port: z.ZodOptional<z.ZodNumber>;
237
+ }, z.core.$strip>;
238
+ baseUrl: z.ZodURL;
239
+ }, z.core.$strip>;
package/dist/contract.js CHANGED
@@ -70,6 +70,13 @@ var LOOPBACK_HOSTS = new Set([
70
70
  "[::1]",
71
71
  "localhost"
72
72
  ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
73
80
  var projectSchema = z.looseObject({
74
81
  name: z.string(),
75
82
  path: z.string(),
@@ -89,6 +96,19 @@ var busContextSchema = z.object({
89
96
  roster: rosterSchema,
90
97
  credentials: credentialsSchema.optional()
91
98
  });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
92
112
  export {
93
113
  vcsStatusSchema,
94
114
  vcsStatusEntrySchema,
@@ -109,6 +129,9 @@ export {
109
129
  messagePartSchema,
110
130
  messageListSchema,
111
131
  messageEnvelopeSchema,
132
+ managedSpawnConfigSchema,
133
+ loopbackOk,
134
+ discoveryFileSchema,
112
135
  diffSchema,
113
136
  diffEntrySchema,
114
137
  credentialsSchema,
package/dist/core.js CHANGED
@@ -70,6 +70,13 @@ var LOOPBACK_HOSTS = new Set([
70
70
  "[::1]",
71
71
  "localhost"
72
72
  ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
73
80
  var projectSchema = z.looseObject({
74
81
  name: z.string(),
75
82
  path: z.string(),
@@ -89,6 +96,19 @@ var busContextSchema = z.object({
89
96
  roster: rosterSchema,
90
97
  credentials: credentialsSchema.optional()
91
98
  });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
92
112
 
93
113
  // src/core.ts
94
114
  function findProject(projects, name) {
@@ -1,4 +1,6 @@
1
1
  import { z } from "zod";
2
+ import { discoveryFileSchema, loopbackOk, managedSpawnConfigSchema } from "./contract";
3
+ export { discoveryFileSchema, managedSpawnConfigSchema };
2
4
  /**
3
5
  * Per-roster state directory: `$XDG_STATE_HOME|~/.local/state`/
4
6
  * `space-bus/<first 16 hex of sha256(absolute roster path)>/`. Keying by a
@@ -9,23 +11,6 @@ export declare function stateDirFor(rosterPath: string): string;
9
11
  export declare function discoveryFilePath(rosterPath: string): string;
10
12
  export declare function lockFilePath(rosterPath: string): string;
11
13
  export declare function logFilePath(rosterPath: string): string;
12
- export declare const managedSpawnConfigSchema: z.ZodObject<{
13
- command: z.ZodOptional<z.ZodArray<z.ZodString>>;
14
- cwd: z.ZodOptional<z.ZodString>;
15
- port: z.ZodOptional<z.ZodNumber>;
16
- }, z.core.$strip>;
17
- export declare const discoveryFileSchema: z.ZodObject<{
18
- port: z.ZodNumber;
19
- pid: z.ZodNumber;
20
- identity: z.ZodString;
21
- password: z.ZodString;
22
- spawnConfig: z.ZodObject<{
23
- command: z.ZodOptional<z.ZodArray<z.ZodString>>;
24
- cwd: z.ZodOptional<z.ZodString>;
25
- port: z.ZodOptional<z.ZodNumber>;
26
- }, z.core.$strip>;
27
- baseUrl: z.ZodURL;
28
- }, z.core.$strip>;
29
14
  export type DiscoveryFile = z.infer<typeof discoveryFileSchema>;
30
15
  /**
31
16
  * Writes the discovery file atomically: a temp file (0600) in the same
@@ -37,7 +22,7 @@ export declare function writeDiscovery(rosterPath: string, data: DiscoveryFile):
37
22
  /** Reads and validates the discovery file; returns null if absent or corrupt. */
38
23
  export declare function readDiscovery(rosterPath: string): DiscoveryFile | null;
39
24
  export declare function removeDiscovery(rosterPath: string): void;
40
- export declare function loopbackOk(baseUrl: string): boolean;
25
+ export { loopbackOk };
41
26
  export interface LiveEndpoint {
42
27
  baseUrl: string;
43
28
  credentials: {
package/dist/index.js CHANGED
@@ -70,6 +70,13 @@ var LOOPBACK_HOSTS = new Set([
70
70
  "[::1]",
71
71
  "localhost"
72
72
  ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
73
80
  var projectSchema = z.looseObject({
74
81
  name: z.string(),
75
82
  path: z.string(),
@@ -89,6 +96,19 @@ var busContextSchema = z.object({
89
96
  roster: rosterSchema,
90
97
  credentials: credentialsSchema.optional()
91
98
  });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
92
112
 
93
113
  // src/core.ts
94
114
  function findProject(projects, name) {
@@ -713,19 +733,6 @@ function lockFilePath(rosterPath) {
713
733
  function logFilePath(rosterPath) {
714
734
  return join(stateDirFor(rosterPath), "server.log");
715
735
  }
716
- var managedSpawnConfigSchema = z2.object({
717
- command: z2.array(z2.string()).optional(),
718
- cwd: z2.string().optional(),
719
- port: z2.number().int().nonnegative().optional()
720
- });
721
- var discoveryFileSchema = z2.object({
722
- port: z2.number().int().nonnegative(),
723
- pid: z2.number().int().positive(),
724
- identity: z2.string(),
725
- password: z2.string(),
726
- spawnConfig: managedSpawnConfigSchema,
727
- baseUrl: z2.url()
728
- });
729
736
  function writeDiscovery(rosterPath, data) {
730
737
  const dir = stateDirFor(rosterPath);
731
738
  mkdirSync(dir, { recursive: true, mode: 448 });
@@ -766,14 +773,6 @@ function removeDiscovery(rosterPath) {
766
773
  throw err2;
767
774
  }
768
775
  }
769
- function loopbackOk(baseUrl) {
770
- try {
771
- const url = new URL(baseUrl);
772
- return LOOPBACK_HOSTS.has(url.hostname);
773
- } catch {
774
- return false;
775
- }
776
- }
777
776
  function attachLive(rosterPath) {
778
777
  const discovery = readDiscovery(rosterPath);
779
778
  if (!discovery)
@@ -816,7 +815,7 @@ function verifyIdentity(pid, storedIdentity) {
816
815
  return current !== null && current === storedIdentity;
817
816
  }
818
817
  var lockFileSchema = z2.object({
819
- pid: z2.number().int().positive(),
818
+ pid: z2.number().int().min(2),
820
819
  startTime: z2.string(),
821
820
  since: z2.number()
822
821
  });
@@ -912,7 +911,7 @@ function provisionalFilePath(rosterPath) {
912
911
  return join(stateDirFor(rosterPath), "spawn.provisional.json");
913
912
  }
914
913
  var provisionalFileSchema = z2.object({
915
- pid: z2.number().int().positive(),
914
+ pid: z2.number().int().min(2),
916
915
  identity: z2.string(),
917
916
  password: z2.string(),
918
917
  since: z2.number()
@@ -1108,11 +1107,34 @@ function redactedReadinessError(rosterPath, password, reason) {
1108
1107
  return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
1109
1108
  ${tail}`);
1110
1109
  }
1110
+ function isEpermError(err2) {
1111
+ return typeof err2 === "object" && err2 !== null && "code" in err2 && err2.code === "EPERM";
1112
+ }
1113
+ function signalGroup(pid, sig) {
1114
+ if (pid <= 1) {
1115
+ try {
1116
+ process.kill(pid, sig);
1117
+ } catch {}
1118
+ return false;
1119
+ }
1120
+ try {
1121
+ process.kill(-pid, sig);
1122
+ return true;
1123
+ } catch (err2) {
1124
+ if (err2.code === "EPERM") {
1125
+ throw err2;
1126
+ }
1127
+ }
1128
+ try {
1129
+ process.kill(pid, sig);
1130
+ } catch {}
1131
+ return false;
1132
+ }
1111
1133
  function killIdentifiedProcess(pid, identity) {
1112
1134
  try {
1113
1135
  if (identity !== null && identity !== undefined && identity !== "") {
1114
1136
  if (verifyIdentity(pid, identity))
1115
- process.kill(pid, "SIGTERM");
1137
+ signalGroup(pid, "SIGTERM");
1116
1138
  return;
1117
1139
  }
1118
1140
  process.kill(pid, "SIGTERM");
@@ -1361,23 +1383,31 @@ async function stopServer(rosterPath) {
1361
1383
  return { stopped: false };
1362
1384
  }
1363
1385
  const { pid } = discovery;
1386
+ let groupSignaled;
1387
+ const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
1364
1388
  try {
1365
- process.kill(pid, "SIGTERM");
1366
- } catch {
1389
+ groupSignaled = signalGroup(pid, "SIGTERM");
1390
+ } catch (err2) {
1391
+ if (isEpermError(err2)) {
1392
+ return { stopped: false };
1393
+ }
1367
1394
  removeDiscovery(rosterPath);
1368
1395
  return { stopped: true };
1369
1396
  }
1370
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
1397
+ if (await waitForCompletion(STOP_GRACE_MS)) {
1371
1398
  removeDiscovery(rosterPath);
1372
1399
  return { stopped: true };
1373
1400
  }
1374
1401
  try {
1375
- process.kill(pid, "SIGKILL");
1376
- } catch {
1402
+ groupSignaled = signalGroup(pid, "SIGKILL");
1403
+ } catch (err2) {
1404
+ if (isEpermError(err2)) {
1405
+ return { stopped: false };
1406
+ }
1377
1407
  removeDiscovery(rosterPath);
1378
1408
  return { stopped: true };
1379
1409
  }
1380
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
1410
+ if (await waitForCompletion(STOP_GRACE_MS)) {
1381
1411
  removeDiscovery(rosterPath);
1382
1412
  return { stopped: true };
1383
1413
  }
@@ -1392,6 +1422,23 @@ async function waitForDeath(pid, budgetMs) {
1392
1422
  }
1393
1423
  return !isAlive(pid);
1394
1424
  }
1425
+ async function waitForGroupDeath(pid, budgetMs) {
1426
+ const deadline = Date.now() + budgetMs;
1427
+ const groupAlive = () => {
1428
+ try {
1429
+ process.kill(-pid, 0);
1430
+ return true;
1431
+ } catch {
1432
+ return false;
1433
+ }
1434
+ };
1435
+ while (Date.now() < deadline) {
1436
+ if (!groupAlive())
1437
+ return true;
1438
+ await sleep(POLL_INTERVAL_MS);
1439
+ }
1440
+ return !groupAlive();
1441
+ }
1395
1442
 
1396
1443
  // src/tools/bus_result.ts
1397
1444
  import { tool } from "@opencode-ai/plugin";
package/dist/mcp.js CHANGED
@@ -71,6 +71,13 @@ var LOOPBACK_HOSTS = new Set([
71
71
  "[::1]",
72
72
  "localhost"
73
73
  ]);
74
+ function loopbackOk(baseUrl) {
75
+ try {
76
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
77
+ } catch {
78
+ return false;
79
+ }
80
+ }
74
81
  var projectSchema = z.looseObject({
75
82
  name: z.string(),
76
83
  path: z.string(),
@@ -90,6 +97,19 @@ var busContextSchema = z.object({
90
97
  roster: rosterSchema,
91
98
  credentials: credentialsSchema.optional()
92
99
  });
100
+ var managedSpawnConfigSchema = z.object({
101
+ command: z.array(z.string()).optional(),
102
+ cwd: z.string().optional(),
103
+ port: z.number().int().nonnegative().optional()
104
+ });
105
+ var discoveryFileSchema = z.object({
106
+ port: z.number().int().nonnegative(),
107
+ pid: z.number().int().min(2),
108
+ identity: z.string(),
109
+ password: z.string(),
110
+ spawnConfig: managedSpawnConfigSchema,
111
+ baseUrl: z.url()
112
+ });
93
113
 
94
114
  // src/core.ts
95
115
  function findProject(projects, name) {
@@ -714,19 +734,6 @@ function lockFilePath(rosterPath) {
714
734
  function logFilePath(rosterPath) {
715
735
  return join(stateDirFor(rosterPath), "server.log");
716
736
  }
717
- var managedSpawnConfigSchema = z2.object({
718
- command: z2.array(z2.string()).optional(),
719
- cwd: z2.string().optional(),
720
- port: z2.number().int().nonnegative().optional()
721
- });
722
- var discoveryFileSchema = z2.object({
723
- port: z2.number().int().nonnegative(),
724
- pid: z2.number().int().positive(),
725
- identity: z2.string(),
726
- password: z2.string(),
727
- spawnConfig: managedSpawnConfigSchema,
728
- baseUrl: z2.url()
729
- });
730
737
  function writeDiscovery(rosterPath, data) {
731
738
  const dir = stateDirFor(rosterPath);
732
739
  mkdirSync(dir, { recursive: true, mode: 448 });
@@ -767,14 +774,6 @@ function removeDiscovery(rosterPath) {
767
774
  throw err2;
768
775
  }
769
776
  }
770
- function loopbackOk(baseUrl) {
771
- try {
772
- const url = new URL(baseUrl);
773
- return LOOPBACK_HOSTS.has(url.hostname);
774
- } catch {
775
- return false;
776
- }
777
- }
778
777
  function attachLive(rosterPath) {
779
778
  const discovery = readDiscovery(rosterPath);
780
779
  if (!discovery)
@@ -817,7 +816,7 @@ function verifyIdentity(pid, storedIdentity) {
817
816
  return current !== null && current === storedIdentity;
818
817
  }
819
818
  var lockFileSchema = z2.object({
820
- pid: z2.number().int().positive(),
819
+ pid: z2.number().int().min(2),
821
820
  startTime: z2.string(),
822
821
  since: z2.number()
823
822
  });
@@ -913,7 +912,7 @@ function provisionalFilePath(rosterPath) {
913
912
  return join(stateDirFor(rosterPath), "spawn.provisional.json");
914
913
  }
915
914
  var provisionalFileSchema = z2.object({
916
- pid: z2.number().int().positive(),
915
+ pid: z2.number().int().min(2),
917
916
  identity: z2.string(),
918
917
  password: z2.string(),
919
918
  since: z2.number()
@@ -1109,11 +1108,34 @@ function redactedReadinessError(rosterPath, password, reason) {
1109
1108
  return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
1110
1109
  ${tail}`);
1111
1110
  }
1111
+ function isEpermError(err2) {
1112
+ return typeof err2 === "object" && err2 !== null && "code" in err2 && err2.code === "EPERM";
1113
+ }
1114
+ function signalGroup(pid, sig) {
1115
+ if (pid <= 1) {
1116
+ try {
1117
+ process.kill(pid, sig);
1118
+ } catch {}
1119
+ return false;
1120
+ }
1121
+ try {
1122
+ process.kill(-pid, sig);
1123
+ return true;
1124
+ } catch (err2) {
1125
+ if (err2.code === "EPERM") {
1126
+ throw err2;
1127
+ }
1128
+ }
1129
+ try {
1130
+ process.kill(pid, sig);
1131
+ } catch {}
1132
+ return false;
1133
+ }
1112
1134
  function killIdentifiedProcess(pid, identity) {
1113
1135
  try {
1114
1136
  if (identity !== null && identity !== undefined && identity !== "") {
1115
1137
  if (verifyIdentity(pid, identity))
1116
- process.kill(pid, "SIGTERM");
1138
+ signalGroup(pid, "SIGTERM");
1117
1139
  return;
1118
1140
  }
1119
1141
  process.kill(pid, "SIGTERM");
@@ -1362,23 +1384,31 @@ async function stopServer(rosterPath) {
1362
1384
  return { stopped: false };
1363
1385
  }
1364
1386
  const { pid } = discovery;
1387
+ let groupSignaled;
1388
+ const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
1365
1389
  try {
1366
- process.kill(pid, "SIGTERM");
1367
- } catch {
1390
+ groupSignaled = signalGroup(pid, "SIGTERM");
1391
+ } catch (err2) {
1392
+ if (isEpermError(err2)) {
1393
+ return { stopped: false };
1394
+ }
1368
1395
  removeDiscovery(rosterPath);
1369
1396
  return { stopped: true };
1370
1397
  }
1371
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
1398
+ if (await waitForCompletion(STOP_GRACE_MS)) {
1372
1399
  removeDiscovery(rosterPath);
1373
1400
  return { stopped: true };
1374
1401
  }
1375
1402
  try {
1376
- process.kill(pid, "SIGKILL");
1377
- } catch {
1403
+ groupSignaled = signalGroup(pid, "SIGKILL");
1404
+ } catch (err2) {
1405
+ if (isEpermError(err2)) {
1406
+ return { stopped: false };
1407
+ }
1378
1408
  removeDiscovery(rosterPath);
1379
1409
  return { stopped: true };
1380
1410
  }
1381
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
1411
+ if (await waitForCompletion(STOP_GRACE_MS)) {
1382
1412
  removeDiscovery(rosterPath);
1383
1413
  return { stopped: true };
1384
1414
  }
@@ -1393,6 +1423,23 @@ async function waitForDeath(pid, budgetMs) {
1393
1423
  }
1394
1424
  return !isAlive(pid);
1395
1425
  }
1426
+ async function waitForGroupDeath(pid, budgetMs) {
1427
+ const deadline = Date.now() + budgetMs;
1428
+ const groupAlive = () => {
1429
+ try {
1430
+ process.kill(-pid, 0);
1431
+ return true;
1432
+ } catch {
1433
+ return false;
1434
+ }
1435
+ };
1436
+ while (Date.now() < deadline) {
1437
+ if (!groupAlive())
1438
+ return true;
1439
+ await sleep(POLL_INTERVAL_MS);
1440
+ }
1441
+ return !groupAlive();
1442
+ }
1396
1443
 
1397
1444
  // src/mcp.ts
1398
1445
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -1522,7 +1569,7 @@ async function mcpLoadContext() {
1522
1569
  }
1523
1570
  return loadContext();
1524
1571
  }
1525
- var version = "0.6.1";
1572
+ var version = "0.7.1";
1526
1573
  var server = new McpServer({
1527
1574
  name: "space-bus",
1528
1575
  version
package/dist/server.d.ts CHANGED
@@ -51,15 +51,6 @@ export declare function ensureServer(rosterPath: string, options?: EnsureServerO
51
51
  * recorded at spawn time (R5b) — surfaced, not auto-corrected.
52
52
  */
53
53
  export declare function serverStatus(rosterPath: string): ServerStatus;
54
- /**
55
- * Stops a managed server: identity-verified SIGTERM, then polls for actual
56
- * death (bounded by STOP_GRACE_MS) before escalating to SIGKILL and polling
57
- * again. The discovery file is removed ONLY once the process is confirmed
58
- * dead (or was never verified-live/already gone) — a process that ignores
59
- * both signals is reported as not stopped and the discovery file is left in
60
- * place, so credentials aren't silently discarded while the server still
61
- * holds the port. OS-authz only (R7b) — no additional permission model.
62
- */
63
54
  export declare function stopServer(rosterPath: string): Promise<{
64
55
  stopped: boolean;
65
56
  }>;
package/dist/server.js CHANGED
@@ -70,6 +70,13 @@ var LOOPBACK_HOSTS = new Set([
70
70
  "[::1]",
71
71
  "localhost"
72
72
  ]);
73
+ function loopbackOk(baseUrl) {
74
+ try {
75
+ return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname);
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
73
80
  var projectSchema = z.looseObject({
74
81
  name: z.string(),
75
82
  path: z.string(),
@@ -89,6 +96,19 @@ var busContextSchema = z.object({
89
96
  roster: rosterSchema,
90
97
  credentials: credentialsSchema.optional()
91
98
  });
99
+ var managedSpawnConfigSchema = z.object({
100
+ command: z.array(z.string()).optional(),
101
+ cwd: z.string().optional(),
102
+ port: z.number().int().nonnegative().optional()
103
+ });
104
+ var discoveryFileSchema = z.object({
105
+ port: z.number().int().nonnegative(),
106
+ pid: z.number().int().min(2),
107
+ identity: z.string(),
108
+ password: z.string(),
109
+ spawnConfig: managedSpawnConfigSchema,
110
+ baseUrl: z.url()
111
+ });
92
112
 
93
113
  // src/config.ts
94
114
  import { existsSync, readFileSync as readFileSync2, realpathSync } from "node:fs";
@@ -128,19 +148,6 @@ function lockFilePath(rosterPath) {
128
148
  function logFilePath(rosterPath) {
129
149
  return join(stateDirFor(rosterPath), "server.log");
130
150
  }
131
- var managedSpawnConfigSchema = z2.object({
132
- command: z2.array(z2.string()).optional(),
133
- cwd: z2.string().optional(),
134
- port: z2.number().int().nonnegative().optional()
135
- });
136
- var discoveryFileSchema = z2.object({
137
- port: z2.number().int().nonnegative(),
138
- pid: z2.number().int().positive(),
139
- identity: z2.string(),
140
- password: z2.string(),
141
- spawnConfig: managedSpawnConfigSchema,
142
- baseUrl: z2.url()
143
- });
144
151
  function writeDiscovery(rosterPath, data) {
145
152
  const dir = stateDirFor(rosterPath);
146
153
  mkdirSync(dir, { recursive: true, mode: 448 });
@@ -181,14 +188,6 @@ function removeDiscovery(rosterPath) {
181
188
  throw err;
182
189
  }
183
190
  }
184
- function loopbackOk(baseUrl) {
185
- try {
186
- const url = new URL(baseUrl);
187
- return LOOPBACK_HOSTS.has(url.hostname);
188
- } catch {
189
- return false;
190
- }
191
- }
192
191
  function attachLive(rosterPath) {
193
192
  const discovery = readDiscovery(rosterPath);
194
193
  if (!discovery)
@@ -231,7 +230,7 @@ function verifyIdentity(pid, storedIdentity) {
231
230
  return current !== null && current === storedIdentity;
232
231
  }
233
232
  var lockFileSchema = z2.object({
234
- pid: z2.number().int().positive(),
233
+ pid: z2.number().int().min(2),
235
234
  startTime: z2.string(),
236
235
  since: z2.number()
237
236
  });
@@ -327,7 +326,7 @@ function provisionalFilePath(rosterPath) {
327
326
  return join(stateDirFor(rosterPath), "spawn.provisional.json");
328
327
  }
329
328
  var provisionalFileSchema = z2.object({
330
- pid: z2.number().int().positive(),
329
+ pid: z2.number().int().min(2),
331
330
  identity: z2.string(),
332
331
  password: z2.string(),
333
332
  since: z2.number()
@@ -523,11 +522,34 @@ function redactedReadinessError(rosterPath, password, reason) {
523
522
  return new Error(`space-bus: managed server for roster ${rosterPath} ${reason}. Log tail:
524
523
  ${tail}`);
525
524
  }
525
+ function isEpermError(err) {
526
+ return typeof err === "object" && err !== null && "code" in err && err.code === "EPERM";
527
+ }
528
+ function signalGroup(pid, sig) {
529
+ if (pid <= 1) {
530
+ try {
531
+ process.kill(pid, sig);
532
+ } catch {}
533
+ return false;
534
+ }
535
+ try {
536
+ process.kill(-pid, sig);
537
+ return true;
538
+ } catch (err) {
539
+ if (err.code === "EPERM") {
540
+ throw err;
541
+ }
542
+ }
543
+ try {
544
+ process.kill(pid, sig);
545
+ } catch {}
546
+ return false;
547
+ }
526
548
  function killIdentifiedProcess(pid, identity) {
527
549
  try {
528
550
  if (identity !== null && identity !== undefined && identity !== "") {
529
551
  if (verifyIdentity(pid, identity))
530
- process.kill(pid, "SIGTERM");
552
+ signalGroup(pid, "SIGTERM");
531
553
  return;
532
554
  }
533
555
  process.kill(pid, "SIGTERM");
@@ -776,23 +798,31 @@ async function stopServer(rosterPath) {
776
798
  return { stopped: false };
777
799
  }
778
800
  const { pid } = discovery;
801
+ let groupSignaled;
802
+ const waitForCompletion = (budgetMs) => groupSignaled ? waitForGroupDeath(pid, budgetMs) : waitForDeath(pid, budgetMs);
779
803
  try {
780
- process.kill(pid, "SIGTERM");
781
- } catch {
804
+ groupSignaled = signalGroup(pid, "SIGTERM");
805
+ } catch (err) {
806
+ if (isEpermError(err)) {
807
+ return { stopped: false };
808
+ }
782
809
  removeDiscovery(rosterPath);
783
810
  return { stopped: true };
784
811
  }
785
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
812
+ if (await waitForCompletion(STOP_GRACE_MS)) {
786
813
  removeDiscovery(rosterPath);
787
814
  return { stopped: true };
788
815
  }
789
816
  try {
790
- process.kill(pid, "SIGKILL");
791
- } catch {
817
+ groupSignaled = signalGroup(pid, "SIGKILL");
818
+ } catch (err) {
819
+ if (isEpermError(err)) {
820
+ return { stopped: false };
821
+ }
792
822
  removeDiscovery(rosterPath);
793
823
  return { stopped: true };
794
824
  }
795
- if (await waitForDeath(pid, STOP_GRACE_MS)) {
825
+ if (await waitForCompletion(STOP_GRACE_MS)) {
796
826
  removeDiscovery(rosterPath);
797
827
  return { stopped: true };
798
828
  }
@@ -807,6 +837,23 @@ async function waitForDeath(pid, budgetMs) {
807
837
  }
808
838
  return !isAlive(pid);
809
839
  }
840
+ async function waitForGroupDeath(pid, budgetMs) {
841
+ const deadline = Date.now() + budgetMs;
842
+ const groupAlive = () => {
843
+ try {
844
+ process.kill(-pid, 0);
845
+ return true;
846
+ } catch {
847
+ return false;
848
+ }
849
+ };
850
+ while (Date.now() < deadline) {
851
+ if (!groupAlive())
852
+ return true;
853
+ await sleep(POLL_INTERVAL_MS);
854
+ }
855
+ return !groupAlive();
856
+ }
810
857
  export {
811
858
  stopServer,
812
859
  serverStatus,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fro.bot/space-bus",
3
- "version": "0.6.1",
3
+ "version": "0.7.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",
@@ -31,6 +31,10 @@
31
31
  "./server": {
32
32
  "types": "./dist/server.d.ts",
33
33
  "import": "./dist/server.js"
34
+ },
35
+ "./attach": {
36
+ "types": "./dist/attach.d.ts",
37
+ "import": "./dist/attach.js"
34
38
  }
35
39
  },
36
40
  "bin": {
@@ -48,7 +52,7 @@
48
52
  "scripts": {
49
53
  "clean": "rm -rf dist",
50
54
  "build": "bun run clean && bun run build.ts && tsc -p tsconfig.build.json --emitDeclarationOnly",
51
- "dev": "bun build ./src/index.ts ./src/mcp.ts ./src/cli.ts ./src/core.ts ./src/config.ts ./src/contract.ts ./src/format.ts ./src/server.ts --outdir dist --target node --external @opencode-ai/plugin --external @modelcontextprotocol/sdk --external zod --watch",
55
+ "dev": "bun build ./src/index.ts ./src/mcp.ts ./src/cli.ts ./src/core.ts ./src/config.ts ./src/contract.ts ./src/format.ts ./src/server.ts --outdir dist --target node --external @opencode-ai/plugin --external @modelcontextprotocol/sdk --external zod --watch & bun build ./src/attach.ts --outdir dist --target node --external @opencode-ai/plugin --external @modelcontextprotocol/sdk --external zod --watch & wait",
52
56
  "typecheck": "tsc --noEmit",
53
57
  "lint": "biome check .",
54
58
  "fix": "biome check --write .",